摘要:注意這時(shí)中結(jié)果為,即將原來(lái)的替換成,所以直接返回線程池執(zhí)行的結(jié)果。提醒中的是你業(yè)務(wù)代碼執(zhí)行的步驟。所以異步異常處理和同步相同,在這段請(qǐng)求中處理。比多,調(diào)用時(shí)間即發(fā)起之前。正是請(qǐng)求被異步化,從而使得能,即。
用法
@GetMapping("/ffffd") public Callableprocess() { return () -> { Thread.sleep(1000L); return "call"; }; } @GetMapping("/ffffd") public DeferredResult quotes() { DeferredResult deferredResult = new DeferredResult (); return deferredResult; } // In some other thread... deferredResult.setResult(data);
返回Callable和DeferredResult區(qū)別在于the return value of DeferredResult will also be produced from any thread, i.e. one that is not managed by Spring MVC.
運(yùn)行時(shí)客戶(hù)端請(qǐng)求,DispatcherType:REQUEST 即與同步請(qǐng)求一樣
Controller返回Callable
HandlerMethodReturnValueHandlerComposite選出CallableMethodReturnValueHandler來(lái)處理Callable
WebAsyncManager#startCallableProcessing -> #startAsyncProcessing -> StandardServletAsyncWebRequest#startAsync
由于spring的StandardServletAsyncWebRequest內(nèi)部托管J2EE容器的Request(該Request實(shí)現(xiàn)ServletRequest),所以調(diào)用ServletRequest#startAsync(ServletRequest, ServletResponse)生成AsyncContext托管于StandardServletAsyncWebRequest內(nèi)部,該AsyncContext保存ServletRequest和ServletResponse,以便之后的DispatcherType:ASYNC請(qǐng)求能取出對(duì)應(yīng)的ServletResponse
WebAsyncManager#startCallableProcessing 調(diào)用完畢后,向taskExecutor線程池中提交任務(wù)來(lái)執(zhí)行Callable#call,注意該任務(wù)最后必定會(huì)執(zhí)行WebAsyncManager#setConcurrentResultAndDispatch,即執(zhí)行StandardServletAsyncWebRequest#dispatch,就是執(zhí)行前面生成的AsyncContext#dispatch
DispatcherType:ASYNC 這是由AsyncContext#dispatch發(fā)起的。
注意這時(shí)RequestMappingHandlerAdapter#invokeHandlerMethod(HttpServletRequest, HttpServletResponse, HandlerMethod)中if (asyncManager.hasConcurrentResult())結(jié)果為true,即將原來(lái)的ServletInvocableHandlerMethod替換成ConcurrentResultHandlerMethod,所以invocableMethod.invokeAndHandle(webRequest, mavContainer);直接返回taskExecutor線程池執(zhí)行的結(jié)果。
HandlerMethodReturnValueHandlerComposite選出你結(jié)果類(lèi)型對(duì)應(yīng)的ReturnValueHandler來(lái)處理你的結(jié)果。
返回客戶(hù)端。
提醒:RequestMappingHandlerAdapter#invokeHandlerMethod(HttpServletRequest, HttpServletResponse, HandlerMethod)中的invocableMethod.invokeAndHandle(webRequest, mavContainer);是你業(yè)務(wù)代碼執(zhí)行的步驟。
The call to request.startAsync() returns AsyncContext which can be used for further control over async processing. For example it provides the method dispatch, that is similar to a forward from the Servlet API except it allows an application to resume request processing on a Servlet container thread.
運(yùn)行時(shí)流程有點(diǎn)繞,沒(méi)功夫畫(huà)時(shí)序圖。
Exception Handlingwhen a Callable raises an Exception Spring MVC dispatches to the Servlet container with the Exception as the result and that leads to resume request processing with the Exception instead of a controller method return value. When using a DeferredResult you have a choice whether to call setResult or setErrorResult with an Exception instance.
所以異步異常處理和同步相同,在DispatcherType:ASYNC這段請(qǐng)求中處理。
The DeferredResult type also provides methods such as onTimeout(Runnable) and onCompletion(Runnable).
When using a Callable you can wrap it with an instance of WebAsyncTask which also provides registration methods for timeout and completion.
AsyncHandlerInterceptor比HandlerInterceptor多#afterConcurrentHandlingStarted,調(diào)用時(shí)間即DispatcherType:ASYNC發(fā)起之前。
HTTP Streaming正是請(qǐng)求被異步化,從而使得能long polling,即HTTP Streaming。
可以用ResponseBodyEmitter來(lái)創(chuàng)建邏輯上的Streaming,注意ResponseBodyEmitter#send的內(nèi)容都會(huì)通過(guò)對(duì)應(yīng)的HttpMessageConverter來(lái)轉(zhuǎn)化:
@RequestMapping("/events") public ResponseBodyEmitter handle() { ResponseBodyEmitter emitter = new ResponseBodyEmitter(); // Save the emitter somewhere.. return emitter; } // In some other thread emitter.send("Hello once"); // and again later on emitter.send("Hello again"); // and done at some point emitter.complete();
當(dāng)然 ResponseBodyEmitter can also be used as the body in a ResponseEntity in order to customize the status and headers of the response.
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/71000.html
摘要:概念響應(yīng)式編程,異步非阻塞就是響應(yīng)式編程,與之相對(duì)應(yīng)的是命令式編程。的另外一種實(shí)現(xiàn)方式就是消息隊(duì)列。非阻塞設(shè)計(jì)利用規(guī)范中的實(shí)現(xiàn)實(shí)現(xiàn)代碼鏈接 注: 本文是由讀者觀看小馬哥公開(kāi)課視頻過(guò)程中的筆記整理而成。更多Spring Framework文章可參看筆者個(gè)人github: spring-framework-lesson 。 0. 編程模型與并發(fā)模型 Spring 5實(shí)現(xiàn)了一部分Reacti...
摘要:異步處理簡(jiǎn)介地址相關(guān)系列文章異步處理詳解分析本文講到的所有特性皆是基于的,不是基于的。用于異步返回結(jié)果,使用自己的,使用負(fù)責(zé)處理它。配置執(zhí)行異步操作需要用到,這個(gè)可以在用方法來(lái)提供相關(guān)文檔。 Spring MVC異步處理簡(jiǎn)介 Github地址 相關(guān)系列文章: Servlet 3.0 異步處理詳解 Servlet 3.1 Async IO分析 本文講到的所有特性皆是基于Servlet...
摘要:這是我重新復(fù)習(xí)的原因放棄了之前自己實(shí)現(xiàn)的全面擁抱的這個(gè)改動(dòng)是非常大的而且閱讀的源碼可以發(fā)現(xiàn)其中大部分函數(shù)都支持了類(lèi)型檢驗(yàn)和返回值提示值得閱讀 廢話(huà)不多說(shuō),直接上代碼 __auth__ = aleimu __doc__ = 學(xué)習(xí)tornado6.0+ 版本與python3.7+ import time import asyncio import tornado.gen import t...
摘要:是一個(gè)倡議,它提倡提供一種帶有非阻塞背壓的異步流處理的標(biāo)準(zhǔn)。是標(biāo)準(zhǔn)的實(shí)現(xiàn)之一。的實(shí)現(xiàn)細(xì)節(jié)請(qǐng)求響應(yīng)的與請(qǐng)求響應(yīng)的暴露為是請(qǐng)求的的消費(fèi)者是響應(yīng)的的生產(chǎn)者內(nèi)部的內(nèi)部 北京時(shí)間 9 月 26 日,Oracle 官方宣布 Java 11 正式發(fā)布 一、JDK HTTP Client介紹 JDK11中的17個(gè)新特性 showImg(https://segmentfault.com/img/remo...
摘要:原文鏈接編程方法論響應(yīng)式與代碼設(shè)計(jì)實(shí)戰(zhàn)序,來(lái)自于微信公眾號(hào)次靈均閣正文內(nèi)容在一月的架構(gòu)和設(shè)計(jì)趨勢(shì)報(bào)告中,響應(yīng)式編程和函數(shù)式仍舊編列在第一季度的早期采納者中。 原文鏈接:《Java編程方法論:響應(yīng)式RxJava與代碼設(shè)計(jì)實(shí)戰(zhàn)》序,來(lái)自于微信公眾號(hào):次靈均閣 正文內(nèi)容 在《2019 一月的InfoQ 架構(gòu)和設(shè)計(jì)趨勢(shì)報(bào)告》1中,響應(yīng)式編程(Reactive Programming)和函數(shù)式...
閱讀 859·2021-09-07 09:58
閱讀 2718·2021-08-31 09:42
閱讀 2886·2019-08-30 14:18
閱讀 3106·2019-08-30 14:08
閱讀 1856·2019-08-30 12:57
閱讀 2781·2019-08-26 13:31
閱讀 1326·2019-08-26 11:58
閱讀 1087·2019-08-23 18:06