use of jakarta.servlet.AsyncEvent in project tomcat by apache.
the class AsyncContextImpl method setErrorState.
public void setErrorState(Throwable t, boolean fireOnError) {
if (t != null) {
request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, t);
}
request.getCoyoteRequest().action(ActionCode.ASYNC_ERROR, null);
if (fireOnError) {
if (log.isDebugEnabled()) {
log.debug(sm.getString("asyncContextImpl.fireOnError"));
}
AsyncEvent errorEvent = new AsyncEvent(event.getAsyncContext(), event.getSuppliedRequest(), event.getSuppliedResponse(), t);
List<AsyncListenerWrapper> listenersCopy = new ArrayList<>(listeners);
for (AsyncListenerWrapper listener : listenersCopy) {
try {
listener.fireOnError(errorEvent);
} catch (Throwable t2) {
ExceptionUtils.handleThrowable(t2);
log.warn(sm.getString("asyncContextImpl.onErrorError", listener.getClass().getName()), t2);
}
}
}
AtomicBoolean result = new AtomicBoolean();
request.getCoyoteRequest().action(ActionCode.ASYNC_IS_ERROR, result);
if (result.get()) {
// No listener called dispatch() or complete(). This is an error.
// SRV.2.3.3.3 (search for "error dispatch")
// Take a local copy to avoid threading issues if another thread
// clears this (can happen during error handling with non-container
// threads)
ServletResponse servletResponse = this.servletResponse;
if (servletResponse instanceof HttpServletResponse) {
((HttpServletResponse) servletResponse).setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
Host host = (Host) context.getParent();
Valve stdHostValve = host.getPipeline().getBasic();
if (stdHostValve instanceof StandardHostValve) {
((StandardHostValve) stdHostValve).throwable(request, request.getResponse(), t);
}
request.getCoyoteRequest().action(ActionCode.ASYNC_IS_ERROR, result);
if (result.get()) {
// Still in the error state. The error page did not call
// complete() or dispatch(). Complete the async processing.
complete();
}
}
}
use of jakarta.servlet.AsyncEvent in project spring-framework by spring-projects.
the class OpenEntityManagerInViewTests method testOpenEntityManagerInViewInterceptorAsyncErrorScenario.
@Test
public void testOpenEntityManagerInViewInterceptorAsyncErrorScenario() throws Exception {
// Initial request thread
OpenEntityManagerInViewInterceptor interceptor = new OpenEntityManagerInViewInterceptor();
interceptor.setEntityManagerFactory(factory);
given(this.factory.createEntityManager()).willReturn(this.manager);
interceptor.preHandle(this.webRequest);
assertThat(TransactionSynchronizationManager.hasResource(this.factory)).isTrue();
AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.request);
asyncManager.setTaskExecutor(this.taskExecutor);
asyncManager.setAsyncWebRequest(asyncWebRequest);
asyncManager.startCallableProcessing((Callable<String>) () -> "anything");
this.taskExecutor.await();
assertThat(asyncManager.getConcurrentResult()).as("Concurrent result ").isEqualTo("anything");
interceptor.afterConcurrentHandlingStarted(this.webRequest);
assertThat(TransactionSynchronizationManager.hasResource(this.factory)).isFalse();
// Async request error
given(this.manager.isOpen()).willReturn(true);
MockAsyncContext asyncContext = (MockAsyncContext) this.request.getAsyncContext();
for (AsyncListener listener : asyncContext.getListeners()) {
listener.onError(new AsyncEvent(asyncContext, new Exception()));
}
for (AsyncListener listener : asyncContext.getListeners()) {
listener.onComplete(new AsyncEvent(asyncContext));
}
verify(this.manager).close();
}
use of jakarta.servlet.AsyncEvent in project spring-framework by spring-projects.
the class StandardServletAsyncWebRequestTests method startAsyncAfterCompleted.
@Test
public void startAsyncAfterCompleted() throws Exception {
this.asyncRequest.onComplete(new AsyncEvent(new MockAsyncContext(this.request, this.response)));
assertThatIllegalStateException().isThrownBy(this.asyncRequest::startAsync).withMessage("Async processing has already completed");
}
use of jakarta.servlet.AsyncEvent in project spring-framework by spring-projects.
the class StandardServletAsyncWebRequestTests method onErrorHandlerAfterOnErrorEvent.
// SPR-13292
@SuppressWarnings("unchecked")
@Test
public void onErrorHandlerAfterOnErrorEvent() throws Exception {
Consumer<Throwable> handler = mock(Consumer.class);
this.asyncRequest.addErrorHandler(handler);
this.asyncRequest.startAsync();
Exception e = new Exception();
this.asyncRequest.onError(new AsyncEvent(this.request.getAsyncContext(), e));
verify(handler).accept(e);
}
use of jakarta.servlet.AsyncEvent in project spring-framework by spring-projects.
the class WebAsyncManagerErrorTests method startCallableProcessingErrorAndComplete.
@Test
public void startCallableProcessingErrorAndComplete() throws Exception {
StubCallable callable = new StubCallable();
CallableProcessingInterceptor interceptor = mock(CallableProcessingInterceptor.class);
Exception e = new Exception();
given(interceptor.handleError(this.asyncWebRequest, callable, e)).willReturn(RESULT_NONE);
this.asyncManager.registerCallableInterceptor("interceptor", interceptor);
this.asyncManager.startCallableProcessing(callable);
AsyncEvent event = new AsyncEvent(new MockAsyncContext(this.servletRequest, this.servletResponse), e);
this.asyncWebRequest.onError(event);
this.asyncWebRequest.onComplete(event);
assertThat(this.asyncManager.hasConcurrentResult()).isTrue();
assertThat(this.asyncManager.getConcurrentResult()).isEqualTo(e);
verify(interceptor).beforeConcurrentHandling(this.asyncWebRequest, callable);
verify(interceptor).afterCompletion(this.asyncWebRequest, callable);
}
Aggregations