You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.8 KiB
Java
77 lines
2.8 KiB
Java
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;
|
|
import com.glxp.api.entity.inv.InvRemindSetEntity;
|
|
import com.glxp.api.entity.system.ScheduledEntity;
|
|
import com.glxp.api.req.system.ScheduledRequest;
|
|
import com.glxp.api.service.inv.InvRemindMsgService;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.scheduling.annotation.SchedulingConfigurer;
|
|
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
|
|
import org.springframework.scheduling.support.CronTrigger;
|
|
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
|
|
public class InvRemindMsgTask implements SchedulingConfigurer {
|
|
|
|
@Resource
|
|
private ScheduledDao scheduledDao;
|
|
@Resource
|
|
private InvRemindMsgService invRemindMsgService;
|
|
@Resource
|
|
private InvRemindSetDao invRemindSetDao;
|
|
|
|
private static ExecutorService executor;
|
|
|
|
@Override
|
|
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
|
|
taskRegistrar.addTriggerTask(this::process, triggerContext -> {
|
|
ScheduledRequest scheduledRequest = new ScheduledRequest();
|
|
scheduledRequest.setCronName("invRemindMsgTask");
|
|
ScheduledEntity scheduled = scheduledDao.findScheduled(scheduledRequest);
|
|
if (null == scheduled) {
|
|
log.error("库存预警定时任务未配置,请注意!");
|
|
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);
|
|
if (CollUtil.isNotEmpty(invRemindSetEntities)) {
|
|
log.info("库存预警设置条数:{}", invRemindSetEntities.size());
|
|
//开始生成库存信息
|
|
for (InvRemindSetEntity invRemindSetEntity : invRemindSetEntities) {
|
|
executor.execute(ThreadUtil.newThread(() -> invRemindMsgService.createRemindMsg(invRemindSetEntity), "createInvRemindThread"));
|
|
}
|
|
} else {
|
|
log.info("无库存预警设置,结束库存扫描");
|
|
}
|
|
}
|
|
}
|