Search in sources :

Example 6 with AsyncWebRequest

use of org.springframework.web.context.request.async.AsyncWebRequest in project spring-framework by spring-projects.

the class OpenEntityManagerInViewTests method testOpenEntityManagerInViewInterceptorAsyncTimeoutScenario.

@Test
public void testOpenEntityManagerInViewInterceptorAsyncTimeoutScenario() 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 timeout
    given(this.manager.isOpen()).willReturn(true);
    MockAsyncContext asyncContext = (MockAsyncContext) this.request.getAsyncContext();
    for (AsyncListener listener : asyncContext.getListeners()) {
        listener.onTimeout(new AsyncEvent(asyncContext));
    }
    for (AsyncListener listener : asyncContext.getListeners()) {
        listener.onComplete(new AsyncEvent(asyncContext));
    }
    verify(this.manager).close();
}
Also used : WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) AsyncListener(jakarta.servlet.AsyncListener) MockAsyncContext(org.springframework.web.testfixture.servlet.MockAsyncContext) AsyncWebRequest(org.springframework.web.context.request.async.AsyncWebRequest) StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) AsyncEvent(jakarta.servlet.AsyncEvent) Test(org.junit.jupiter.api.Test)

Example 7 with AsyncWebRequest

use of org.springframework.web.context.request.async.AsyncWebRequest in project spring-framework by spring-projects.

the class DeferredResultReturnValueHandlerTests method setup.

@BeforeEach
public void setup() throws Exception {
    this.handler = new DeferredResultMethodReturnValueHandler();
    this.request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    this.webRequest = new ServletWebRequest(this.request, response);
    AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, response);
    WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncWebRequest(asyncWebRequest);
    this.request.setAsyncSupported(true);
}
Also used : StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) AsyncWebRequest(org.springframework.web.context.request.async.AsyncWebRequest) StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 8 with AsyncWebRequest

use of org.springframework.web.context.request.async.AsyncWebRequest in project spring-framework by spring-projects.

the class ResponseBodyEmitterReturnValueHandlerTests method responseBodyEmitterWithErrorValue.

@SuppressWarnings("unchecked")
@Test
public void responseBodyEmitterWithErrorValue() throws Exception {
    AsyncWebRequest asyncWebRequest = mock(AsyncWebRequest.class);
    WebAsyncUtils.getAsyncManager(this.request).setAsyncWebRequest(asyncWebRequest);
    ResponseBodyEmitter emitter = new ResponseBodyEmitter(19000L);
    emitter.onError(mock(Consumer.class));
    emitter.onCompletion(mock(Runnable.class));
    MethodParameter type = on(TestController.class).resolveReturnType(ResponseBodyEmitter.class);
    this.handler.handleReturnValue(emitter, type, this.mavContainer, this.webRequest);
    verify(asyncWebRequest).addErrorHandler(any(Consumer.class));
    verify(asyncWebRequest, times(2)).addCompletionHandler(any(Runnable.class));
    verify(asyncWebRequest).startAsync();
}
Also used : Consumer(java.util.function.Consumer) AsyncWebRequest(org.springframework.web.context.request.async.AsyncWebRequest) StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.jupiter.api.Test)

Example 9 with AsyncWebRequest

use of org.springframework.web.context.request.async.AsyncWebRequest in project spring-framework by spring-projects.

the class HandlerFunctionAdapter method getWebAsyncManager.

private WebAsyncManager getWebAsyncManager(HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
    AsyncWebRequest asyncWebRequest = WebAsyncUtils.createAsyncWebRequest(servletRequest, servletResponse);
    asyncWebRequest.setTimeout(this.asyncRequestTimeout);
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(servletRequest);
    asyncManager.setAsyncWebRequest(asyncWebRequest);
    return asyncManager;
}
Also used : WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) AsyncWebRequest(org.springframework.web.context.request.async.AsyncWebRequest)

Example 10 with AsyncWebRequest

use of org.springframework.web.context.request.async.AsyncWebRequest in project spring-framework by spring-projects.

the class DefaultAsyncServerResponse method writeAsync.

static void writeAsync(HttpServletRequest request, HttpServletResponse response, DeferredResult<?> deferredResult) throws ServletException, IOException {
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
    AsyncWebRequest asyncWebRequest = WebAsyncUtils.createAsyncWebRequest(request, response);
    asyncManager.setAsyncWebRequest(asyncWebRequest);
    try {
        asyncManager.startDeferredResultProcessing(deferredResult);
    } catch (IOException | ServletException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new ServletException("Async processing failed", ex);
    }
}
Also used : WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) ServletException(jakarta.servlet.ServletException) IOException(java.io.IOException) AsyncWebRequest(org.springframework.web.context.request.async.AsyncWebRequest) TimeoutException(java.util.concurrent.TimeoutException) ServletException(jakarta.servlet.ServletException) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

AsyncWebRequest (org.springframework.web.context.request.async.AsyncWebRequest)14 StandardServletAsyncWebRequest (org.springframework.web.context.request.async.StandardServletAsyncWebRequest)11 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)7 WebAsyncManager (org.springframework.web.context.request.async.WebAsyncManager)7 Test (org.junit.jupiter.api.Test)6 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)5 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)5 BeforeEach (org.junit.jupiter.api.BeforeEach)4 AsyncEvent (jakarta.servlet.AsyncEvent)3 AsyncListener (jakarta.servlet.AsyncListener)3 MethodParameter (org.springframework.core.MethodParameter)3 MockAsyncContext (org.springframework.web.testfixture.servlet.MockAsyncContext)3 ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)2 EntityManager (jakarta.persistence.EntityManager)1 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)1 FilterChain (jakarta.servlet.FilterChain)1 ServletException (jakarta.servlet.ServletException)1 IOException (java.io.IOException)1 Callable (java.util.concurrent.Callable)1 CompletionException (java.util.concurrent.CompletionException)1