Spring boot 异步线程池
package com.loan.msg.config; import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.AsyncConfigurer; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.Executor; /** * @author 周志伟 * @projectname 项目名称: ${project_name} * @classname: TaskExecutorConfig * @description: * @date 2018/11/29:13:10 */ @Configuration @ComponentScan("com.loan.msg.service") @EnableAsync //开启异步任务支持 public class TaskExecutorConfig implements AsyncConfigurer { @Override public Executor getAsyncExecutor() {//实现AsyncConfigurer接口并重写getAsyncExecutor方法,并返回一个ThreadPoolTaskExecutor,这样我们就获得了一个基于线程池TaskExecutor ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); taskExecutor.setCorePoolSize(10); taskExecutor.setMaxPoolSize(80); taskExecutor.setQueueCapacity(100); taskExecutor.initialize(); return taskExecutor; } @Override public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { return null; } }
package com.loan.msg.service; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; /** * @author 周志伟 * @projectname 项目名称: ${project_name} * @classname: AsService * @description: * @date 2018/11/21:14:27 */ @Service public class AsService { @Async public String sayHello() throws InterruptedException { System.out.println("执行任务一"); //模拟执行任务一花费的时间 Thread.sleep(3000); System.out.println("执行任务二"); return "ok"; } }