1.maven依賴:java
<!--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.定時任務配置類spring
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組件maven
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對任務進行操做ide
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.任務執行類,具體業務邏輯ui
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.啓動監聽去初始化Quartzspa
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()); } } }
0kcode