Browse Source

Consistently check if AsyncContext already completed

Related to SPR-15412
pull/1375/head
Rossen Stoyanchev 9 years ago
parent
commit
f716c8e9bc
  1. 54
      spring-web/src/main/java/org/springframework/http/server/reactive/ServletHttpHandlerAdapter.java

54
spring-web/src/main/java/org/springframework/http/server/reactive/ServletHttpHandlerAdapter.java

@ -108,9 +108,8 @@ public class ServletHttpHandlerAdapter implements Servlet { @@ -108,9 +108,8 @@ public class ServletHttpHandlerAdapter implements Servlet {
ServerHttpRequest httpRequest = createRequest(((HttpServletRequest) request), asyncContext);
ServerHttpResponse httpResponse = createResponse(((HttpServletResponse) response), asyncContext);
asyncContext.addListener(TIMEOUT_LISTENER);
HandlerResultSubscriber subscriber = new HandlerResultSubscriber(asyncContext);
asyncContext.addListener(subscriber);
this.httpHandler.handle(httpRequest, httpResponse).subscribe(subscriber);
}
@ -150,36 +149,12 @@ public class ServletHttpHandlerAdapter implements Servlet { @@ -150,36 +149,12 @@ public class ServletHttpHandlerAdapter implements Servlet {
}
private final static AsyncListener TIMEOUT_LISTENER = new AsyncListener() {
@Override
public void onTimeout(AsyncEvent event) throws IOException {
event.getAsyncContext().complete();
}
@Override
public void onError(AsyncEvent event) throws IOException {
event.getAsyncContext().complete();
}
@Override
public void onStartAsync(AsyncEvent event) throws IOException {
// no-op
}
@Override
public void onComplete(AsyncEvent event) throws IOException {
// no-op
}
};
private class HandlerResultSubscriber implements Subscriber<Void> {
private class HandlerResultSubscriber implements Subscriber<Void>, AsyncListener {
private final AsyncContext asyncContext;
public HandlerResultSubscriber(AsyncContext asyncContext) {
HandlerResultSubscriber(AsyncContext asyncContext) {
this.asyncContext = asyncContext;
}
@ -224,6 +199,29 @@ public class ServletHttpHandlerAdapter implements Servlet { @@ -224,6 +199,29 @@ public class ServletHttpHandlerAdapter implements Servlet {
// e.g. TIMEOUT_LISTENER (above) may have completed the AsyncContext
}
}
// AsyncListener...
@Override
public void onTimeout(AsyncEvent event) throws IOException {
runIfAsyncNotComplete(() -> event.getAsyncContext().complete());
}
@Override
public void onError(AsyncEvent event) throws IOException {
runIfAsyncNotComplete(() -> event.getAsyncContext().complete());
}
@Override
public void onStartAsync(AsyncEvent event) throws IOException {
// No-op
}
@Override
public void onComplete(AsyncEvent event) throws IOException {
// No-op
}
}
}

Loading…
Cancel
Save