序
之前写过一篇文章:springmvc不断输出文本到网页,采用的是对response不断进行write和flush实现的。在spring 4.2版本的时候提供了一个SseEmitter可以直接用来实现这个功能。
实例
@Controller@RequestMapping("/sse")public class SseEmitterController { private static final Logger LOGGER = LoggerFactory.getLogger(SseEmitterController.class); @Autowired @Qualifier("mvcTaskExecutor") ThreadPoolTaskExecutor mvcTaskExecutor; @GetMapping("") public SseEmitter sseDemo() throws InterruptedException { final SseEmitter emitter = new SseEmitter(0L); //timeout设置为0表示不超时 mvcTaskExecutor.execute(() -> { try { for(int i=0;i<100;i++){ emitter.send("hello"+i); LOGGER.info("emit:{}","hello"+i); Thread.sleep(1000*1); } emitter.complete(); } catch (Exception e) { emitter.completeWithError(e); } }); return emitter; }}
输出实例
data:"hello0"data:"hello1"data:"hello2"data:"hello3"data:"hello4"data:"hello5"//......
注意事项
- 这里的SseEmitter的send不能阻塞mvc线程,必须提前返回,然后把send放到异步里头
除了使用上述的放入线程池的方式,也可以直接调用标记有async的方法
- 这里SseEmitter的timeout设置为0,表示不超时. 如果不设置为0,那么如果SseEmitter在指定的时间(
AsyncSupportConfigurer设置的timeout,默认为30秒
)未完成会抛出异常
org.springframework.web.context.request.async.AsyncRequestTimeoutException: null at org.springframework.web.context.request.async.TimeoutDeferredResultProcessingInterceptor.handleTimeout(TimeoutDeferredResultProcessingInterceptor.java:42) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.web.context.request.async.DeferredResultInterceptorChain.triggerAfterTimeout(DeferredResultInterceptorChain.java:75) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.web.context.request.async.WebAsyncManager$5.run(WebAsyncManager.java:392) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.web.context.request.async.StandardServletAsyncWebRequest.onTimeout(StandardServletAsyncWebRequest.java:143) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.apache.catalina.core.AsyncListenerWrapper.fireOnTimeout(AsyncListenerWrapper.java:44) ~[tomcat-embed-core-8.5.16.jar:8.5.16] at org.apache.catalina.core.AsyncContextImpl.timeout(AsyncContextImpl.java:134) ~[tomcat-embed-core-8.5.16.jar:8.5.16] at org.apache.catalina.connector.CoyoteAdapter.asyncDispatch(CoyoteAdapter.java:153) ~[tomcat-embed-core-8.5.16.jar:8.5.16] at org.apache.coyote.AbstractProcessor.dispatch(AbstractProcessor.java:224) ~[tomcat-embed-core-8.5.16.jar:8.5.16] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:53) ~[tomcat-embed-core-8.5.16.jar:8.5.16] at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) ~[tomcat-embed-core-8.5.16.jar:8.5.16] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455) ~[tomcat-embed-core-8.5.16.jar:8.5.16] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.16.jar:8.5.16] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_71] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_71] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.16.jar:8.5.16] at java.lang.Thread.run(Thread.java:745) [na:1.8.0_71]
小结
SseEmitter在运行比较耗时的任务时非常好用,比如实时查看部署进度,比如查看定时任务的实时输出等。