Handle errors via AsyncListener

This is a limited backport of commit #e0678b mainly providing the fix without
exposing the onError callbacks.

Issue: SPR-16058
This commit is contained in:
Rossen Stoyanchev
2017-11-08 23:10:16 -05:00
parent 5867ea0c3c
commit 97bc2762e1
5 changed files with 52 additions and 6 deletions

View File

@@ -50,6 +50,8 @@ public class StandardServletAsyncWebRequest extends ServletWebRequest implements
private final List<Runnable> timeoutHandlers = new ArrayList<Runnable>();
private ErrorHandler errorHandler;
private final List<Runnable> completionHandlers = new ArrayList<Runnable>();
@@ -78,6 +80,10 @@ public class StandardServletAsyncWebRequest extends ServletWebRequest implements
this.timeoutHandlers.add(timeoutHandler);
}
void setErrorHandler(ErrorHandler errorHandler) {
this.errorHandler = errorHandler;
}
@Override
public void addCompletionHandler(Runnable runnable) {
this.completionHandlers.add(runnable);
@@ -134,7 +140,9 @@ public class StandardServletAsyncWebRequest extends ServletWebRequest implements
@Override
public void onError(AsyncEvent event) throws IOException {
onComplete(event);
if (this.errorHandler != null) {
this.errorHandler.handle(event.getThrowable());
}
}
@Override
@@ -153,4 +161,11 @@ public class StandardServletAsyncWebRequest extends ServletWebRequest implements
this.asyncCompleted.set(true);
}
interface ErrorHandler {
void handle(Throwable ex);
}
}

View File

@@ -298,6 +298,16 @@ public final class WebAsyncManager {
}
});
if (this.asyncWebRequest instanceof StandardServletAsyncWebRequest) {
((StandardServletAsyncWebRequest) this.asyncWebRequest).setErrorHandler(
new StandardServletAsyncWebRequest.ErrorHandler() {
@Override
public void handle(Throwable ex) {
setConcurrentResultAndDispatch(ex);
}
});
}
this.asyncWebRequest.addCompletionHandler(new Runnable() {
@Override
public void run() {
@@ -399,6 +409,16 @@ public final class WebAsyncManager {
}
});
if (this.asyncWebRequest instanceof StandardServletAsyncWebRequest) {
((StandardServletAsyncWebRequest) this.asyncWebRequest).setErrorHandler(
new StandardServletAsyncWebRequest.ErrorHandler() {
@Override
public void handle(Throwable ex) {
deferredResult.setErrorResult(ex);
}
});
}
this.asyncWebRequest.addCompletionHandler(new Runnable() {
@Override
public void run() {