Search in sources :

Example 1 with WebAsyncManager

use of cn.taketoday.web.context.async.WebAsyncManager in project today-infrastructure by TAKETODAY.

the class ErrorPageFilterTests method setUpAsyncDispatch.

private void setUpAsyncDispatch() throws Exception {
    this.request.setAsyncSupported(true);
    this.request.setAsyncStarted(true);
    DeferredResult<String> result = new DeferredResult<>();
    ServletRequestContext context = new ServletRequestContext(null, request, response);
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(context);
    asyncManager.setAsyncRequest(new StandardServletAsyncWebRequest(context));
    asyncManager.startDeferredResultProcessing(result);
}
Also used : WebAsyncManager(cn.taketoday.web.context.async.WebAsyncManager) StandardServletAsyncWebRequest(cn.taketoday.web.context.async.StandardServletAsyncWebRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) DeferredResult(cn.taketoday.web.context.async.DeferredResult)

Example 2 with WebAsyncManager

use of cn.taketoday.web.context.async.WebAsyncManager in project today-infrastructure by TAKETODAY.

the class RequestMappingHandlerAdapter method invokeHandlerMethod.

/**
 * Invoke the {@link RequestMapping} handler method preparing a {@link ModelAndView}
 * if view resolution is required.
 *
 * @see #createInvocableHandlerMethod(RequestContext, HandlerMethod)
 */
@Nullable
protected Object invokeHandlerMethod(RequestContext request, HandlerMethod handlerMethod) throws Throwable {
    BindingContext bindingContext = createBindingContext(handlerMethod);
    ModelFactory modelFactory = getModelFactory(handlerMethod, bindingContext);
    // ActionMappingAnnotationHandler handler = annotationHandlerMap.get(handlerMethod);
    ServletInvocableHandlerMethod invocableMethod = createInvocableHandlerMethod(request, handlerMethod);
    invocableMethod.setReturnValueHandlerManager(returnValueHandlerManager);
    RedirectModel inputRedirectModel = RequestContextUtils.getInputRedirectModel(request, redirectModelManager);
    bindingContext.addAllAttributes(inputRedirectModel);
    modelFactory.initModel(request, bindingContext, invocableMethod);
    AsyncWebRequest asyncWebRequest = WebAsyncUtils.createAsyncWebRequest(request);
    asyncWebRequest.setTimeout(asyncRequestTimeout);
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
    asyncManager.setTaskExecutor(taskExecutor);
    asyncManager.setAsyncRequest(asyncWebRequest);
    asyncManager.registerCallableInterceptors(callableInterceptors);
    asyncManager.registerDeferredResultInterceptors(deferredResultInterceptors);
    if (asyncManager.hasConcurrentResult()) {
        Object result = asyncManager.getConcurrentResult();
        bindingContext = asyncManager.getBindingContext();
        asyncManager.clearConcurrentResult();
        LogFormatUtils.traceDebug(log, traceOn -> {
            String formatted = LogFormatUtils.formatValue(result, !traceOn);
            return "Resume with async result [" + formatted + "]";
        });
        invocableMethod = invocableMethod.wrapConcurrentResult(result);
    }
    Object returnValue = invocableMethod.invokeAndHandle(request, bindingContext);
    if (asyncManager.isConcurrentHandlingStarted()) {
        return null;
    }
    // modelFactory.updateModel(request, bindingContext);
    return returnValue;
}
Also used : WebAsyncManager(cn.taketoday.web.context.async.WebAsyncManager) RedirectModel(cn.taketoday.web.view.RedirectModel) BindingContext(cn.taketoday.web.BindingContext) AsyncWebRequest(cn.taketoday.web.context.async.AsyncWebRequest) Nullable(cn.taketoday.lang.Nullable)

Example 3 with WebAsyncManager

use of cn.taketoday.web.context.async.WebAsyncManager in project today-framework by TAKETODAY.

the class ErrorPageFilterTests method setUpAsyncDispatch.

private void setUpAsyncDispatch() throws Exception {
    this.request.setAsyncSupported(true);
    this.request.setAsyncStarted(true);
    DeferredResult<String> result = new DeferredResult<>();
    ServletRequestContext context = new ServletRequestContext(null, request, response);
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(context);
    asyncManager.setAsyncRequest(new StandardServletAsyncWebRequest(request, response));
    asyncManager.startDeferredResultProcessing(result);
}
Also used : WebAsyncManager(cn.taketoday.web.context.async.WebAsyncManager) StandardServletAsyncWebRequest(cn.taketoday.web.context.async.StandardServletAsyncWebRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) DeferredResult(cn.taketoday.web.context.async.DeferredResult)

