Spring boot quartz

1、添加pom依赖


   org.springframework.boot
   spring-boot-starter-quartz

2、写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;
    }
}

3、创建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();
    }

}

4、正常创建类、接口、controller层

6PJ~P~SI~G@@HIG7%TM3YMR.png

你可能感兴趣的:(Spring boot quartz)