SpringBoot配置异步线程池

1.创建配置类

/**
 * 线程池配置
 * @author Administrator
 *
 */
@Configuration
@ComponentScan("com.bootdo.workflow.service")//指定扫描任务执行类
//开始异步支持
@EnableAsync
public class AsyncConfig implements AsyncConfigurer{
	@Override
    public Executor getAsyncExecutor() {
    //线程池
   ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
   taskExecutor.setCorePoolSize(5);
   taskExecutor.setMaxPoolSize(10);
   taskExecutor.setQueueCapacity(25);
   taskExecutor.initialize();
   return taskExecutor;
}

@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
    return null;
}}

2.编写线程执行类

@Service
public class AsyncService {

	private static final Logger logger = LoggerFactory.getLogger(AsyncService.class);
	
	 	@Async
	    public void sendMsgToAll(WfDetailDO tempDetail){
		//需要执行的代码
	    }
}
3.启动类添加注解:@EnableAsync

猜你喜欢

转载自blog.csdn.net/qq_41396619/article/details/81005084