Spring SseEmitter causes Cannot forward after response has been committed exception

夙愿已清 提交于 2019-12-10 18:25:17

问题


Given a simple controller with a method like this:

@RequestMapping(method = RequestMethod.GET, value = "{id}/update")
public ResponseEntity<SseEmitter>  update() throws IOException {
    final SseEmitter  sseEmitter = new SseEmitter();

    return ResponseEntity.ok(sseEmitter);
}

I tried it this way also:

@RequestMapping(method = RequestMethod.GET, value = "{id}/update")
public SseEmitter  update() throws IOException {
    final SseEmitter  sseEmitter = new SseEmitter();

    return sseEmitter;
}

But both way Tomcat 8.0.21 throws the aforementioned exception 30 seconds after the request issued. What is happening under the hood?


回答1:


This may be the default async timeout of your servlet container. You can change the default value with Spring:

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        configurer.setDefaultTimeout(1000000);
    }

}



回答2:


You can set the timeout for the SseEmitter in its constructor:

final SseEmitter sseEmitter = new SseEmitter(60000L); // Timeout in millis

The client will reconnect automatically, but by default it will wait 2-3 seconds. You can override this when you send the messages:

// Instruct the client to reconnect after 500ms
emitter.send(SseEmitter.event().reconnectTime(500).data(message));


来源:https://stackoverflow.com/questions/29880336/spring-sseemitter-causes-cannot-forward-after-response-has-been-committed-except

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!