启动类:添加@EnableAsync注解java
@SpringBootApplication @EnableAsync public class Application{ public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
service方法上添加@Async注解spring
import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @Service public class UserService { @Async public void sendSms(){ System.out.println("####sendSms#### 2"); IntStream.range(0, 5).forEach(d -> { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } }); System.out.println("####sendSms#### 3"); } }
配置框架
package com.boot.common.conf; import java.util.concurrent.ThreadPoolExecutor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; /** * 线程池配置 * @author zhh * */ @Configuration @EnableAsync public class ThreadPoolTaskConfig { private static final int corePoolSize = 10; // 核心线程数(默认线程数) private static final int maxPoolSize = 100; // 最大线程数 private static final int keepAliveTime = 10; // 容许线程空闲时间(单位:默认为秒) private static final int queueCapacity = 200; // 缓冲队列数 private static final String threadNamePrefix = "Async-Service-"; // 线程池名前缀 @Bean("taskExecutor") // bean的名称,默认为首字母小写的方法名 public ThreadPoolTaskExecutor taskExecutor(){ ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(corePoolSize); executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); executor.setKeepAliveSeconds(keepAliveTime); executor.setThreadNamePrefix(threadNamePrefix); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化 executor.initialize(); return executor; } }
测试异步
package com.boot.test1.service; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @Service public class TranTest2Service { Logger log = LoggerFactory.getLogger(TranTest2Service.class); // 发送提醒短信 1 @Async("taskExecutor") public void sendMessage1() throws InterruptedException { log.info("发送短信方法---- 1 执行开始"); Thread.sleep(5000); // 模拟耗时 log.info("发送短信方法---- 1 执行结束"); } // 发送提醒短信 2 @Async("taskExecutor") public void sendMessage2() throws InterruptedException { log.info("发送短信方法---- 2 执行开始"); Thread.sleep(2000); // 模拟耗时 log.info("发送短信方法---- 2 执行结束"); } }
以下方式会使@Async失效
1、异步方法使用static修饰
2、异步类没有使用@Component注解(或其余注解)致使spring没法扫描到异步类
3、异步方法不能与异步方法在同一个类中
4、类中须要使用@Autowired或@Resource等注解自动注入,不能本身手动new对象
5、若是使用SpringBoot框架必须在启动类中增长@EnableAsync注解
6、在Async 方法上标注@Transactional是没用的。 在Async 方法调用的方法上标注@Transactional 有效。测试