49 lines
1.7 KiB
Java
49 lines
1.7 KiB
Java
package com.gunshi.project.xyt.schedule;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
import com.gunshi.project.xyt.model.ForecastPlan;
|
|
import com.gunshi.project.xyt.service.ForecastPlanService;
|
|
import jakarta.annotation.Resource;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.boot.CommandLineRunner;
|
|
import org.springframework.context.annotation.Profile;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.function.Function;
|
|
import java.util.stream.Collectors;
|
|
|
|
/**
|
|
* @author cxw
|
|
* @description 任务预热,预先加载数据库已经配置好的任务
|
|
* @classname TaskGroupJobRunner.java
|
|
* @create 2024-07-30, 星期二, 9:42:39
|
|
*/
|
|
@Component
|
|
@Slf4j
|
|
@Profile("prod")
|
|
public class TaskGroupJobRunner implements CommandLineRunner {
|
|
|
|
@Resource
|
|
private TaskGroupHandler taskGroupHandler;
|
|
|
|
@Autowired
|
|
private ForecastPlanService forecastPlanService;
|
|
|
|
@Override
|
|
public void run(String... args) {
|
|
// 初始加载数据库里状态为自动、正常的定时任务
|
|
List<ForecastPlan> planList = forecastPlanService.list(new QueryWrapper<ForecastPlan>().eq("type", "1").eq("status", "0"));
|
|
if (CollectionUtils.isNotEmpty(planList)) {
|
|
Map<Integer, ForecastPlan> jobMap = planList.stream()
|
|
.collect(Collectors.toMap(ForecastPlan::getId, Function.identity(), (key1, key2)->key2));
|
|
for (Map.Entry<Integer, ForecastPlan> entry : jobMap.entrySet()) {
|
|
taskGroupHandler.addCronJob(String.valueOf(entry.getKey()), entry.getValue());
|
|
}
|
|
}
|
|
}
|
|
}
|