Add onError callback to DeferredResult

Issue: SPR-15614
This commit is contained in:
Violeta Georgieva
2017-06-23 16:48:40 +03:00
committed by Rossen Stoyanchev
parent 140542e8b1
commit e0678ba583
27 changed files with 771 additions and 41 deletions

View File

@@ -219,6 +219,7 @@ class ReactiveTypeHandler {
terminate();
this.emitter.complete();
});
this.emitter.onError(t -> this.emitter.completeWithError(t));
subscription.request(1);
}

View File

@@ -19,6 +19,7 @@ package org.springframework.web.servlet.mvc.method.annotation;
import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.Consumer;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpResponse;
@@ -74,6 +75,8 @@ public class ResponseBodyEmitter {
private final DefaultCallback timeoutCallback = new DefaultCallback();
private final ErrorCallback errorCallback = new ErrorCallback();
private final DefaultCallback completionCallback = new DefaultCallback();
@@ -123,6 +126,7 @@ public class ResponseBodyEmitter {
}
else {
this.handler.onTimeout(this.timeoutCallback);
this.handler.onError(this.errorCallback);
this.handler.onCompletion(this.completionCallback);
}
}
@@ -168,11 +172,9 @@ public class ResponseBodyEmitter {
this.handler.send(object, mediaType);
}
catch (IOException ex) {
completeWithError(ex);
throw ex;
}
catch (Throwable ex) {
completeWithError(ex);
throw new IllegalStateException("Failed to send " + object, ex);
}
}
@@ -214,6 +216,15 @@ public class ResponseBodyEmitter {
this.timeoutCallback.setDelegate(callback);
}
/**
* Register code to invoke when an error occurred while processing the async request.
* This method is called from a container thread when an error occurred while processing
* an async request.
*/
public synchronized void onError(Consumer<Throwable> callback) {
this.errorCallback.setDelegate(callback);
}
/**
* Register code to invoke when the async request completes. This method is
* called from a container thread when an async request completed for any
@@ -244,6 +255,8 @@ public class ResponseBodyEmitter {
void onTimeout(Runnable callback);
void onError(Consumer<Throwable> callback);
void onCompletion(Runnable callback);
}
@@ -291,4 +304,22 @@ public class ResponseBodyEmitter {
}
}
private class ErrorCallback implements Consumer<Throwable> {
private Consumer<Throwable> delegate;
public void setDelegate(Consumer<Throwable> callback) {
this.delegate = callback;
}
@Override
public void accept(Throwable t) {
ResponseBodyEmitter.this.complete = true;
if (this.delegate != null) {
this.delegate.accept(t);
}
}
}
}

View File

@@ -19,6 +19,8 @@ package org.springframework.web.servlet.mvc.method.annotation;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.function.Consumer;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -217,6 +219,11 @@ public class ResponseBodyEmitterReturnValueHandler implements HandlerMethodRetur
this.deferredResult.onTimeout(callback);
}
@Override
public void onError(Consumer<Throwable> callback) {
this.deferredResult.onError(callback);
}
@Override
public void onCompletion(Runnable callback) {
this.deferredResult.onCompletion(callback);