创建线程池
package com.firefly.performance.core.common;
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;
@Configuration
@EnableAsync
public class ThreadPoolCommon {
@Bean
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
// 核心池大小
taskExecutor.setCorePoolSize(5);
// 最大线程数
taskExecutor.setMaxPoolSize(20);
// 队列程度
taskExecutor.setQueueCapacity(100);
// 线程空闲时间
taskExecutor.setKeepAliveSeconds(60);
// 线程前缀名称
taskExecutor.setThreadNamePrefix("asyncExecutor--");
// 该方法用来设置 线程池关闭 的时候 等待 所有任务都完成后,再继续 销毁 其他的 Bean,
// 这样这些 异步任务 的 销毁 就会先于 数据库连接池对象 的销毁。
taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
// 任务的等待时间 如果超过这个时间还没有销毁就 强制销毁,以确保应用最后能够被关闭,而不是阻塞住。
taskExecutor.setAwaitTerminationSeconds(60);
// 线程不够用时由调用的线程处理该任务
taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return taskExecutor;
}
}
调用
package com.firefly.performance.core.service.personal.performance;
import com.firefly.performance.api.enums.basic.config.PerformanceTypeEnum;
import com.firefly.performance.core.config.EmailNoticeConfigEnum;
import com.firefly.performance.core.dao.basic.config.TimeControlDao;
import com.firefly.performance.core.dao.performance.PersonalPerformanceDao;
import com.firefly.performance.core.entity.basic.config.TimeControl;
import com.firefly.performance.core.entity.performance.PersonalPerformance;
import com.firefly.performance.core.service.PushAppMessageService;
import com.firefly.performance.core.utils.Assert;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author zhaocn
* @since 2022-10-13
*/
@Slf4j
@Service
public class AgreementConfigService {
@Resource
private PersonalPerformanceDao personalPerformanceDao;
@Resource
private PushAppMessageService pushAppMessageService;
@Resource
private ThreadPoolTaskExecutor taskExecutor;
@Resource
private TimeControlDao timeControlDao;
public Boolean remindBatch(List<Long> performanceIds){
//根据个人绩效id查询员工邮箱
Assert.listNotEmpty(performanceIds,"请选择员工!");
List<PersonalPerformance> personalPerformances = personalPerformanceDao.selectBatchIds(performanceIds);
if (CollectionUtils.isNotEmpty(personalPerformances)) {
Map<String, List<PersonalPerformance>> performanceListMap = personalPerformances.stream().collect(Collectors.groupingBy(data -> MessageFormat.format("{0}#{1}", data.getYear(), data.getCycle())));
for (Map.Entry<String, List<PersonalPerformance>> entry : performanceListMap.entrySet()) {
List<PersonalPerformance> tempList = entry.getValue();
if (CollectionUtils.isNotEmpty(tempList)) {
PersonalPerformance performance = tempList.get(0);
TimeControl timeControl = timeControlDao.selectTimeControlByYearAndCycle(performance.getFirmId(), performance.getYear(), performance.getCycle(), PerformanceTypeEnum.PERSONAL.getType());
taskExecutor.execute(() -> pushAppMessageService.sendAppMessageToEmployee(Collections.singletonList(timeControl), EmailNoticeConfigEnum.SALARY_AGREEMENT, tempList));
}
}
}
return true;
}
}