1
0

QuartzConfig.java 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package com.simuwang.manage.init;
  2. import com.alibaba.fastjson.JSON;
  3. import com.simuwang.base.common.enums.OpenStatusType;
  4. import com.simuwang.base.common.util.QuartzUtils;
  5. import com.simuwang.base.config.DaqProperties;
  6. import com.simuwang.base.pojo.dos.MailboxInfoDO;
  7. import com.simuwang.base.pojo.dto.MailboxInfoDTO;
  8. import com.simuwang.base.pojo.dto.QuartzBean;
  9. import com.simuwang.manage.service.EmailConfigService;
  10. import org.quartz.Scheduler;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.beans.factory.annotation.Value;
  15. import org.springframework.boot.ApplicationArguments;
  16. import org.springframework.boot.ApplicationRunner;
  17. import org.springframework.context.annotation.Configuration;
  18. import org.springframework.core.annotation.Order;
  19. import java.util.List;
  20. /**
  21. * 定时任务启动类
  22. * Author: chenjianhua
  23. * Date: 2024/9/17 11:55
  24. * Description: ${DESCRIPTION}
  25. */
  26. @Configuration
  27. @Order(2)
  28. public class QuartzConfig implements ApplicationRunner {
  29. @Autowired
  30. private Scheduler scheduler;
  31. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  32. @Autowired
  33. private EmailConfigService emailConfigService;
  34. @Value("${spring.application.name}")
  35. private String groupName;
  36. private String JOB_CLASS="com.simuwang.manage.task.ParseSchedulerTask";
  37. private final Boolean enableQuartz;
  38. public QuartzConfig(DaqProperties properties) {
  39. this.enableQuartz = properties.getEnableQuartz();
  40. }
  41. @Override
  42. public void run(ApplicationArguments args){
  43. // 没有开启定时任务功能时直接退出
  44. if (!enableQuartz) {
  45. return;
  46. }
  47. List<MailboxInfoDO> mailboxInfoDOS = emailConfigService.getAll();
  48. for(MailboxInfoDO mailboxInfoDO : mailboxInfoDOS){
  49. try{
  50. QuartzBean quartzBean = new QuartzBean();
  51. quartzBean.setCronExpression(mailboxInfoDO.getCron());
  52. quartzBean.setStatus(mailboxInfoDO.getOpenStatus());
  53. quartzBean.setGroupName(groupName);
  54. quartzBean.setJobName(mailboxInfoDO.getEmail());
  55. quartzBean.setJobClass(JOB_CLASS);
  56. //请求参数
  57. MailboxInfoDTO paramDTO = new MailboxInfoDTO();
  58. paramDTO.setAccount(mailboxInfoDO.getEmail());
  59. paramDTO.setPassword(mailboxInfoDO.getPassword());
  60. paramDTO.setPort(mailboxInfoDO.getPort());
  61. paramDTO.setHost(mailboxInfoDO.getServer());
  62. paramDTO.setProtocol(mailboxInfoDO.getProtocol());
  63. quartzBean.setJobParam(JSON.toJSONString(paramDTO));
  64. QuartzUtils.createScheduleJob(scheduler,quartzBean);
  65. if(mailboxInfoDO.getOpenStatus().equals(OpenStatusType.YES.getCode())){
  66. QuartzUtils.resumeScheduleJob(scheduler,quartzBean.getJobName());
  67. }else{
  68. QuartzUtils.pauseScheduleJob(scheduler,quartzBean.getJobName());
  69. }
  70. }catch (Exception e) {
  71. logger.error(e.getMessage(),e);
  72. }
  73. }
  74. }
  75. }