问题
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