我使用spring 4.3.7.RELEASE来设置服务器发送事件。并使用@scheduled
注解每2秒发送一次消息。下面是控制器。
@Controller
public class MySSEController {
private final SseEmitter sseEmitter = new SseEmitter();
private int counter = 0;
@RequestMapping("/ssestream")
public SseEmitter getRealTimeMessageAction() throws IOException {
sseEmitter.send("MessageCounter : " + counter);
return sseEmitter;
}
@Scheduled(fixedDelay = 2*1000)
public void scheduledMsgEmitter() throws IOException
{
if(null != sseEmitter) {
sseEmitter.send("MessageCounter : " + ++counter);
}
}
}
我直接从eclipse在Tomcat 9上运行这个程序。应用程序每2秒启动并向浏览器发送消息。但过了一段时间后,它停止发送消息,我在eclipse控制台中看到下面的异常。
Mar 16, 2017 6:57:34 PM org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver handleAsyncRequestTimeoutException
SEVERE: Async timeout for GET [/streaming-web/stream/ssestream]
Mar 16, 2017 6:57:35 PM org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler handleError
SEVERE: Unexpected error occurred in scheduled task.
java.lang.IllegalStateException: ResponseBodyEmitter is already set complete
at org.springframework.util.Assert.state(Assert.java:70)
at org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter.send(ResponseBodyEmitter.java:158)
at org.springframework.web.servlet.mvc.method.annotation.SseEmitter.send(SseEmitter.java:126)
at org.springframework.web.servlet.mvc.method.annotation.SseEmitter.send(SseEmitter.java:107)
at org.springframework.web.servlet.mvc.method.annotation.SseEmitter.send(SseEmitter.java:89)
at com.mycomp.test.controllers.MySSEController.scheduledMsgEmitter(MySSEController.java:25)
at sun.reflect.GeneratedMethodAccessor31.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
我是否遗漏了什么?请帮助我解决此问题。
4条答案
按热度按时间63lcw9qa1#
我不确定这是否是正确的解决方案。
我稍微修改了实现,使其适用于所有连接的客户机Earlier I was unable to deliver the same message to different clients connected to this stream。
ctzwtxfj2#
SseEmitter超时以毫秒为单位:60001是60秒。它是会话超时,不受会话活动的影响。
超时需要设置为会话的预期持续时间,以毫秒为单位。因此,86400000(或更高)是完全合适的。
icomxhvb3#
我使用了
new org.springframework.web.servlet.mvc.method.annotation.SseEmitter(0L)
,但没有完成事件。unhi4e5o4#
记录我获得的有关SSE的信息。
积分:
ScheduledExecutorService
答:
new SseEmitter(0L)
是我的工作参考文献: