package com.template.controller; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.template.annotation.PassToken; import com.template.common.utils.QuartzJobUtils; import com.template.mapper.SmartDataTaskMapper; import com.template.model.pojo.SmartDataTask; import org.quartz.JobDataMap; import org.quartz.JobDetail; import org.quartz.JobExecutionContext; import org.quartz.JobKey; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.quartz.QuartzJobBean; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.Date; @Component public class Task extends QuartzJobBean { @Autowired private SmartDataTaskMapper smartDataTaskMapper; @Override @PassToken protected void executeInternal(JobExecutionContext jobExecutionContext) { // 输出当前时间 // Date date = new Date(); // SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // String dateString = dateFormat.format(date); // // System.out.println("定时任务名称:【" + key.getName() + "】,执行时间:" + dateString); JobDetail jobDetail = jobExecutionContext.getJobDetail(); JobKey key = jobDetail.getKey(); // 及时更新下次更新时间 QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq(key.getName() != null, "tk_name", key.getName()); SmartDataTask smartDataTask = smartDataTaskMapper.selectOne(queryWrapper); if (smartDataTask != null) { // 下次执行的时间 String nextExeTime = QuartzJobUtils.getNextExeTime(smartDataTask.getTkCron()); smartDataTask.setTkNextExeTime(nextExeTime); try { smartDataTaskMapper.markTaskById(smartDataTask); } catch (Exception e) { System.out.println(e.getMessage()); } } else { System.out.println("【下次执行的时间】无法更新至数据库中!"); } // 工作内容 JobDataMap jobDataMap = jobDetail.getJobDataMap(); // 来源数据源参数 String sourceDriver = (String) jobDataMap.get("sourceDriver"); String sourceUrl = (String) jobDataMap.get("sourceUrl"); String sourceUser = (String) jobDataMap.get("sourceUser"); String sourcePassword = (String) jobDataMap.get("sourcePassword"); String sourceSql = (String) jobDataMap.get("sourceSql"); // 目标数据源参数 String destinationDriver = (String) jobDataMap.get("destinationDriver"); String destinationUrl = (String) jobDataMap.get("destinationUrl"); String destinationUser = (String) jobDataMap.get("destinationUser"); String destinationPassword = (String) jobDataMap.get("destinationPassword"); String destinationSql = (String) jobDataMap.get("destinationSql"); System.out.println("来源库参数:==============================" + "\n" + " driver:" + sourceDriver + "\n" + " url:" + sourceUrl + "\n" + " user:" + sourceUser + "\n" + " password:" + sourcePassword + "\n" + " sql:" + sourceSql); System.out.println("目标库参数:==============================" + "\n" + " driver:" + destinationDriver + "\n" + " url:" + destinationUrl + "\n" + " user:" + destinationUser + "\n" + " password:" + destinationPassword + "\n" + " sql:" + destinationSql); } }