当前位置: 移动技术网 > IT编程>开发语言>Java > Spring Boot实现异步请求(Servlet 3.0)

Spring Boot实现异步请求(Servlet 3.0)

2019年07月22日  | 移动技术网IT编程  | 我要评论
在spring 3.2 及以后版本中增加了对请求的异步处理,旨在提高请求的处理速度降低服务性能消耗。 在我们的请求中做了耗时处理,当并发请求的情况下,为了避免web se

在spring 3.2 及以后版本中增加了对请求的异步处理,旨在提高请求的处理速度降低服务性能消耗。

在我们的请求中做了耗时处理,当并发请求的情况下,为了避免web server的连接池被长期占用而引起性能问题,调用后生成一个非web的服务线程来处理,增加web服务器的吞吐量。

为此 servlet 3.0 新增了请求的异步处理,spring 也在此基础上做了封装处理。

本文还是以代码例子的方式说明如何在 spring boot 中应用异步请求。

首先说一下几个要点:

1、@webfilter 和 @webservlet 注解中的 asyncsupported = true 属性

异步处理的servlet若存在过滤器,则过滤器的注解@webfilter应设置asyncsupported=true,

否则会报错 a filter or servlet of the current chain does not support asynchronous operations.

2、@enableasync 注解

spring boot 默认添加了一些拦截 /* 的过滤器,因为 /* 会拦截所有请求,按理说我们也要设置 asyncsupported=true 属性。因为这些过滤器都是 spring boot 初始化的,所以它提供了 @enableasync 注解来统一配置,该注解只针对 “非 @webfilter 和 @webservlet 注解的有效”,所以我们自己定义的 filter 还是需要自己配置 asyncsupported=true 的。

3、asynccontext 对象

获取一个异步请求的上下文对象。

4、asynccontext.settimeout(20 * 1000l);

我们不能让异步请求无限的等待下去,通过 settimeout 来设定最大超时时间。

下面通过两种方式来测试异步任务:

先在 springbootsampleapplication 上添加 @enableasync 注解。

再检查所有自定义的filter,如存在如下两种情况需要配置 asyncsupported=true

1) 自定义filter 拦截了 /*

2) 某filter 拦截了 /shanhy/* ,我们需要执行的异步请求的 servlet 为 /shanhy/testcomet

方法一:原生servlet方式

package org.springboot.sample.servlet;

import java.io.ioexception;
import java.util.queue;
import java.util.concurrent.linkedblockingqueue;
import java.util.concurrent.timeunit;

import javax.servlet.asynccontext;
import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

/**
 * http长连接实现
 *
 * @author 单红宇(365384722)
 * @myblog http://blog.csdn.net/catoop/
 * @create 2016年3月29日
 */
@webservlet(urlpatterns = "/xs/cometservlet", asyncsupported = true)
//异步处理的servlet若存在过滤器,则过滤器的注解@webfilter应设置asyncsupported=true,
//否则会报错a filter or servlet of the current chain does not support asynchronous operations.
public class cometservlet extends httpservlet {

 private static final long serialversionuid = -8685285401859800066l;

 private final queue<asynccontext> asynccontexts = new linkedblockingqueue<>();

 private final thread generator = new thread("async event generator") {

  @override
  public void run() {
   while (!generator.isinterrupted()) {// 线程有效
    try {
     while (!asynccontexts.isempty()) {// 不为空
      timeunit.seconds.sleep(10);// 秒,模拟耗时操作
      asynccontext asynccontext = asynccontexts.poll();
      httpservletresponse res = (httpservletresponse) asynccontext.getresponse();
      res.getwriter().write("{\"result\":\"ok - "+system.currenttimemillis()+"\"}");
      res.setstatus(httpservletresponse.sc_ok);
      res.setcontenttype("application/json");
      asynccontext.complete();// 完成
     }
    } catch (interruptedexception e) {
     thread.currentthread().interrupt();
     e.printstacktrace();
    } catch (ioexception e) {
     e.printstacktrace();
    }
   }
  }

 };

 @override
 public void init() throws servletexception {
  super.init();
  generator.start();
 }

 @override
 protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
  system.out.println(">>>>>>>>>>cometservlet request<<<<<<<<<<<");
  dopost(req, resp);
 }

 @override
 protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
  asynccontext asynccontext = req.startasync();
  asynccontext.settimeout(20 * 1000l);
  asynccontexts.offer(asynccontext);
 }

 @override
 public void destroy() {
  super.destroy();
  generator.interrupt();
 }


}

方法二:controller 方式

@controller
public class pagecontroller {

 @requestmapping("/async/test")
 @responsebody
 public callable<string> callable() {
  // 这么做的好处避免web server的连接池被长期占用而引起性能问题,
  // 调用后生成一个非web的服务线程来处理,增加web服务器的吞吐量。
  return new callable<string>() {
   @override
   public string call() throws exception {
    thread.sleep(3 * 1000l);
    return "小单 - " + system.currenttimemillis();
   }
  };
 }

}

最后写一个comet.jsp页面测试:

<%@ page pageencoding="utf-8"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
 <head>
 <title>长连接测试</title>
 <script type="text/javascript" src="${pagecontext.request.contextpath }/webjarslocator/jquery/jquery.js"></script>
 <script type="text/javascript">
  $(function(){
   function longpolling(){
    $.getjson('${pagecontext.request.contextpath }/xs/cometservlet', function(data){
     console.log(data.result);
     $('#n1').html(data.result);
     longpolling();
    });
   }
   longpolling();

   function longpolling2(){
    $.get('${pagecontext.request.contextpath }/async/test', function(data){
     console.log(data);
     $('#n2').html(data);
     longpolling2();
    });
   }
   longpolling2();
  });
 </script>
 </head>

 <body>
 <h1>长连接测试</h1>
 <h2 id="n1"></h2>
 <h2 id="n2"></h2>
 </body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网