一、前言
?
異步執(zhí)行對于開發(fā)者來說并不陌生,在實(shí)際的開發(fā)過程中,很多場景多會使用到異步,相比同步執(zhí)行,異步可以大大縮短請求鏈路耗時(shí)時(shí)間,比如:「發(fā)送短信、郵件、異步更新等」 ,這些都是典型的可以通過異步實(shí)現(xiàn)的場景。
?
二、異步的八種實(shí)現(xiàn)方式
線程Thread
Future
異步框架CompletableFuture
Spring注解@Async
Spring ApplicationEvent事件
消息隊(duì)列
第三方異步框架,比如Hutool的ThreadUtil
Guava異步
三、什么是異步?
首先我們先看一個(gè)常見的用戶下單的場景:
什么是異步
在同步操作中,我們執(zhí)行到 「發(fā)送短信」 的時(shí)候,我們必須等待這個(gè)方法徹底執(zhí)行完才能執(zhí)行 「贈送積分」 這個(gè)操作,如果 「贈送積分」 這個(gè)動作執(zhí)行時(shí)間較長,發(fā)送短信需要等待,這就是典型的同步場景。
實(shí)際上,發(fā)送短信和贈送積分沒有任何的依賴關(guān)系,通過異步,我們可以實(shí)現(xiàn)贈送積分和發(fā)送短信這兩個(gè)操作能夠同時(shí)進(jìn)行,比如:
異步
這就是所謂的異步,是不是非常簡單,下面就說說異步的幾種實(shí)現(xiàn)方式吧。
四、異步編程
4.1 線程異步
public?class?AsyncThread?extends?Thread?{ ????@Override ????public?void?run()?{ ????????System.out.println("Current?thread?name:"?+?Thread.currentThread().getName()?+?"?Send?email?success!"); ????} ????public?static?void?main(String[]?args)?{ ????????AsyncThread?asyncThread?=?new?AsyncThread(); ????????asyncThread.run(); ????} }
當(dāng)然如果每次都創(chuàng)建一個(gè)Thread線程,頻繁的創(chuàng)建、銷毀,浪費(fèi)系統(tǒng)資源,我們可以采用線程池:
private?ExecutorService?executorService?=?Executors.newCachedThreadPool(); public?void?fun()?{ ????executorService.submit(new?Runnable()?{ ????????@Override ????????public?void?run()?{ ????????????log.info("執(zhí)行業(yè)務(wù)邏輯..."); ????????} ????}); }
可以將業(yè)務(wù)邏輯封裝到Runnable或Callable中,交由線程池來執(zhí)行。
4.2 Future異步
@Slf4j public?class?FutureManager?{ ????public?String?execute()?throws?Exception?{ ????????ExecutorService?executor?=?Executors.newFixedThreadPool(1); ????????Future?future?=?executor.submit(new?Callable ()?{ ????????????@Override ????????????public?String?call()?throws?Exception?{ ????????????????System.out.println("?---?task?start?---?"); ????????????????Thread.sleep(3000); ????????????????System.out.println("?---?task?finish?---"); ????????????????return?"this?is?future?execute?final?result!!!"; ????????????} ????????}); ????????//這里需要返回值時(shí)會阻塞主線程 ????????String?result?=?future.get(); ????????log.info("Future?get?result:?{}",?result); ????????return?result; ????} ????@SneakyThrows ????public?static?void?main(String[]?args)?{ ????????FutureManager?manager?=?new?FutureManager(); ????????manager.execute(); ????} }
輸出結(jié)果:
?---?task?start?---? ?---?task?finish?--- ?Future?get?result:?this?is?future?execute?final?result!!!
4.2.1 Future的不足之處
Future的不足之處的包括以下幾點(diǎn):
無法被動接收異步任務(wù)的計(jì)算結(jié)果:雖然我們可以主動將異步任務(wù)提交給線程池中的線程來執(zhí)行,但是待異步任務(wù)執(zhí)行結(jié)束之后,主線程無法得到任務(wù)完成與否的通知,它需要通過get方法主動獲取任務(wù)執(zhí)行的結(jié)果。 Future件彼此孤立:有時(shí)某一個(gè)耗時(shí)很長的異步任務(wù)執(zhí)行結(jié)束之后,你想利用它返回的結(jié)果再做進(jìn)一步的運(yùn)算,該運(yùn)算也會是一個(gè)異步任務(wù),兩者之間的關(guān)系需要程序開發(fā)人員手動進(jìn)行綁定賦予,F(xiàn)uture并不能將其形成一個(gè)任務(wù)流(pipeline),每一個(gè)Future都是彼此之間都是孤立的,所以才有了后面的CompletableFuture,CompletableFuture就可以將多個(gè)Future串聯(lián)起來形成任務(wù)流。 Futrue沒有很好的錯(cuò)誤處理機(jī)制:截止目前,如果某個(gè)異步任務(wù)在執(zhí)行發(fā)的過程中發(fā)生了異常,調(diào)用者無法被動感知,必須通過捕獲get方法的異常才知曉異步任務(wù)執(zhí)行是否出現(xiàn)了錯(cuò)誤,從而在做進(jìn)一步的判斷處理。
4.3 CompletableFuture實(shí)現(xiàn)異步
public?class?CompletableFutureCompose?{ ????/** ?????*?thenAccept子任務(wù)和父任務(wù)公用同一個(gè)線程 ?????*/ ????@SneakyThrows ????public?static?void?thenRunAsync()?{ ????????CompletableFuture?cf1?=?CompletableFuture.supplyAsync(()?->?{ ????????????System.out.println(Thread.currentThread()?+?"?cf1?do?something...."); ????????????return?1; ????????}); ????????CompletableFuture ?cf2?=?cf1.thenRunAsync(()?->?{ ????????????System.out.println(Thread.currentThread()?+?"?cf2?do?something..."); ????????}); ????????//等待任務(wù)1執(zhí)行完成 ????????System.out.println("cf1結(jié)果->"?+?cf1.get()); ????????//等待任務(wù)2執(zhí)行完成 ????????System.out.println("cf2結(jié)果->"?+?cf2.get()); ????} ????public?static?void?main(String[]?args)?{ ????????thenRunAsync(); ????} }
我們不需要顯式使用ExecutorService,CompletableFuture 內(nèi)部使用了ForkJoinPool來處理異步任務(wù),如果在某些業(yè)務(wù)場景我們想自定義自己的異步線程池也是可以的。
4.4 Spring的@Async異步
4.4.1 自定義異步線程池
/** ?*?線程池參數(shù)配置,多個(gè)線程池實(shí)現(xiàn)線程池隔離,@Async注解,默認(rèn)使用系統(tǒng)自定義線程池,可在項(xiàng)目中設(shè)置多個(gè)線程池,在異步調(diào)用的時(shí)候,指明需要調(diào)用的線程池名稱,比如:@Async("taskName") @EnableAsync @Configuration public?class?TaskPoolConfig?{ ????/** ?????*?自定義線程池 ?????* ?????**/ ????@Bean("taskExecutor") ????public?Executor?taskExecutor()?{ ????????//返回可用處理器的Java虛擬機(jī)的數(shù)量?12 ????????int?i?=?Runtime.getRuntime().availableProcessors(); ????????System.out.println("系統(tǒng)最大線程數(shù)??:?"?+?i); ????????ThreadPoolTaskExecutor?executor?=?new?ThreadPoolTaskExecutor(); ????????//核心線程池大小 ????????executor.setCorePoolSize(16); ????????//最大線程數(shù) ????????executor.setMaxPoolSize(20); ????????//配置隊(duì)列容量,默認(rèn)值為Integer.MAX_VALUE ????????executor.setQueueCapacity(99999); ????????//活躍時(shí)間 ????????executor.setKeepAliveSeconds(60); ????????//線程名字前綴 ????????executor.setThreadNamePrefix("asyncServiceExecutor?-"); ????????//設(shè)置此執(zhí)行程序應(yīng)該在關(guān)閉時(shí)阻止的最大秒數(shù),以便在容器的其余部分繼續(xù)關(guān)閉之前等待剩余的任務(wù)完成他們的執(zhí)行 ????????executor.setAwaitTerminationSeconds(60); ????????//等待所有的任務(wù)結(jié)束后再關(guān)閉線程池 ????????executor.setWaitForTasksToCompleteOnShutdown(true); ????????return?executor; ????} }
4.4.2 AsyncService
public?interface?AsyncService?{ ????MessageResult?sendSms(String?callPrefix,?String?mobile,?String?actionType,?String?content); ????MessageResult?sendEmail(String?email,?String?subject,?String?content); } @Slf4j @Service public?class?AsyncServiceImpl?implements?AsyncService?{ ????@Autowired ????private?IMessageHandler?mesageHandler; ????@Override ????@Async("taskExecutor") ????public?MessageResult?sendSms(String?callPrefix,?String?mobile,?String?actionType,?String?content)?{ ????????try?{ ????????????Thread.sleep(1000); ????????????mesageHandler.sendSms(callPrefix,?mobile,?actionType,?content); ????????}?catch?(Exception?e)?{ ????????????log.error("發(fā)送短信異常?->?",?e) ????????} ????} ???? ????@Override ????@Async("taskExecutor") ????public?sendEmail(String?email,?String?subject,?String?content)?{ ????????try?{ ????????????Thread.sleep(1000); ????????????mesageHandler.sendsendEmail(email,?subject,?content); ????????}?catch?(Exception?e)?{ ????????????log.error("發(fā)送email異常?->?",?e) ????????} ????} }
在實(shí)際項(xiàng)目中, 使用@Async調(diào)用線程池,推薦等方式是是使用自定義線程池的模式,不推薦直接使用@Async直接實(shí)現(xiàn)異步。
4.5 Spring ApplicationEvent事件實(shí)現(xiàn)異步
4.5.1 定義事件
public?class?AsyncSendEmailEvent?extends?ApplicationEvent?{ ????/** ?????*?郵箱 ?????**/ ????private?String?email; ???/** ?????*?主題 ?????**/ ????private?String?subject; ????/** ?????*?內(nèi)容 ?????**/ ????private?String?content; ?? ????/** ?????*?接收者 ?????**/ ????private?String?targetUserId; }
4.5.2 定義事件處理器
@Slf4j @Component public?class?AsyncSendEmailEventHandler?implements?ApplicationListener?{ ????@Autowired ????private?IMessageHandler?mesageHandler; ???? ????@Async("taskExecutor") ????@Override ????public?void?onApplicationEvent(AsyncSendEmailEvent?event)?{ ????????if?(event?==?null)?{ ????????????return; ????????} ????????String?email?=?event.getEmail(); ????????String?subject?=?event.getSubject(); ????????String?content?=?event.getContent(); ????????String?targetUserId?=?event.getTargetUserId(); ????????mesageHandler.sendsendEmailSms(email,?subject,?content,?targerUserId); ??????} }
另外,可能有些時(shí)候采用ApplicationEvent實(shí)現(xiàn)異步的使用,當(dāng)程序出現(xiàn)異常錯(cuò)誤的時(shí)候,需要考慮補(bǔ)償機(jī)制,那么這時(shí)候可以結(jié)合Spring Retry重試來幫助我們避免這種異常造成數(shù)據(jù)不一致問題。
4.6 消息隊(duì)列
4.6.1 回調(diào)事件消息生產(chǎn)者
@Slf4j @Component public?class?CallbackProducer?{ ????@Autowired ????AmqpTemplate?amqpTemplate; ????public?void?sendCallbackMessage(CallbackDTO?allbackDTO,?final?long?delayTimes)?{ ????????log.info("生產(chǎn)者發(fā)送消息,callbackDTO,{}",?callbackDTO); ????????amqpTemplate.convertAndSend(CallbackQueueEnum.QUEUE_GENSEE_CALLBACK.getExchange(),?CallbackQueueEnum.QUEUE_GENSEE_CALLBACK.getRoutingKey(),?JsonMapper.getInstance().toJson(genseeCallbackDTO),?new?MessagePostProcessor()?{ ????????????@Override ????????????public?Message?postProcessMessage(Message?message)?throws?AmqpException?{ ????????????????//給消息設(shè)置延遲毫秒值,通過給消息設(shè)置x-delay頭來設(shè)置消息從交換機(jī)發(fā)送到隊(duì)列的延遲時(shí)間 ????????????????message.getMessageProperties().setHeader("x-delay",?delayTimes); ????????????????message.getMessageProperties().setCorrelationId(callbackDTO.getSdkId()); ????????????????return?message; ????????????} ????????}); ????} }
4.6.2 回調(diào)事件消息消費(fèi)者
@Slf4j @Component @RabbitListener(queues?=?"message.callback",?containerFactory?=?"rabbitListenerContainerFactory") public?class?CallbackConsumer?{ ????@Autowired ????private?IGlobalUserService?globalUserService; ????@RabbitHandler ????public?void?handle(String?json,?Channel?channel,?@Headers?Map?map)?throws?Exception?{ ????????if?(map.get("error")?!=?null)?{ ????????????//否認(rèn)消息 ????????????channel.basicNack((Long)?map.get(AmqpHeaders.DELIVERY_TAG),?false,?true); ????????????return; ????????} ????????try?{ ???????? ????????????CallbackDTO?callbackDTO?=?JsonMapper.getInstance().fromJson(json,?CallbackDTO.class); ????????????//執(zhí)行業(yè)務(wù)邏輯 ????????????globalUserService.execute(callbackDTO); ????????????//消息消息成功手動確認(rèn),對應(yīng)消息確認(rèn)模式acknowledge-mode:?manual ????????????channel.basicAck((Long)?map.get(AmqpHeaders.DELIVERY_TAG),?false); ????????}?catch?(Exception?e)?{ ????????????log.error("回調(diào)失敗?->?{}",?e); ????????} ????} }
4.7 ThreadUtil異步工具類
@Slf4j public?class?ThreadUtils?{ ????public?static?void?main(String[]?args)?{ ????????for?(int?i?=?0;?i?3;?i++)?{ ????????????ThreadUtil.execAsync(()?->?{ ????????????????ThreadLocalRandom?threadLocalRandom?=?ThreadLocalRandom.current(); ????????????????int?number?=?threadLocalRandom.nextInt(20)?+?1; ????????????????System.out.println(number); ????????????}); ????????????log.info("當(dāng)前第:"?+?i?+?"個(gè)線程"); ????????} ????????log.info("task?finish!"); ????} }
4.8 Guava異步
Guava的ListenableFuture顧名思義就是可以監(jiān)聽的Future,是對java原生Future的擴(kuò)展增強(qiáng)。我們知道Future表示一個(gè)異步計(jì)算任務(wù),當(dāng)任務(wù)完成時(shí)可以得到計(jì)算結(jié)果。如果我們希望一旦計(jì)算完成就拿到結(jié)果展示給用戶或者做另外的計(jì)算,就必須使用另一個(gè)線程不斷的查詢計(jì)算狀態(tài)。這樣做,代碼復(fù)雜,而且效率低下。使用「Guava ListenableFuture」 可以幫我們檢測Future是否完成了,不需要再通過get()方法苦苦等待異步的計(jì)算結(jié)果,如果完成就自動調(diào)用回調(diào)函數(shù),這樣可以減少并發(fā)程序的復(fù)雜度。
ListenableFuture是一個(gè)接口,它從jdk的Future接口繼承,添加了void addListener(Runnable listener, Executor executor)方法。
我們看下如何使用ListenableFuture。首先需要定義ListenableFuture的實(shí)例:
?ListeningExecutorService?executorService?=?MoreExecutors.listeningDecorator(Executors.newCachedThreadPool()); ????????final?ListenableFuture?listenableFuture?=?executorService.submit(new?Callable ()?{ ????????????@Override ????????????public?Integer?call()?throws?Exception?{ ????????????????log.info("callable?execute...") ????????????????TimeUnit.SECONDS.sleep(1); ????????????????return?1; ????????????} ????????});
首先通過MoreExecutors類的靜態(tài)方法listeningDecorator方法初始化一個(gè)ListeningExecutorService的方法,然后使用此實(shí)例的submit方法即可初始化ListenableFuture對象。
ListenableFuture要做的工作,在Callable接口的實(shí)現(xiàn)類中定義,這里只是休眠了1秒鐘然后返回一個(gè)數(shù)字1,有了ListenableFuture實(shí)例,可以執(zhí)行此Future并執(zhí)行Future完成之后的回調(diào)函數(shù)。
?Futures.addCallback(listenableFuture,?new?FutureCallback()?{ ????@Override ????public?void?onSuccess(Integer?result)?{ ????????//成功執(zhí)行... ????????System.out.println("Get?listenable?future's?result?with?callback?"?+?result); ????} ????@Override ????public?void?onFailure(Throwable?t)?{ ????????//異常情況處理... ????????t.printStackTrace(); ????} });
那么,以上就是本期介紹的實(shí)現(xiàn)異步的8種方式了。
審核編輯:黃飛
?
評論
查看更多