美文网首页
Quartz实例

Quartz实例

作者: 奶盐味小圆饼 | 来源:发表于2020-06-10 14:35 被阅读0次
     <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context-support</artifactId>
      </dependency>
      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
      </dependency>
    
    import com.example33.demo.job.QuartzDemo;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
    import org.springframework.scheduling.quartz.JobDetailFactoryBean;
    import org.springframework.scheduling.quartz.SchedulerFactoryBean;
    import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;
    
    @Configuration
    public class QuartzConfig {
        /**
         * 1,创建Job对象
         */
        @Bean
        public JobDetailFactoryBean jobDetailFactoryBean(){
            JobDetailFactoryBean factory = new JobDetailFactoryBean();
            factory.setJobClass(QuartzDemo.class);
            return factory;
        }
    
        /**
         * 2,创建Trigger对象 --简单trigger
         */
    //    @Bean
    //    public SimpleTriggerFactoryBean simpleTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
    //        SimpleTriggerFactoryBean factory = new SimpleTriggerFactoryBean();
    //        //关联JobDetail对象
    //        factory.setJobDetail(jobDetailFactoryBean.getObject());
    //        //该参数表示一个执行的毫秒数
    //        factory.setRepeatInterval(2000);
    //        //重复次数
    //        factory.setRepeatCount(5);
    //        return factory;
    //    }
    
        /**
         * 2,创建Trigger对象  -- cron 表达式
         * @param jobDetailFactoryBean
         * @return
         */
        @Bean
        public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
            CronTriggerFactoryBean factory = new CronTriggerFactoryBean();
            factory.setJobDetail(jobDetailFactoryBean.getObject());
            factory.setCronExpression("0/2 * * * * ?");
            return factory;
        }
        /**
         * 3,创建Scheduler对象
         */
        @Bean
        public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean,MyAdaptableJobFactory myAdaptableJobFactory){
            SchedulerFactoryBean factory = new SchedulerFactoryBean();
            //关联trigger
            factory.setTriggers(cronTriggerFactoryBean.getObject());
            factory.setJobFactory(myAdaptableJobFactory);
            return factory;
        }
    }
    
    cron 表达式
    例如: 0/2 * * * * ?
     秒 分钟 小时 日 月 星期
     星号(*):可用在所有字段中,表示对应时间域的每一个时刻,例如:"每分钟"
     问号(?):该字段只在日期喝星期字段中使用,它通常指定为"无意义的值",相当于占位符
     减号(-):表达一个范围,如再小时字段中使用"10-12",则表示从10到12点,即10,11,12
     逗号(,):表达一个列表值,如再星期字段中使用"MON,WED,FRI",则表示星期一,星期三和星期五
     斜杠(/):x/y表达一个等步长序列,x为起始值,y为增量步长值,如在分钟字段中使用 0/15,则表示为0,15,30和45秒,
     星期字段 1~7 1表示星期日  7表示星期六
    
    import org.quartz.spi.TriggerFiredBundle;
    import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
    import org.springframework.scheduling.quartz.AdaptableJobFactory;
    import org.springframework.stereotype.Component;
    
    @Component("myAdaptableJobFactory")
    public class MyAdaptableJobFactory extends AdaptableJobFactory {
    
        private final AutowireCapableBeanFactory autowireCapableBeanFactory;
    
        public MyAdaptableJobFactory(AutowireCapableBeanFactory autowireCapableBeanFactory) {
            this.autowireCapableBeanFactory = autowireCapableBeanFactory;
        }
    
        protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
            Object obj = super.createJobInstance(bundle);
            this.autowireCapableBeanFactory.autowireBean(obj);
            return obj;
        }
    }
    
    import com.example33.demo.service.UsersService;
    import org.quartz.Job;
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    import org.springframework.beans.factory.annotation.Autowired;
    
    import java.util.Date;
    
    public class QuartzDemo implements Job {
    
        @Autowired
        private UsersService usersService;
    
        @Override
        public void execute(JobExecutionContext context) throws JobExecutionException {
            System.out.println("Execute..."+new Date());
            this.usersService.addUsers();
        }
    }
    
    @SpringBootApplication(scanBasePackages = "com.example33")
    @MapperScan(basePackages = "com.example33.**.mapper")
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
    }
    

    相关文章

      网友评论

          本文标题:Quartz实例

          本文链接:https://www.haomeiwen.com/subject/uixptktx.html