springboot2.0以上开启异步执行和自定义线程池配置
springboot2.0 以上项目开启异步支持 以下是关键代码
package com.xsrt.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.ThreadPoolExecutor; @Configuration @EnableAsync public class AsyncConfig { @Bean("threadPoolTaskExecutor") public ThreadPoolTaskExecutor threadPoolTaskExecutor(){ ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); taskExecutor.setCorePoolSize(Runtime.getRuntime().availableProcessors()*2); taskExecutor.setMaxPoolSize(60); taskExecutor.setQueueCapacity(20000); taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); taskExecutor.setThreadGroupName("Task-"); taskExecutor.setThreadNamePrefix("Async-"); taskExecutor.setBeanName("threadPoolTaskExecutor"); return taskExecutor; } }
如果代码中需要多个线程池,可以按照如下方式配置
package com.xsrt.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.ThreadPoolExecutor; @Configuration @EnableAsync public class AsyncConfig { @Bean("threadPoolTaskExecutor") @Primary //指定当前线程池为主线程池 public ThreadPoolTaskExecutor threadPoolTaskExecutor(){ ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); taskExecutor.setCorePoolSize(Runtime.getRuntime().availableProcessors()*2); taskExecutor.setMaxPoolSize(60); taskExecutor.setQueueCapacity(2000); taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); taskExecutor.setThreadGroupName("Task-"); taskExecutor.setThreadNamePrefix("Async-"); taskExecutor.setBeanName("threadPoolTaskExecutor"); return taskExecutor; } @Bean("threadPool")//其他线程池 public ThreadPoolTaskExecutor threadPool(){ ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); taskExecutor.setCorePoolSize(Runtime.getRuntime().availableProcessors()*2); taskExecutor.setMaxPoolSize(60); taskExecutor.setQueueCapacity(200); taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); taskExecutor.setThreadGroupName("Task-"); taskExecutor.setThreadNamePrefix("Pool-"); taskExecutor.setBeanName("threadPoolTaskExecutor"); return taskExecutor; } }
异步方法调用如下
package com.ccbobe.websocket.async; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @Slf4j @Service public class AsyncService { @Async public String asyncPrimary(){ log.info("执行 Primary 异步......"); return null; } @Async("threadPool") public String asyncPools(){ log.info("执行 threadPool 异步......"); return null; } }
注意事项:使用@Async 注解时,需要注意一下几点 1 当项目中只有一个线程池时,我们只需要写 @Async 即可将需要异步的方法进行异步; 2 当项目中存在多个线程池,我们在写异步时,需要注意如果只写@Async注解没有任何属性则将此方法的执行异步到带有 @Primary 注解修饰的线程池中执行。 3 还可以将方法异步到指定的线程池中,如 @Async(“threadPool”)则是将此方法异步到threadPool 线程池中执行。
上一篇:
IDEA上Java项目控制台中文乱码