package com.zksy.data.schedule; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.zksy.data.domain.po.XhTimeTask; import com.zksy.data.mapper.XhTimeTaskMapper; import com.zksy.data.utils.TaskManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.List; @Component @EnableScheduling public class SyncTimeTaskSchedule { @Autowired private TaskManager taskManager; @Autowired private XhTimeTaskMapper xhTimeTaskMapper; @Autowired private CollectDataSchedule collectDataSchedule; @Autowired private ApplicationContext applicationContext; /** * 每天进行一次xhTimeTask 表数据同步定时任务 */ @Scheduled(cron = "0 0 0 * * ?") // @Scheduled(cron = "*/10 * * * * *") public void syncXhTimeTask(){ //把xhtimetask这个表的所有service_name 都取出来放在一个list中 LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(XhTimeTask::getEnabled,1); List list = xhTimeTaskMapper.selectList(wrapper); //判断list的长度和taskManager.getTaskMap()的长度是否一致,如果不一致就进行同步 taskManager.stopAllTasks(); if(list.size()!=taskManager.getTaskMap().size()){ list.parallelStream().forEach(task->{ if(!taskManager.getTaskMap().containsKey(task.getTaskName())){ taskManager.addTask(task.getTaskName(),() -> { //暂时先打印 // Object bean = applicationContext.getBean(task.getServiceName()); // if(bean instanceof IBaseSaveDataInterface){ // ((IBaseSaveDataInterface) bean).saveDataByUniCode(collectDataSchedule.serviceMap.get(task.getServiceName())); // } System.out.println("执行任务"+task.getServiceName()); }, task.getCronExpression()); } }); } //taskManager.getTaskMap()判断这个集合中的key有没有这个service_name,如果没有就先从xhtimetask取出数据添加进定时任务 } }