解決SpringBoot中使用@Async注解失效的問題
錯(cuò)誤示例,同一個(gè)類中使用異步方法:
package com.xqnode.learning.controller;import com.fasterxml.jackson.core.JsonProcessingException;import org.springframework.scheduling.annotation.Async;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;import java.util.Map;import java.util.concurrent.TimeUnit;@RestController@RequestMapping('/test')public class TestController { @GetMapping('/async') public Map<Object, Object> test() throws InterruptedException, JsonProcessingException { System.out.println('接收到請(qǐng)求。。。'); task(); Map<Object, Object> map = new HashMap<>(); System.out.println('請(qǐng)求結(jié)束。。。'); map.put('msg', '操作成功'); map.put('code', '0'); return map; } @Async void task() throws InterruptedException { TimeUnit.SECONDS.sleep(5); System.out.println('線程任務(wù)執(zhí)行結(jié)束。。。'); }}
來分析下錯(cuò)誤的原因:
當(dāng)我使用postman調(diào)用該接口的時(shí)候發(fā)現(xiàn),我預(yù)想中的立即返回并沒有發(fā)生。后來查詢資料發(fā)現(xiàn),在controller調(diào)用本類中的異步方法,不會(huì)生效
正確示例:
package com.xqnode.learning.controller;import com.xqnode.learning.service.AsyncService;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;import java.util.Map;@RestController@RequestMapping('/test')public class TestController { private static final Logger LOG = LoggerFactory.getLogger(TestController.class); @Autowired private AsyncService asyncService; @GetMapping('/async') public Map<Object, Object> test() throws InterruptedException { LOG.info('接收到請(qǐng)求。。。'); asyncService.task(); Map<Object, Object> map = new HashMap<>(); LOG.info('請(qǐng)求結(jié)束。。。'); map.put('msg', '操作成功'); map.put('code', '0'); return map; }}
將異步調(diào)用放到service中:
package com.xqnode.learning.service;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.scheduling.annotation.Async;import org.springframework.stereotype.Service;import java.util.concurrent.TimeUnit;@Servicepublic class AsyncService { private static final Logger LOG = LoggerFactory.getLogger(AsyncService.class); @Async('asyncExecutor') public void task() throws InterruptedException { TimeUnit.SECONDS.sleep(5); LOG.info('線程任務(wù)執(zhí)行結(jié)束。。。'); }}
這時(shí)候再重啟項(xiàng)目請(qǐng)求該接口,就會(huì)發(fā)現(xiàn)接口可以立即返回值。而task任務(wù)是異步執(zhí)行的,5秒之后才會(huì)打印結(jié)果:
補(bǔ)充知識(shí):springboot + @Async 實(shí)現(xiàn)異步方法之踩坑填坑
在使用@Async注解實(shí)現(xiàn)異步方法的時(shí)候,我們項(xiàng)目中實(shí)現(xiàn)了AsyncConfigurer接口來自定義線程池和異常處理。其中自定義線程池的代碼如上圖,異常處理項(xiàng)目中是自定義異常類實(shí)現(xiàn)AsyncUncaughtExceptionHandler 接口,這里未貼出代碼。那么到底踩了什么坑呢?
第一:
加了@Async注解的方法調(diào)不起來。由于我不是項(xiàng)目及代碼的原創(chuàng),花了很多時(shí)間才分析出問題。經(jīng)過仔細(xì)分析,由于業(yè)務(wù)場(chǎng)景需要,我們?cè)陧?xiàng)目啟動(dòng)的時(shí)候就創(chuàng)建了3個(gè)線程(也就是創(chuàng)建了3個(gè)任務(wù))這正好占滿了核心線程數(shù)(3個(gè)),當(dāng)調(diào)到@Async的方法時(shí),相當(dāng)于有新的任務(wù)進(jìn)來,結(jié)合線程池的原理可知,新的任務(wù)都會(huì)放到阻塞隊(duì)列里面去,直到阻塞隊(duì)列滿了才可能會(huì)創(chuàng)建新的線程來執(zhí)行任務(wù)或者執(zhí)行飽和策略(這與Runtime.getRuntime().availableProcessors()能獲取到的線程數(shù)有關(guān)),所以造成了異步方法調(diào)不起來的假象。
解決方法:根據(jù)實(shí)際情況將線程池的核心線程數(shù)和最大線程數(shù)調(diào)整到合適的值。
第二:
在解決了上一個(gè)問題后,又發(fā)現(xiàn)了新的問題,后面某段代碼看不到執(zhí)行的日志,也不知道錯(cuò)在哪里,沒有錯(cuò)誤日志。由于之前項(xiàng)目的日志并不詳細(xì),在逐步完善各處日志后,終于發(fā)現(xiàn)問題,報(bào)出了某個(gè)實(shí)體類沒有默認(rèn)構(gòu)造函數(shù)的錯(cuò)誤。在@Async異步方法中有一個(gè)把json轉(zhuǎn)成實(shí)體bean的操作,由于這個(gè)bean創(chuàng)建了有參構(gòu)造函數(shù),卻沒有默認(rèn)的無參構(gòu)造函數(shù),所以拋異常了,正好被getAsyncUncaughtExceptionHandler()捕捉到,由于當(dāng)時(shí)此方法沒有日志,也是花了點(diǎn)時(shí)間的。
解決方法:完善日志,給實(shí)體類加上無參構(gòu)造函數(shù)。
通過這兩個(gè)問題,自己也總結(jié)了一下,在使用線程池相關(guān)的知識(shí)時(shí),一定要先了解原理,不然可能給自己挖坑,還有就是平常寫代碼時(shí)要養(yǎng)成寫注釋和打日志的習(xí)慣,否則出問題時(shí)可能要多花很久的時(shí)間找問題。
以上這篇解決SpringBoot中使用@Async注解失效的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. UDDI FAQs2. python 批量下載bilibili視頻的gui程序3. 詳解CSS偽元素的妙用單標(biāo)簽之美4. python numpy庫np.percentile用法說明5. PHP 面向?qū)ο蟪绦蛟O(shè)計(jì)之類屬性與類常量實(shí)現(xiàn)方法分析6. HTML <!DOCTYPE> 標(biāo)簽7. java實(shí)現(xiàn)2048小游戲(含注釋)8. CSS自定義滾動(dòng)條樣式案例詳解9. Java Spring WEB應(yīng)用實(shí)例化如何實(shí)現(xiàn)10. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法
