1.调整线程池大小,库存预警任务使用自定义线程池管理

master
x_z 2 years ago
parent f0f4b215c0
commit ccb3445c75

@ -1,6 +1,7 @@
package com.glxp.api.task;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.thread.ExecutorBuilder;
import cn.hutool.core.thread.ThreadUtil;
import com.glxp.api.dao.inv.InvRemindSetDao;
import com.glxp.api.dao.schedule.ScheduledDao;
@ -16,6 +17,8 @@ import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
@Slf4j
@Component
@ -28,6 +31,8 @@ public class InvRemindMsgTask implements SchedulingConfigurer {
@Resource
private InvRemindSetDao invRemindSetDao;
private static ExecutorService executor;
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addTriggerTask(this::process, triggerContext -> {
@ -39,10 +44,19 @@ public class InvRemindMsgTask implements SchedulingConfigurer {
return null;
}
String cron = scheduled.getCron();
initExecutor();
return new CronTrigger(cron).nextExecutionTime(triggerContext);
});
}
private void initExecutor() {
executor = ExecutorBuilder.create()
.setCorePoolSize(10)
.setMaxPoolSize(300)
.setWorkQueue(new LinkedBlockingDeque<>())
.build();
}
private void process() {
log.info("开始扫描库存信息,生成库存预警消息");
List<InvRemindSetEntity> invRemindSetEntities = invRemindSetDao.selectList(null);
@ -50,7 +64,7 @@ public class InvRemindMsgTask implements SchedulingConfigurer {
log.info("库存预警设置条数:{}", invRemindSetEntities.size());
//开始生成库存信息
for (InvRemindSetEntity invRemindSetEntity : invRemindSetEntities) {
ThreadUtil.execAsync(ThreadUtil.newThread(() -> invRemindMsgService.createRemindMsg(invRemindSetEntity), "createInvRemindThread"));
executor.execute(ThreadUtil.newThread(() -> invRemindMsgService.createRemindMsg(invRemindSetEntity), "createInvRemindThread"));
}
} else {
log.info("无库存预警设置,结束库存扫描");

@ -4,12 +4,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.concurrent.Executor;
@ -26,8 +24,8 @@ public class TaskExecutorConfig implements AsyncConfigurer {
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor();
threadPool.setCorePoolSize(10);//当前线程数
threadPool.setMaxPoolSize(20);// 最大线程数
threadPool.setQueueCapacity(5);//线程池所使用的缓冲队列
threadPool.setMaxPoolSize(200);// 最大线程数
threadPool.setQueueCapacity(Integer.MAX_VALUE);//线程池所使用的缓冲队列
threadPool.setWaitForTasksToCompleteOnShutdown(true);//等待任务在关机时完成--表明等待所有线程执行完
threadPool.setAwaitTerminationSeconds(60 * 15);// 等待时间 默认为0此时立即停止并没等待xx秒后强制停止
threadPool.setThreadNamePrefix("MyAsync-");// 线程名称前缀

Loading…
Cancel
Save