1
0

QuartzConfig.java 3.6 KB

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