【SpringBoot WEB 系列】异步请求知识点与使用姿势小结

【SpringBoot WEB 系列】异步请求知识点与使用姿势小结

【SpringBoot WEB系列】异步请求知识点与使用姿势小结

在 Servlet3.0 就引入了异步请求的支持,但是在实际的业务开发中,可能用过这个特性的童鞋并不多?

本篇博文作为异步请求的扫盲和使用教程,将包含以下知识点

  • 什么是异步请求,有什么特点,适用场景
  • 四种使用姿势: AsyncContext 方式CallableWebAsyncTaskDeferredResult

I. 异步请求

异步对于我们而言,应该属于经常可以听到的词汇了,在实际的开发中多多少少都会用到,那么什么是异步请求呢

1. 异步请求描述

先介绍一下同步与异步:

一个正常调用,吭哧吭哧执行完毕之后直接返回,这个叫同步;

接收到调用,自己不干,新开一个线程来做,主线程自己则去干其他的事情,等后台线程吭哧吭哧的跑完之后,主线程再返回结果,这个就叫异步

异步请求:

我们这里讲到的异步请求,主要是针对 web 请求而言,后端响应请求的一种手段,同步/异步对于前端而言是无感知、无区别的

同步请求,后端接收到请求之后,直接在处理请求线程中,执行业务逻辑,并返回

【SpringBoot WEB 系列】异步请求知识点与使用姿势小结

异步请求,后端接收到请求之后,新开一个线程,来执行业务逻辑,释放请求线程,避免请求线程被大量耗时的请求沾满,导致服务不可用

【SpringBoot WEB 系列】异步请求知识点与使用姿势小结

2. 特点

通过上面两张图,可以知道异步请求的最主要特点

  • 业务线程,处理请求逻辑
  • 请求处理线程立即释放,通过回调处理线程返回结果

3. 场景分析

从特点出发,也可以很容易看出异步请求,更适用于耗时的请求,快速的释放请求处理线程,避免 web 容器的请求线程被打满,导致服务不可用

举一个稍微极端一点的例子,比如我以前做过的一个多媒体服务,提供图片、音视频的编辑,这些服务接口有同步返回结果的也有异步返回结果的;同步返回结果的接口有快有慢,大部分耗时可能<10ms,而有部分接口耗时则在几十甚至上百

这种场景下,耗时的接口就可以考虑用异步请求的方式来支持了,避免占用过多的请求处理线程,影响其他的服务

II. 使用姿势

接下来介绍四种异步请求的使用姿势,原理一致,只是使用的场景稍有不同

1. AsyncContext

在 Servlet3.0+之后就支持了异步请求,第一种方式比较原始,相当于直接借助 Servlet 的规范来实现,当然下面的 case 并不是直接创建一个 servlet,而是借助AsyncContext来实现

<code>@RestController
@RequestMapping(path = "servlet")
public class ServletRest {

@GetMapping(path = "get")
public void get(HttpServletRequest request) {
AsyncContext asyncContext = request.startAsync();
asyncContext.addListener(new AsyncListener() {
@Override
public void onComplete(AsyncEvent asyncEvent) throws IOException {
System.out.println("操作完成:" + Thread.currentThread().getName());
}

@Override
public void onTimeout(AsyncEvent asyncEvent) throws IOException {
System.out.println("超时返回!!!");
asyncContext.getResponse().setCharacterEncoding("utf-8");
asyncContext.getResponse().setContentType("text/html;charset=UTF-8");
asyncContext.getResponse().getWriter().println("超时了!!!!");
}

@Override
public void onError(AsyncEvent asyncEvent) throws IOException {
System.out.println("出现了m某些异常");
asyncEvent.getThrowable().printStackTrace();

asyncContext.getResponse().setCharacterEncoding("utf-8");
asyncContext.getResponse().setContentType("text/html;charset=UTF-8");
asyncContext.getResponse().getWriter().println("出现了某些异常哦!!!!");
}

@Override
public void onStartAsync(AsyncEvent asyncEvent) throws IOException {
System.out.println("开始执行");
}
});

asyncContext.setTimeout(3000L);
asyncContext.start(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(Long.parseLong(request.getParameter("sleep")));
System.out.println("内部线程:" + Thread.currentThread().getName());
asyncContext.getResponse().setCharacterEncoding("utf-8");
asyncContext.getResponse().setContentType("text/html;charset=UTF-8");

asyncContext.getResponse().getWriter().println("异步返回!");
asyncContext.getResponse().getWriter().flush();
// 异步完成,释放
asyncContext.complete();
} catch (Exception e) {
e.printStackTrace();
}
}
});

System.out.println("主线程over!!! " + Thread.currentThread().getName());
}
}/<code>

完整的实现如上,简单的来看一下一般步骤

  • javax.servlet.ServletRequest#startAsync()获取AsyncContext
  • 添加监听器 asyncContext.addListener(AsyncListener)(这个是可选的) 用户请求开始、超时、异常、完成时回调
  • 设置超时时间 asyncContext.setTimeout(3000L) (可选)
  • 异步任务asyncContext.start(Runnable)

