淺談一下SpringBoot中的異步任務(wù)
SpringBoot異步任務(wù)
SpringBoot 中的異步任務(wù)主要是指在 SpringBoot 中使用異步線程完成處理任務(wù)。
在 SpringBoot 中使用異步線程非常簡(jiǎn)單,只需要兩個(gè)注解就可以搞定。一個(gè)是 @EnableAsync (用于開(kāi)啟異步注解),另一個(gè)是 @Async (一般加在方法上,表示該方法異步執(zhí)行)。
1、使用自帶的線程池實(shí)現(xiàn)異步任務(wù)
第一步
在啟動(dòng)類(lèi)上加上注解 @EnableAsync ,如下所示

第二步
寫(xiě)接口,并將接口的實(shí)現(xiàn)方法,用注解 @Async 標(biāo)識(shí)。
- controller層
package com.yuhuofei.controller;
import com.yuhuofei.service.AsyncService;
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;
/**
* @Description
* @ClassName AsyncController
* @Author yuhuofei
* @Date 2022/8/22 21:51
* @Version 1.0
*/
@RestController
@RequestMapping("/async")
public class AsyncController {
@Autowired
private AsyncService asyncService;
@GetMapping("/getAsyncHello")
public String getAsyncHello(){
return asyncService.getHello();
}
}
service層接口
package com.yuhuofei.service;
/**
* @Description
* @ClassName AsyncService
* @Author yuhuofei
* @Date 2022/8/22 21:55
* @Version 1.0
*/
public interface AsyncService {
String getHello();
}
service層接口實(shí)現(xiàn)類(lèi)
package com.yuhuofei.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
/**
* @Description
* @ClassName AsyncServiceImpl
* @Author yuhuofei
* @Date 2022/8/22 21:55
* @Version 1.0
*/
@Service
public class AsyncServiceImpl implements AsyncService {
@Override
@Async
public String getHello() {
return "hello,測(cè)試一下";
}
}
至此完成異步任務(wù)的簡(jiǎn)單實(shí)現(xiàn)。
2、使用自定義的線程池實(shí)現(xiàn)異步任務(wù)
第一步
自定義一個(gè)線程池,如下所示
package com.yuhuofei.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* @author yuhuofei
* @version 1.0
* @description 自定義異步線程池
* @date 2022/8/22 21:55
*/
@Configuration
@EnableAsync
public class AsyncExecutorConfig {
private static final int CORE_POOL_SIZE = 10;
private static final int MAX_POOL_SIZE = 20;
private static final int QUEUE_CAPACITY = 2000;
private static final String THREAD_NAME_PREFIX = "AsyncExecutor-self";
private static final int KEEP_ALIVE_SECONDS = 60 * 10;
@Bean("asyncExecutor")
public Executor asyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(CORE_POOL_SIZE);
executor.setMaxPoolSize(MAX_POOL_SIZE);
executor.setQueueCapacity(QUEUE_CAPACITY);
executor.setThreadNamePrefix(THREAD_NAME_PREFIX);
executor.setAllowCoreThreadTimeOut(true);
executor.setKeepAliveSeconds(KEEP_ALIVE_SECONDS);
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
}
第二步
使用自定義線程池,如下所示

package com.yuhuofei.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
/**
* @Description
* @ClassName AsyncServiceImpl
* @Author yuhuofei
* @Date 2022/8/22 21:55
* @Version 1.0
*/
@Service
public class AsyncServiceImpl implements AsyncService {
@Override
@Async("asyncExecutor")
public String getHello() {
return "hello,測(cè)試一下";
}
}
由于自定義線程池時(shí)已經(jīng)開(kāi)啟了異步注解,因此可以不用在啟動(dòng)類(lèi)上加了,至此完成使用自定義線程池實(shí)現(xiàn)異步任務(wù)。
到此這篇關(guān)于淺談一下SpringBoot中的異步任務(wù)的文章就介紹到這了,更多相關(guān)SpringBoot異步任務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java通過(guò)注解實(shí)現(xiàn)分表詳解
這篇文章主要為大家詳細(xì)介紹了java如何通過(guò)注解實(shí)現(xiàn)分表,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考一下2024-11-11
Spring及Mybatis整合占位符解析失敗問(wèn)題解決
這篇文章主要介紹了Spring及Mybatis整合占位符解析失敗問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
WxJava微信公眾號(hào)開(kāi)發(fā)入門(mén)實(shí)戰(zhàn)
本文主要介紹了WxJava微信公眾號(hào)開(kāi)發(fā)入門(mén)實(shí)戰(zhàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
Java try()語(yǔ)句實(shí)現(xiàn)try-with-resources異常管理機(jī)制操作
這篇文章主要介紹了Java try()語(yǔ)句實(shí)現(xiàn)try-with-resources異常管理機(jī)制操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Mybatis主配置文件的properties標(biāo)簽詳解
這篇文章主要介紹了Mybatis主配置文件的properties標(biāo)簽,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
SpringBoot RESTful風(fēng)格入門(mén)講解
RESTful是一種web軟件風(fēng)格,它不是標(biāo)準(zhǔn)也不是協(xié)議,它不一定要采用,只是一種風(fēng)格,它倡導(dǎo)的是一個(gè)資源定位(url)及資源操作的風(fēng)格,這篇文章主要介紹了SpringBoot使用RESTful接口2022-11-11
springboot controller 增加指定前綴的兩種實(shí)現(xiàn)方法
這篇文章主要介紹了springboot controller 增加指定前綴的兩種實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02