Example 4 with WebAsyncManager

use of cn.taketoday.web.context.async.WebAsyncManager in project today-framework by TAKETODAY.

the class RequestMappingHandlerAdapter method invokeHandlerMethod.

/**
 * Invoke the {@link RequestMapping} handler method preparing a {@link ModelAndView}
 * if view resolution is required.
 *
 * @see #createInvocableHandlerMethod(RequestContext, HandlerMethod)
 */
@Nullable
protected Object invokeHandlerMethod(RequestContext request, HandlerMethod handlerMethod) throws Throwable {
    BindingContext bindingContext = createBindingContext(handlerMethod);
    ModelFactory modelFactory = getModelFactory(handlerMethod, bindingContext);
    ServletInvocableHandlerMethod invocableMethod = createInvocableHandlerMethod(request, handlerMethod);
    RedirectModel inputRedirectModel = RequestContextUtils.getInputRedirectModel(request, redirectModelManager);
    bindingContext.addAllAttributes(inputRedirectModel);
    modelFactory.initModel(request, bindingContext, invocableMethod);
    AsyncWebRequest asyncWebRequest = WebAsyncUtils.createAsyncWebRequest(request);
    asyncWebRequest.setTimeout(asyncRequestTimeout);
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
    asyncManager.setTaskExecutor(taskExecutor);
    asyncManager.setAsyncRequest(asyncWebRequest);
    asyncManager.registerCallableInterceptors(callableInterceptors);
    asyncManager.registerDeferredResultInterceptors(deferredResultInterceptors);
    if (asyncManager.hasConcurrentResult()) {
        Object result = asyncManager.getConcurrentResult();
        bindingContext = asyncManager.getBindingContext();
        asyncManager.clearConcurrentResult();
        LogFormatUtils.traceDebug(log, traceOn -> {
            String formatted = LogFormatUtils.formatValue(result, !traceOn);
            return "Resume with async result [" + formatted + "]";
        });
        invocableMethod = invocableMethod.wrapConcurrentResult(result);
    }
    Object returnValue = invocableMethod.invokeAndHandle(request, bindingContext);
    if (asyncManager.isConcurrentHandlingStarted()) {
        return null;
    }
    // modelFactory.updateModel(request, bindingContext);
    return returnValue;
}
Also used : WebAsyncManager(cn.taketoday.web.context.async.WebAsyncManager) RedirectModel(cn.taketoday.web.view.RedirectModel) BindingContext(cn.taketoday.web.BindingContext) AsyncWebRequest(cn.taketoday.web.context.async.AsyncWebRequest) Nullable(cn.taketoday.lang.Nullable)

Example 5 with WebAsyncManager

use of cn.taketoday.web.context.async.WebAsyncManager in project today-framework by TAKETODAY.

the class ResponseBodyEmitterReturnValueHandlerTests method responseBodyFluxWithError.

// gh-21972
@Test
public void responseBodyFluxWithError() throws Exception {
    this.request.addHeader("Accept", "text/event-stream");
    HandlerMethod type = on(TestController.class).resolveHandlerMethod(Flux.class, String.class);
    Sinks.Many<String> sink = Sinks.many().unicast().onBackpressureBuffer();
    this.handler.handleReturnValue(webRequest, type, sink.asFlux());
    assertThat(this.request.isAsyncStarted()).isTrue();
    IllegalStateException ex = new IllegalStateException("wah wah");
    sink.tryEmitError(ex);
    sink.tryEmitComplete();
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.webRequest);
    assertThat(asyncManager.getConcurrentResult()).isSameAs(ex);
    assertThat(this.response.getContentType()).isNull();
}
Also used : WebAsyncManager(cn.taketoday.web.context.async.WebAsyncManager) Sinks(reactor.core.publisher.Sinks) Test(org.junit.jupiter.api.Test)

Aggregations

WebAsyncManager (cn.taketoday.web.context.async.WebAsyncManager)5 Nullable (cn.taketoday.lang.Nullable)2 BindingContext (cn.taketoday.web.BindingContext)2 AsyncWebRequest (cn.taketoday.web.context.async.AsyncWebRequest)2 DeferredResult (cn.taketoday.web.context.async.DeferredResult)2 StandardServletAsyncWebRequest (cn.taketoday.web.context.async.StandardServletAsyncWebRequest)2 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)2 RedirectModel (cn.taketoday.web.view.RedirectModel)2 Test (org.junit.jupiter.api.Test)1 Sinks (reactor.core.publisher.Sinks)1