/**
* 最近在做订单模块,用户购买服务类产品之后,需要进行预约,预约成功之后分别给商家和用户发送提醒短信。
*
* 我们可以使用springBoot默认的线程池,不过一般我们会自定义线程池(因为比较灵活),配置方式有:
* 1、使用 xml 文件配置的方式
* 2、使用Java代码结合@Configuration进行配置(推荐使用,本文也是围绕这个)
*
*/
TreadPoolTaskConfig(线程池配置类):
package com.thread.pool.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.ThreadPoolExecutor;
/**
* @remark 线程池配置类
* <p>
* 默认情况下,在创建线程池后,线程池的线程数为0,当有任务来的时候,就会创建一线程去执行任务
* 当线程池中的线程数目达到核心池corePoolSize后,就会把后来到达的任务放进队列当中;
* 当队列满了,就会继续创建线程,再当线程数量大于maxPoolSize后,开始使用拒绝策略拒绝
* 而JDK自带的拒绝策略有四种:
* 直接丢弃 抛异常
* 由调用者再调用者的线程执行 丢弃组赛队列里面等待时间最长的一个线程
*/
@Configuration
@EnableAsync
public class ThreadPoolTaskConfig {
/**
* 核心线程数目(也即默认线程数)
*/
private static final int corePoolSize = 20;
/**
* 最大线程数
*/
private static final int maxPoolSize = 100;
/**
* 允许线程最大空闲时间(单位,默认秒)
*/
private static final int keepAliveTime = 10;
/**
* 缓冲队列大小
*/
private static final int queryCapacity = 200;
/**
* 线程池名前缀
*/
private static final String threadNamePrefix = "Async-Service-";
@Bean("taskExecutor")
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(corePoolSize);
taskExecutor.setMaxPoolSize(maxPoolSize);
taskExecutor.setQueueCapacity(queryCapacity);
taskExecutor.setKeepAliveSeconds(keepAliveTime);
taskExecutor.setThreadNamePrefix(threadNamePrefix);
// 线程池对拒绝任务的处理策略
// CallerRunsPolicy:由调用线程(提交任务的线程)处理该任务
taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 初始化
taskExecutor.initialize();
return taskExecutor;
}
}
OrderTaskService(调用发短信的方法):
package com.thread.pool.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @remark 调用发短信的方法
*/
@Service
public class OrderTaskService {
@Autowired
private TranTest2Service tranTest2Service;
/**
* 订单处理任务
*/
public void orderTask() throws InterruptedException {
// 取消订单
this.cancelOrder();
tranTest2Service.sendMessage1();
tranTest2Service.sendMessage2();
}
/**
* 取消订单
*/
public void cancelOrder(){
System.out.println("取消订单的方法执行----开始");
System.out.println("取消订单的方法执行----结束");
}
}
TranTest2Service(模拟取消订单后发短信):
package com.thread.pool.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
/**
* @remark 用于模拟取消订单后发短信
*/
@Service
public class TranTest2Service {
/**
* 定义输出日志
*/
Logger logger = LoggerFactory.getLogger(TranTest2Service.class);
/**
* 发送提醒短息1
*/
@PostConstruct // 加上该注解项目启动是就执行一次该方法
@Async("taskExecutor") // @Async("taskExecutor") 对应我们自定义线程池中的 @Bean("taskExecutor") ,表示使用我们自定义的线程池。
public void sendMessage1() throws InterruptedException {
logger.info("发送短信1开始执行-----");
/**
* 模拟中间耗时
*/
Thread.sleep(5000);
logger.info("发送短信1执行结束----");
}
/**
* 发送提醒短息1
*/
@PostConstruct // 加上该注解项目启动是就执行一次该方法
@Async("taskExecutor") // @Async("taskExecutor") 对应我们自定义线程池中的 @Bean("taskExecutor") ,表示使用我们自定义的线程池。
public void sendMessage2() throws InterruptedException {
logger.info("发送短信2开始执行----");
/**
* 模拟中间耗时
*/
Thread.sleep(5000);
logger.info("发送短信2执行结束----");
}
}
OrderTaskController(接口测试):
package com.thread.pool.controller;
import com.thread.pool.service.OrderTaskService;
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;
/**
* @remark
*/
@RestController
@RequestMapping("/api")
public class OrderTaskController {
@Autowired
private OrderTaskService orderTaskService;
@GetMapping("/task")
public Boolean orderTask(){
try {
orderTaskService.orderTask();
} catch (InterruptedException e) {
e.printStackTrace();
}
return true;
}
@RequestMapping("/some")
public String someHandle(){
return "hello";
}
}
输出结果: