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

第二步
寫接口,并將接口的實現(xiàn)方法,用注解 @Async 標(biāo)識。
- 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層接口實現(xiàn)類
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,測試一下";
}
}
至此完成異步任務(wù)的簡單實現(xiàn)。
2、使用自定義的線程池實現(xiàn)異步任務(wù)
第一步
自定義一個線程池,如下所示
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,測試一下";
}
}
由于自定義線程池時已經(jīng)開啟了異步注解,因此可以不用在啟動類上加了,至此完成使用自定義線程池實現(xiàn)異步任務(wù)。
到此這篇關(guān)于淺談一下SpringBoot中的異步任務(wù)的文章就介紹到這了,更多相關(guān)SpringBoot異步任務(wù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
idea新建聚合項目并附上標(biāo)簽的詳細(xì)過程
這篇文章主要介紹了idea新建聚合項目并附上標(biāo)簽的詳細(xì)過程,本文通過實例圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08
SpringBoot使用阿里OSS實現(xiàn)文件云存儲的方法
這篇文章主要介紹了SpringBoot使用阿里OSS實現(xiàn)文件云存儲,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
詳解Java兩種方式簡單實現(xiàn):爬取網(wǎng)頁并且保存
本篇文章主要介紹了Java兩種方式簡單實現(xiàn):爬取網(wǎng)頁并且保存 ,主要用UrlConnection、HttpClient爬取實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下。2016-12-12
SpringBoot?Test的webEnvironment源碼解讀
這篇文章主要為大家介紹了SpringBoot?Test的webEnvironment源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
Spring Boot Gateway 從入門到精通全面指南
Spring Cloud Gateway 作為微服務(wù)架構(gòu)中的重要組件,提供了強大而靈活的路由與過濾功能,通過本教程,您應(yīng)該已經(jīng)了解了如何集成 Spring Cloud Gateway、如何配置路由規(guī)則以及如何利用其豐富的功能來滿足各種業(yè)務(wù)需求,感興趣的朋友跟隨小編一起看看吧2025-09-09

