1.maven依赖:
<!--quartz--> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency>
<!-- 该依赖必加,里面有sping对schedule的支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
2.定时任务配置类
import org.quartz.Scheduler; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.quartz.SchedulerFactoryBean; /** * 定时任务配置 * * @author yangfeng * @date 2018-07-03 */ @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(); } }
3.配置job交给spring管理,主要目的就是解决job类 注入其他service或者使用Spring组件
import org.quartz.spi.TriggerFiredBundle; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.scheduling.quartz.AdaptableJobFactory; import org.springframework.stereotype.Component; @Component public class SpringJobFactory extends AdaptableJobFactory { @Autowired private AutowireCapableBeanFactory capableBeanFactory; @Override protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception { Object jobInstance = super.createJobInstance(bundle); capableBeanFactory.autowireBean(jobInstance); return jobInstance; } }
4.定时任务创建job,通过注入Scheduler对任务进行操作
import org.quartz.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * 定时任务服务 * * @author yangfeng * @date 2018-07-03 */ @Service public class QuartzService { private static final String JOB_GROUP = "event_job_group"; private static final String TRIGGER_GROUP = "event_trigger_group"; @Autowired private Scheduler scheduler; /** * 创建定时任务 * @param jobDetailName * @param cronExpression * @param jobClass * @throws SchedulerException */ public void createScheduleJob(String jobDetailName, String cronExpression, Class<? extends Job> jobClass) throws SchedulerException { JobDetail jobDetail = JobBuilder.newJob(jobClass) .withIdentity("task_" + jobDetailName, JOB_GROUP).storeDurably().requestRecovery().build(); CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression); Trigger trigger = TriggerBuilder.newTrigger().withIdentity("task_" + jobDetailName, TRIGGER_GROUP).withSchedule(scheduleBuilder).build(); scheduler.scheduleJob(jobDetail, trigger); } }
5.任务执行类,具体业务逻辑
import com.jp.zpzc.service.IUserVoteService; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.annotation.Resource; /** * 投票统计 * * @author yangfeng * @date 2018-07-03 */ public class VoteStatisticsTask implements Job { private static Logger LOG = LoggerFactory.getLogger(VoteStatisticsTask.class); @Resource private IUserVoteService userVoteService; @Override public void execute(JobExecutionContext jobExecutionContext) { //统计已结束的投票 userVoteService.updateVote(); } }
6.启动监听去初始化Quartz
import org.quartz.SchedulerException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.Configuration; import org.springframework.context.event.ContextRefreshedEvent; /** * 监听器,启动定时任务 * * @author yangfeng * @date 2018-07-03 */ @Configuration public class ApplicationStartQuartzJobListener implements ApplicationListener<ContextRefreshedEvent> { private static Logger LOG = LoggerFactory.getLogger(ApplicationStartQuartzJobListener.class); @Autowired private QuartzService quartzService; /** * 初始启动quartz */ @Override public void onApplicationEvent(ContextRefreshedEvent event) { try { //成分分析 //quartzService.createScheduleJob("IngredientAnalysisTask", "*/5 * * * * ?", IngredientAnalysisTask.class); //投票统计 quartzService.createScheduleJob("VoteStatisticsTask", "*/5 * * * * ?", VoteStatisticsTask.class); LOG.info("任务已经启动..."); } catch (SchedulerException e) { LOG.error(e.getMessage()); } } }
0k