添加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();
}
}
网友评论