This commit is contained in:
Rossen Stoyanchev 2017-06-26 14:50:11 -04:00
parent e0678ba583
commit eb0479dee8
7 changed files with 29 additions and 28 deletions

View File

@ -46,6 +46,7 @@ public interface AsyncWebRequest extends NativeWebRequest {
/** /**
* Add a handler to invoke when an error occurred while concurrent * Add a handler to invoke when an error occurred while concurrent
* handling of a request. * handling of a request.
* @since 5.0
*/ */
void addErrorHandler(Consumer<Throwable> exceptionHandler); void addErrorHandler(Consumer<Throwable> exceptionHandler);

View File

@ -102,17 +102,18 @@ public interface CallableProcessingInterceptor {
<T> Object handleTimeout(NativeWebRequest request, Callable<T> task) throws Exception; <T> Object handleTimeout(NativeWebRequest request, Callable<T> task) throws Exception;
/** /**
* Invoked from a container thread when an error occurred while processing the async request * Invoked from a container thread when an error occurred while processing
* before the {@code Callable} task completes. Implementations may return a value, * the async request before the {@code Callable} task completes.
* including an {@link Exception}, to use instead of the value the * Implementations may return a value, including an {@link Exception}, to
* {@link Callable} did not return in time. * use instead of the value the {@link Callable} did not return in time.
* @param request the current request * @param request the current request
* @param task the task for the current async request * @param task the task for the current async request
* @paramt t the error that occurred while request processing * @param t the error that occurred while request processing
* @return a concurrent result value; if the value is anything other than * @return a concurrent result value; if the value is anything other than
* {@link #RESULT_NONE} or {@link #RESPONSE_HANDLED}, concurrent processing * {@link #RESULT_NONE} or {@link #RESPONSE_HANDLED}, concurrent processing
* is resumed and subsequent interceptors are not invoked * is resumed and subsequent interceptors are not invoked
* @throws Exception in case of errors * @throws Exception in case of errors
* @since 5.0
*/ */
<T> Object handleError(NativeWebRequest request, Callable<T> task, Throwable t) throws Exception; <T> Object handleError(NativeWebRequest request, Callable<T> task, Throwable t) throws Exception;

View File

@ -154,11 +154,13 @@ public class DeferredResult<T> {
} }
/** /**
* Register code to invoke when an error occurred while processing the async request. * Register code to invoke when an error occurred during the async request.
* <p>This method is called from a container thread when an error occurred while * <p>This method is called from a container thread when an error occurs
* processing an async request before the {@code DeferredResult} has been populated. * while processing an async request before the {@code DeferredResult} has
* It may invoke {@link DeferredResult#setResult setResult} or * been populated. It may invoke {@link DeferredResult#setResult setResult}
* {@link DeferredResult#setErrorResult setErrorResult} to resume processing. * or {@link DeferredResult#setErrorResult setErrorResult} to resume
* processing.
* @since 5.0
*/ */
public void onError(Consumer<Throwable> callback) { public void onError(Consumer<Throwable> callback) {
this.errorCallback = callback; this.errorCallback = callback;

View File

@ -27,7 +27,7 @@ import org.springframework.web.context.request.NativeWebRequest;
* @author Violeta Georgieva * @author Violeta Georgieva
* @since 5.0 * @since 5.0
*/ */
public class ErrorCallableProcessingInterceptor extends CallableProcessingInterceptorAdapter { class ErrorCallableProcessingInterceptor extends CallableProcessingInterceptorAdapter {
@Override @Override
public <T> Object handleError(NativeWebRequest request, Callable<T> task, Throwable t) throws Exception { public <T> Object handleError(NativeWebRequest request, Callable<T> task, Throwable t) throws Exception {

View File

@ -143,23 +143,17 @@ public class StandardServletAsyncWebRequest extends ServletWebRequest implements
@Override @Override
public void onError(AsyncEvent event) throws IOException { public void onError(AsyncEvent event) throws IOException {
for (Consumer<Throwable> handler : this.exceptionHandlers) { this.exceptionHandlers.forEach(consumer -> consumer.accept(event.getThrowable()));
handler.accept(event.getThrowable());
}
} }
@Override @Override
public void onTimeout(AsyncEvent event) throws IOException { public void onTimeout(AsyncEvent event) throws IOException {
for (Runnable handler : this.timeoutHandlers) { this.timeoutHandlers.forEach(Runnable::run);
handler.run();
}
} }
@Override @Override
public void onComplete(AsyncEvent event) throws IOException { public void onComplete(AsyncEvent event) throws IOException {
for (Runnable handler : this.completionHandlers) { this.completionHandlers.forEach(Runnable::run);
handler.run();
}
this.asyncContext = null; this.asyncContext = null;
this.asyncCompleted.set(true); this.asyncCompleted.set(true);
} }

View File

@ -153,12 +153,14 @@ public class WebAsyncTask<V> implements BeanFactoryAware {
} }
/** /**
* Register code to invoke when an error occurred while processing the async request. * Register code to invoke for an error during async request processing.
* <p>This method is called from a container thread when an error occurred while processing * <p>This method is called from a container thread when an error occurred
* an async request before the {@code Callable} has completed. The callback is executed in * while processing an async request before the {@code Callable} has
* the same thread and therefore should return without blocking. It may return * completed. The callback is executed in the same thread and therefore
* an alternative value to use, including an {@link Exception} or return * should return without blocking. It may return an alternative value to
* use, including an {@link Exception} or return
* {@link CallableProcessingInterceptor#RESULT_NONE RESULT_NONE}. * {@link CallableProcessingInterceptor#RESULT_NONE RESULT_NONE}.
* @since 5.0
*/ */
public void onError(Callable<V> callback) { public void onError(Callable<V> callback) {
this.errorCallback = callback; this.errorCallback = callback;

View File

@ -217,9 +217,10 @@ public class ResponseBodyEmitter {
} }
/** /**
* Register code to invoke when an error occurred while processing the async request. * Register code to invoke for an error during async request processing.
* This method is called from a container thread when an error occurred while processing * This method is called from a container thread when an error occurred
* an async request. * while processing an async request.
* @since 5.0
*/ */
public synchronized void onError(Consumer<Throwable> callback) { public synchronized void onError(Consumer<Throwable> callback) {
this.errorCallback.setDelegate(callback); this.errorCallback.setDelegate(callback);