Advertisement
aironman

EthQuartzScheduler

Jul 23rd, 2019
147
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.aironman.demoquartz.scheduler;
  2.  
  3. import javax.annotation.PostConstruct;
  4.  
  5. import com.aironman.demoquartz.pojo.Ethereum;
  6. import org.quartz.JobDetail;
  7. import org.quartz.SimpleTrigger;
  8. import org.quartz.Trigger;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
  13. import org.springframework.context.ApplicationContext;
  14. import org.springframework.context.annotation.Bean;
  15. import org.springframework.context.annotation.Configuration;
  16. import org.springframework.core.io.ClassPathResource;
  17. import org.springframework.scheduling.quartz.JobDetailFactoryBean;
  18. import org.springframework.scheduling.quartz.SchedulerFactoryBean;
  19. import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;
  20. import org.springframework.scheduling.quartz.SpringBeanJobFactory;
  21.  
  22. import com.aironman.demoquartz.config.AutoWiringSpringBeanJobFactory;
  23.  
  24. @Configuration
  25. @ConditionalOnExpression("'${using.spring.schedulerFactory}'=='true'")
  26. public class EthQuartzScheduler {
  27.  
  28. Logger logger = LoggerFactory.getLogger(getClass());
  29.  
  30. @Autowired
  31. private ApplicationContext applicationContext;
  32.  
  33. @PostConstruct
  34. public void init() {
  35. logger.info("EthQuartzScheduler POSTCONSTRUCT...");
  36. }
  37.  
  38. @Bean
  39. public SpringBeanJobFactory springBeanJobFactory() {
  40. AutoWiringSpringBeanJobFactory jobFactory = new AutoWiringSpringBeanJobFactory();
  41. logger.debug("Configuring Job factory");
  42.  
  43. jobFactory.setApplicationContext(applicationContext);
  44. return jobFactory;
  45. }
  46.  
  47. @Bean
  48. public SchedulerFactoryBean scheduler(Trigger trigger, JobDetail job) {
  49.  
  50. SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean();
  51. schedulerFactory.setConfigLocation(new ClassPathResource("quartz.properties"));
  52.  
  53. logger.info("Setting the ETH Scheduler up");
  54. schedulerFactory.setJobFactory(springBeanJobFactory());
  55. schedulerFactory.setJobDetails(job);
  56. schedulerFactory.setTriggers(trigger);
  57.  
  58. return schedulerFactory;
  59. }
  60.  
  61. @Bean
  62. public JobDetailFactoryBean jobDetail() {
  63.  
  64. JobDetailFactoryBean jobDetailFactory = new JobDetailFactoryBean();
  65. jobDetailFactory.setJobClass(EthereumJob.class);
  66. jobDetailFactory.setName("ETH-Qrtz_Job_Detail");
  67. jobDetailFactory.setDescription("Invoke ETH Job service...");
  68. jobDetailFactory.setDurability(true);
  69. logger.info("ETH Quartz scheduler set up!");
  70. return jobDetailFactory;
  71. }
  72.  
  73. @Bean
  74. public SimpleTriggerFactoryBean trigger(JobDetail job) {
  75.  
  76. SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
  77. trigger.setJobDetail(job);
  78.  
  79. int frequencyInSec = 5;
  80. logger.info("Configuring trigger to fire EthQuartzScheduler every {} seconds", frequencyInSec);
  81.  
  82. trigger.setRepeatInterval(frequencyInSec * 1000);
  83. trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
  84. trigger.setName("ETH-Qrtz_Trigger");
  85. logger.info("ETH-Qrtz_Trigger TRIGGERED!");
  86. return trigger;
  87. }
  88.  
  89.  
  90. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement