ThreadPoolTaskConfig
package com.zygh.smsbackend.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 org.springframework.scheduling.annotation.EnableScheduling;
import java.util.concurrent.ThreadPoolExecutor;
@EnableScheduling
@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 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);
// 线程池对拒绝任务的处理策略
// CallerRunsPolicy:由调用线程(提交任务的线程)处理该任务
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 初始化
executor.initialize();
return executor;
}
}
第一种情况
/**
* @Description 短信发送信息落库
* @Author wpz
* @Date 2023/5/26 9:28
*/
@Component
@Slf4j
public class DataBaseService {
private final static String SENDING = "2";
@Resource
private SMSMapper smsMapper;
@Resource
private ReceiveUserMapper receiveUserMapper;
@Resource
private ExternalPersonnelMapper externalPersonnelMapper;
@Resource
private MessageDetailsMapper messageDetailsMapper;
@Resource
MessageDetailsViewMapper messageDetailsViewMapper;
/**
* 新短信内容落库
*
* @param sendSMSNewVO 短信信息
* @param idList id集合
* @param phones 手机号集合
* @param id 短信条数的主键
*/
@Async("taskExecutor")
public void sendMSMDataBaseNew(SendSMSNewVO sendSMSNewVO, List<Integer> idList, List<String> phones, Integer id) {
log.info("新短信数据落库开始:{}", DateUtil.now());
long start = System.currentTimeMillis();
//id集合
List<ReceiveUser> receiveUsersByids = new ArrayList<>();
if (Objects.nonNull(idList) && !idList.isEmpty()) {
receiveUsersByids = idsRegister(sendSMSNewVO, idList, id);
}
//手机号集合
if (Objects.nonNull(phones) && !phones.isEmpty()) {
phonesRegister(sendSMSNewVO, phones, id, receiveUsersByids);
}
log.info("新短信数据落库结束:{},耗时:{}", DateUtil.now(), System.currentTimeMillis() - start);
}
}
@Slf4j
@Service
public class SendMSMService {
@Resource
private DataBaseService dataBaseService;
/**
* 发送短信新接口
*
* @param sendSMSNewVO 发送短信所需信息
* @return 操作详情
*/
public Result sendMSMDataBaseNew(SendSMSNewVO sendSMSNewVO) {
//发送
dataBaseService.sendMSMDataBaseNew(sendSMSNewVO, ids, phones, textMessage.getId());
return Result.success("操作成功!");
}
}
第二种情况(@Resource 注解注入)
package com.zygh.smsbackend.cron;
import cn.hutool.core.util.NumberUtil;
import com.zygh.smsbackend.domain.TextMessageCount;
import com.zygh.smsbackend.mapper.SMSMapper;
import com.zygh.smsbackend.mapper.TextMessageCountMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
/**
* @Description 发送短信统计任务
* @Author wpz
* @Date 2024/08/16 14:36
*/
@Slf4j
@Component
public class TextMessageCountTask {
@Resource
private SMSMapper smsMapper;
@Resource
private TextMessageCountMapper textMessageCountMapper;
@Resource
private ThreadPoolTaskExecutor taskExecutor;
@Async
public void oneTask() {
LocalDate minDay = smsMapper.minDay();
LocalDate now = LocalDate.now().minusDays(2);
List<LocalDate> timeList = new ArrayList<>();
while (minDay.isBefore(now) || minDay.isEqual(now)) {
timeList.add(minDay);
minDay = minDay.plusDays(1);
}
for (LocalDate day : timeList) {
taskExecutor.execute(() -> {
long startTime = day.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
long endTime = day.atTime(LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
List<TextMessageCount> textMessageCounts = smsMapper.countByDay(startTime, endTime);
for (TextMessageCount textMessageCount : textMessageCounts) {
textMessageCount.setDay(day);
if (NumberUtil.isInteger(textMessageCount.getSender())) {
textMessageCount.setType(1);
}
textMessageCountMapper.insert(textMessageCount);
}
});
}
}
}
package com.zygh.smsbackend.service;
import com.zygh.smsbackend.cron.TextMessageCountTask;
/**
* @Description 统计页面 service
* @Author wpz
* @Date 2023/8/18 15:51
*/
@Service
public class MsmCountService {
@Resource
private TextMessageCountTask textMessageCountTask;
/**
* 一次数据统计
* @return 一次数据统计
*/
public Result<String> oneTask() {
textMessageCountTask.oneTask();
return Result.success("执行成功");
}
}