Spring定时器Quartz的用法
首先导入需要的两个jar:
spring-context-support-4.1.1.RELEASE.jar
quartz-2.2.1.jar
1.创建两个类:
2.
QuartzConfiguration:
package com.baibeiyun.yunbang.common.webservice; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.scheduling.quartz.CronTriggerFactoryBean; import org.springframework.scheduling.quartz.JobDetailFactoryBean; import org.springframework.scheduling.quartz.SchedulerFactoryBean; @Configuration public class QuartzConfiguration { @SuppressWarnings("unused") private final Logger log = LoggerFactory.getLogger(getClass()); @Bean public SchedulerFactoryBean schedulerFactory() { SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean(); schedulerFactoryBean.setTriggers(cronTriggerFactoryBean().getObject()); schedulerFactoryBean.setTaskExecutor(threadPoolTaskExecutor()); schedulerFactoryBean.setApplicationContextSchedulerContextKey("applicationContext"); return schedulerFactoryBean; } @Bean public CronTriggerFactoryBean cronTriggerFactoryBean() { CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean(); cronTriggerFactoryBean.setJobDetail(jobDetailFactoryBean().getObject()); cronTriggerFactoryBean.setCronExpression("0 0/1 * * * ?"); //cronTriggerFactoryBean.setCronExpression("0/5 * * * * ?"); return cronTriggerFactoryBean; } @Bean public JobDetailFactoryBean jobDetailFactoryBean() { JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean(); jobDetailFactoryBean.setJobClass(WorkorderCheckJob.class); jobDetailFactoryBean.setDurability(true); return jobDetailFactoryBean; } @Bean public ThreadPoolTaskExecutor threadPoolTaskExecutor() { ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor(); threadPoolTaskExecutor.setCorePoolSize(10); threadPoolTaskExecutor.setMaxPoolSize(20); threadPoolTaskExecutor.setQueueCapacity(5); threadPoolTaskExecutor.setKeepAliveSeconds(100); return threadPoolTaskExecutor; } }
3.WorkorderCheckJob:
package com.textile.quartz; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.SchedulerException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.scheduling.quartz.QuartzJobBean; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; /** * Created by Administrator on 2016/12/4. */ @Service @Transactional public class WorkorderCheckJob extends QuartzJobBean{ @SuppressWarnings("unused") private final Logger log = LoggerFactory.getLogger(getClass()); private static boolean isRun = false; protected void executeInternal(JobExecutionContext executionContext) throws JobExecutionException { if (isRun) { System.out.println("前一次未执行完,跳过本次任务!"); return; } isRun = true; task(executionContext); isRun = false; System.out.println("执行逻辑-isRunFinish"); } private void task(JobExecutionContext executionContext) { try { ApplicationContext applicationContext = (ApplicationContext) executionContext.getScheduler().getContext().get("applicationContext"); //AppInstanceService appInstanceService = (AppInstanceService)applicationContext.getBean(AppInstanceService.class); //appInstanceService.clearInstanceTime(); } catch (BeansException e) { e.printStackTrace(); } catch (SchedulerException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }
4.定时的任务在第三步的类中的executeInternal执行,定时的配置信息在以上的第二步的类中。