Browse Source

Polish AsyncListener in ServletHttpHandlerAdapter

pull/1204/head
Rossen Stoyanchev 8 years ago
parent
commit
c1e5e3a87e
  1. 39
      spring-web/src/main/java/org/springframework/http/server/reactive/ServletHttpHandlerAdapter.java
  2. 5
      spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java
  3. 16
      spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpResponse.java

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

@ -91,7 +91,7 @@ public class ServletHttpHandlerAdapter extends HttpServlet { @@ -91,7 +91,7 @@ public class ServletHttpHandlerAdapter extends HttpServlet {
servletRequest, this.dataBufferFactory, this.bufferSize);
ServletServerHttpResponse response = new ServletServerHttpResponse(
servletResponse, this.dataBufferFactory, this.bufferSize);
asyncContext.addListener(new HandlerAsyncEvent(request, response));
asyncContext.addListener(new ErrorHandlingAsyncListener(request, response));
HandlerResultSubscriber resultSubscriber = new HandlerResultSubscriber(asyncContext);
this.handler.handle(request, response).subscribe(resultSubscriber);
}
@ -130,40 +130,47 @@ public class ServletHttpHandlerAdapter extends HttpServlet { @@ -130,40 +130,47 @@ public class ServletHttpHandlerAdapter extends HttpServlet {
}
}
private static final class HandlerAsyncEvent implements AsyncListener {
private static final class ErrorHandlingAsyncListener implements AsyncListener {
private final ServletServerHttpRequest request;
private final ServletServerHttpResponse response;
public HandlerAsyncEvent(ServletServerHttpRequest request,
public ErrorHandlingAsyncListener(ServletServerHttpRequest request,
ServletServerHttpResponse response) {
this.request = request;
this.response = response;
}
@Override
public void onComplete(AsyncEvent event) throws IOException {
// no op
public void onTimeout(AsyncEvent event) {
Throwable ex = event.getThrowable();
if (ex == null) {
ex = new IllegalStateException("Async operation timeout.");
}
this.request.handleAsyncListenerError(ex);
this.response.handleAsyncListenerError(ex);
}
@Override
public void onTimeout(AsyncEvent event) throws IOException {
Throwable t = event.getThrowable();
if (t == null) {
t = new IllegalStateException("Async operation timeout.");
}
request.onError(t);
response.onError(t);
public void onError(AsyncEvent event) {
this.request.handleAsyncListenerError(event.getThrowable());
this.response.handleAsyncListenerError(event.getThrowable());
}
@Override
public void onError(AsyncEvent event) throws IOException {
request.onError(event.getThrowable());
response.onError(event.getThrowable());
public void onStartAsync(AsyncEvent event) {
// no op
}
@Override
public void onStartAsync(AsyncEvent event) throws IOException {
public void onComplete(AsyncEvent event) {
// no op
}
}
}

5
spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java

@ -170,9 +170,10 @@ public class ServletServerHttpRequest extends AbstractServerHttpRequest { @@ -170,9 +170,10 @@ public class ServletServerHttpRequest extends AbstractServerHttpRequest {
}
}
void onError(Throwable t) {
/** Handle a timeout/error callback from the Servlet container */
void handleAsyncListenerError(Throwable ex) {
if (this.bodyPublisher != null) {
this.bodyPublisher.onError(t);
this.bodyPublisher.onError(ex);
}
}

16
spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpResponse.java

@ -154,15 +154,15 @@ public class ServletServerHttpResponse extends AbstractListenerServerHttpRespons @@ -154,15 +154,15 @@ public class ServletServerHttpResponse extends AbstractListenerServerHttpRespons
}
}
void onError(Throwable t) {
if (bodyFlushProcessor != null) {
bodyFlushProcessor.cancel();
bodyFlushProcessor.onError(t);
/** Handle a timeout/error callback from the Servlet container */
void handleAsyncListenerError(Throwable ex) {
if (this.bodyFlushProcessor != null) {
this.bodyFlushProcessor.cancel();
this.bodyFlushProcessor.onError(ex);
}
if (bodyProcessor != null) {
bodyProcessor.cancel();
bodyProcessor.onError(t);
if (this.bodyProcessor != null) {
this.bodyProcessor.cancel();
this.bodyProcessor.onError(ex);
}
}

Loading…
Cancel
Save