SpringBoot 中的异步任务主要是指在 SpringBoot 中使用异步线程完成处理任务。
在 SpringBoot 中使用异步线程非常简单,只需要两个注解就可以搞定。一个是 @EnableAsync (用于开启异步注解),另一个是 @Async (一般加在方法上,表示该方法异步执行)。
1、使用自带的线程池实现异步任务
第一步
在启动类上加上注解 @EnableAsync ,如下所示
第二步
写接口,并将接口的实现方法,用注解 @Async 标识。
- 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层接口实现类
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,测试一下";
}
}
至此完成异步任务的简单实现。
2、使用自定义的线程池实现异步任务
第一步
自定义一个线程池,如下所示
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,测试一下";
}
}
由于自定义线程池时已经开启了异步注解,因此可以不用在启动类上加了,至此完成使用自定义线程池实现异步任务。