1:AutowiringSpringBeanJobFactory.java
package com.microwisdom.grgzpt.jobs; import org.quartz.spi.TriggerFiredBundle; import org.springframework.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.scheduling.quartz.SpringBeanJobFactory; /** * Created by yan on 2017/5/17. */ public class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements ApplicationContextAware { private transient AutowireCapableBeanFactory beanFactory; @Override public void setApplicationContext(final ApplicationContext context) { beanFactory = context.getAutowireCapableBeanFactory(); } @Override protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception { final Object job = super.createJobInstance(bundle); beanFactory.autowireBean(job); return job; } }
2:GrdbJob.java
package com.microwisdom.grgzpt.jobs; import com.google.gson.Gson; import com.microwisdom.grgzpt.dbsy.DbsyVo; import com.microwisdom.grgzpt.redis.RedisService; import com.microwisdom.grgzpt.utils.DateUtil; import org.quartz.DisallowConcurrentExecution; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.beans.factory.annotation.Autowired; /** * Created by yan on 2017/5/16. * 个人待办任务 */ @DisallowConcurrentExecution public class GrdbJob implements Job{ @Autowired RedisService redisService; @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("Now:"+ DateUtil.getCurrTime()); redisService.set(vo.getSwrydm(), new Gson().toJson(vo)); } }
3:SchedulerConfig.java
package com.microwisdom.grgzpt.jobs; import org.quartz.JobDetail; import org.quartz.SimpleTrigger; import org.quartz.Trigger; import org.quartz.spi.JobFactory; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.config.PropertiesFactoryBean; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.scheduling.quartz.CronTriggerFactoryBean; import org.springframework.scheduling.quartz.JobDetailFactoryBean; import org.springframework.scheduling.quartz.SchedulerFactoryBean; import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean; import java.io.IOException; import java.util.Properties; /** * Created by yan on 2017/5/17. */ @Configuration public class SchedulerConfig { @Bean public JobFactory jobFactory(ApplicationContext applicationContext) { AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory(); jobFactory.setApplicationContext(applicationContext); return jobFactory; } @Bean public SchedulerFactoryBean schedulerFactoryBean(JobFactory jobFactory, Trigger simpleJobTrigger) throws IOException{ SchedulerFactoryBean factory = new SchedulerFactoryBean(); factory.setJobFactory(jobFactory); factory.setQuartzProperties(quartzProperties()); factory.setTriggers(simpleJobTrigger); return factory; } @Bean public CronTriggerFactoryBean simpleJobTrigger(@Qualifier("simpleJobDetail") JobDetail jobDetail) { CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean (); factoryBean.setJobDetail(jobDetail); factoryBean.setStartDelay(1000L); factoryBean.setName("trigger1"); factoryBean.setGroup("group1"); //周1至周5,每天上午8点至下午18点,每分钟执行一次 factoryBean.setCronExpression("0 0/1 8-18 ? * 2-6"); return factoryBean; } @Bean public JobDetailFactoryBean simpleJobDetail() { JobDetailFactoryBean factoryBean = new JobDetailFactoryBean(); factoryBean.setJobClass(GrdbJob.class); factoryBean.setGroup("group1"); factoryBean.setName("job1"); return factoryBean; } @Bean public Properties quartzProperties() throws IOException { PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean(); propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties")); propertiesFactoryBean.afterPropertiesSet(); return propertiesFactoryBean.getObject(); } }