2. Callable

相比较于上面的复杂的示例,SpringMVC 可以非常 easy 的实现,直接返回一个Callable即可

<code>@RestController
@RequestMapping(path = "call")
public class CallableRest {

@GetMapping(path = "get")
public Callable<string> get() {
Callable<string> callable = new Callable<string>() {
@Override
public String call() throws Exception {
System.out.println("do some thing");

Thread.sleep(1000);
System.out.println("执行完毕,返回!!!");
return "over!";
}
};

return callable;
}


@GetMapping(path = "exception")
public Callable<string> exception() {
Callable<string> callable = new Callable<string>() {
@Override
public String call() throws Exception {
System.out.println("do some thing");
Thread.sleep(1000);
System.out.println("出现异常,返回!!!");
throw new RuntimeException("some error!");
}
};

return callable;
}
}/<string>/<string>/<string>/<string>/<string>/<string>/<code>

请注意上面的两种 case,一个正常返回,一个业务执行过程中,抛出来异常

分别请求,输出如下

<code># http://localhost:8080/call/get
do some thing
执行完毕,返回!!!/<code>

异常请求: http://localhost:8080/call/exception

【SpringBoot WEB 系列】异步请求知识点与使用姿势小结

<code>do some thing
出现异常,返回!!!
2020-03-29 16:12:06.014 ERROR 24084 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] threw exception

java.lang.RuntimeException: some error!
\tat com.git.hui.boot.async.rest.CallableRest$2.call(CallableRest.java:40) ~[classes/:na]
\tat com.git.hui.boot.async.rest.CallableRest$2.call(CallableRest.java:34) ~[classes/:na]
\tat org.springframework.web.context.request.async.WebAsyncManager.lambda$startCallableProcessing$4(WebAsyncManager.java:328) ~[spring-web-5.2.1.RELEASE.jar:5.2.1.RELEASE]
\tat java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[na:1.8.0_171]
\tat java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) ~[na:1.8.0_171]
\tat java.util.concurrent.FutureTask.run(FutureTask.java) ~[na:1.8.0_171]
\tat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_171]
\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_171]
\tat java.lang.Thread.run(Thread.java:748) [na:1.8.0_171]/<code>

3. WebAsyncTask

callable 的方式,非常直观简单,但是我们经常关注的超时+异常的处理却不太好,这个时候我们可以用WebAsyncTask,实现姿势也很简单,包装一下callable,然后设置各种回调事件即可

<code>@RestController
@RequestMapping(path = "task")
public class WebAysncTaskRest {

@GetMapping(path = "get")
public WebAsyncTask<string> get(long sleep, boolean error) {
Callable<string> callable = () -> {
System.out.println("do some thing");
Thread.sleep(sleep);

if (error) {
System.out.println("出现异常,返回!!!");
throw new RuntimeException("异常了!!!");
}

return "hello world";
};

// 指定3s的超时
WebAsyncTask<string> webTask = new WebAsyncTask<>(3000, callable);

webTask.onCompletion(() -> System.out.println("over!!!"));

webTask.onTimeout(() -> {
System.out.println("超时了");
return "超时返回!!!";
});

webTask.onError(() -> {
System.out.println("出现异常了!!!");
return "异常返回";
});

return webTask;
}
}/<string>/<string>/<string>/<code>

4. DeferredResult

DeferredResult与WebAsyncTask最大的区别就是前者不确定什么时候会返回结果,

DeferredResult的这个特点,可以用来做实现很多有意思的东西,如后面将介绍的SseEmitter就用到了它

下面给出一个实例

<code>@RestController
@RequestMapping(path = "defer")
public class DeferredResultRest {

private Map<string> cache = new ConcurrentHashMap<>();

@GetMapping(path = "get")
public DeferredResult<string> get(String id) {
DeferredResult<string> res = new DeferredResult<>();
cache.put(id, res);

res.onCompletion(new Runnable() {
@Override
public void run() {
System.out.println("over!");
}
});
return res;

}

@GetMapping(path = "pub")
public String publish(String id, String content) {
DeferredResult<string> res = cache.get(id);
if (res == null) {
return "no consumer!";
}

res.setResult(content);
return "over!";
}
}/<string>/<string>/<string>/<string>/<code>

在上面的实例中,用户如果先访问http://localhost:8080/defer/get?id=yihuihui,不会立马有结果,直到用户再次访问http://localhost:8080/defer/pub?id=yihuihui&content=哈哈时,前面的请求才会有结果返回

【SpringBoot WEB 系列】异步请求知识点与使用姿势小结

那么这个可以设置超时么,如果一直把前端挂住,貌似也不太合适吧

  • 在构造方法中指定超时时间: new DeferredResult<>(3000L)
  • 设置全局的默认超时时间
<code>@Configuration
@EnableWebMvc
public class WebConf implements WebMvcConfigurer {

@Override
public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
// 超时时间设置为60s
configurer.setDefaultTimeout(TimeUnit.SECONDS.toMillis(10));
}
}/<code>


分享到:


相關文章: