美文网首页
Spring boot quartz

Spring boot quartz

作者: 念_ae7b | 来源:发表于2018-11-01 17:57 被阅读0次

    添加pom依赖

    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-quartz</artifactId>
    </dependency>
    

    写Factory、创建job实例

    /**
     * @author lenovo
     * quartz的配置类
     */
    @Configuration
    public class QuartzConfig {
        @Autowired
        private SpringJobFactory springJobFactory;
    
        @Bean
        public SchedulerFactoryBean schedulerFactoryBean() {
            SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
            schedulerFactoryBean.setJobFactory(springJobFactory);
            return schedulerFactoryBean;
        }
    
        @Bean
        public Scheduler scheduler() {
            return schedulerFactoryBean().getScheduler();
        }
    }
    /**
     * @author lenovo
     * 自定义的可配置的JobFactory
     */
    @Component
    public class SpringJobFactory extends AdaptableJobFactory {
    
        @Autowired
        private AutowireCapableBeanFactory capableBeanFactory;
    
    
        /**
         * 创建一个Job实列
         * @param bundle
         * @return
         * @throws Exception
         */
        @Override
        protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
            Object jobInstance = super.createJobInstance(bundle);
            capableBeanFactory.autowireBean(jobInstance);
            return jobInstance;
        }
    }
    

    创建job任务类

    /**
     * Created by lenovo on 2018/10/10.
     * 自定义的Job任务类
     */
    @Component
    public class SporterJob {
    
        @Resource
        private WalksService walksService;
    
        @Scheduled(cron = "0 17 08 * * ? ")
        public void updateTodayWalks() throws Exception {
            walksService.updateWalks();
        }
    
    }
    

    相关文章

      网友评论

          本文标题:Spring boot quartz

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