Task.java 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package com.template.controller;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.template.annotation.PassToken;
  4. import com.template.common.utils.QuartzJobUtils;
  5. import com.template.mapper.SmartDataTaskMapper;
  6. import com.template.model.pojo.SmartDataTask;
  7. import org.quartz.JobDataMap;
  8. import org.quartz.JobDetail;
  9. import org.quartz.JobExecutionContext;
  10. import org.quartz.JobKey;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.scheduling.quartz.QuartzJobBean;
  13. import org.springframework.stereotype.Component;
  14. import java.text.SimpleDateFormat;
  15. import java.util.Date;
  16. @Component
  17. public class Task extends QuartzJobBean {
  18. @Autowired
  19. private SmartDataTaskMapper smartDataTaskMapper;
  20. @Override
  21. @PassToken
  22. protected void executeInternal(JobExecutionContext jobExecutionContext) {
  23. // 输出当前时间
  24. // Date date = new Date();
  25. // SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  26. // String dateString = dateFormat.format(date);
  27. //
  28. // System.out.println("定时任务名称:【" + key.getName() + "】,执行时间:" + dateString);
  29. JobDetail jobDetail = jobExecutionContext.getJobDetail();
  30. JobKey key = jobDetail.getKey();
  31. // 及时更新下次更新时间
  32. QueryWrapper<SmartDataTask> queryWrapper = new QueryWrapper<>();
  33. queryWrapper.eq(key.getName() != null, "tk_name", key.getName());
  34. SmartDataTask smartDataTask = smartDataTaskMapper.selectOne(queryWrapper);
  35. if (smartDataTask != null) {
  36. // 下次执行的时间
  37. String nextExeTime = QuartzJobUtils.getNextExeTime(smartDataTask.getTkCron());
  38. smartDataTask.setTkNextExeTime(nextExeTime);
  39. try {
  40. smartDataTaskMapper.markTaskById(smartDataTask);
  41. } catch (Exception e) {
  42. System.out.println(e.getMessage());
  43. }
  44. } else {
  45. System.out.println("【下次执行的时间】无法更新至数据库中!");
  46. }
  47. // 工作内容
  48. JobDataMap jobDataMap = jobDetail.getJobDataMap();
  49. // 来源数据源参数
  50. String sourceDriver = (String) jobDataMap.get("sourceDriver");
  51. String sourceUrl = (String) jobDataMap.get("sourceUrl");
  52. String sourceUser = (String) jobDataMap.get("sourceUser");
  53. String sourcePassword = (String) jobDataMap.get("sourcePassword");
  54. String sourceSql = (String) jobDataMap.get("sourceSql");
  55. // 目标数据源参数
  56. String destinationDriver = (String) jobDataMap.get("destinationDriver");
  57. String destinationUrl = (String) jobDataMap.get("destinationUrl");
  58. String destinationUser = (String) jobDataMap.get("destinationUser");
  59. String destinationPassword = (String) jobDataMap.get("destinationPassword");
  60. String destinationSql = (String) jobDataMap.get("destinationSql");
  61. System.out.println("来源库参数:==============================" + "\n" +
  62. " driver:" + sourceDriver + "\n" +
  63. " url:" + sourceUrl + "\n" +
  64. " user:" + sourceUser + "\n" +
  65. " password:" + sourcePassword + "\n" +
  66. " sql:" + sourceSql);
  67. System.out.println("目标库参数:==============================" + "\n" +
  68. " driver:" + destinationDriver + "\n" +
  69. " url:" + destinationUrl + "\n" +
  70. " user:" + destinationUser + "\n" +
  71. " password:" + destinationPassword + "\n" +
  72. " sql:" + destinationSql);
  73. }
  74. }