McScheduleTask.java 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package com.malk.schedule;
  2. import com.malk.service.dingtalk.DDClient;
  3. import com.malk.service.dingtalk.DDClient_Event;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.scheduling.annotation.EnableScheduling;
  9. /**
  10. * 定时任务ScheduleTask
  11. * -
  12. * 1. @EnableScheduling:开启定时任务,当前文件文件全局注解【也可单独成为一个控制的配置文件管控全局】
  13. * 2. @Scheduled:定时执行的方法,通过入参控制
  14. * - 1. @Scheduled(fixedDelay = 5000):单位毫秒,当任务执行完毕后5s后再执行
  15. * - 2. @Scheduled(fixedRate = 3000):单位毫秒,表示每隔3秒,不受执行时间影响
  16. * - 3. cron表达式:cron一共有7位,但是最后一位是年,可以留空,cron中,还有一些特殊的符号
  17. * 3. @component / @Configuration:声明定时任务为组件类。若不声明,定时器无效,因为没有注入
  18. */
  19. /**
  20. * @EnableScheduling 条件注入, 根据条件确定当前类是否要装载Bean: 如定时器开发环境不启动
  21. * -
  22. * 示例:注意格式 @ConditionalOnExpression(value = "${spel.scheduling}") 或者 @ConditionalOnProperty(name = "spel.scheduling", havingValue = "false")
  23. * *
  24. * @ConditionalOnExpression,可多个参数,支持与、或关系,默认匹配bool。注意若对应环境未识别到声明会报错
  25. * @ConditionalOnProperty,可多个参数,支持与,若获取值为空识别为false,若有值则将该值与havingValue指定的值进行比较,匹配结果为bool。不支持或
  26. */
  27. @Slf4j
  28. @Configuration
  29. @EnableScheduling
  30. @ConditionalOnProperty(name = {"spel.scheduling"}, havingValue = "false")
  31. public class McScheduleTask {
  32. @Autowired
  33. private DDClient ddClient;
  34. @Autowired
  35. private DDClient_Event ddClient_event;
  36. /**
  37. * 钉钉事件回调 3_3
  38. * -
  39. * 同步钉钉推送失败记录: 推送失败列表, 获取后记录会被清空
  40. */
  41. // @Scheduled(cron = "0 0/30 7-23 * * ?")
  42. public void syncDingTalkFailedList() {
  43. try {
  44. ddClient_event.syncFailedList(ddClient.getAccessToken());
  45. } catch (Exception e) {
  46. // 记录错误信息
  47. e.printStackTrace();
  48. }
  49. }
  50. }