设置beanFactory以及创建job
package cm.wesure.task;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;
public class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements ApplicationContextAware {
private transient AutowireCapableBeanFactory beanFactory;
@Override
public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
beanFactory = applicationContext.getAutowireCapableBeanFactory();
}
@Override
protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {
final Object job = super.createJobInstance(bundle);
beanFactory.autowireBean(job);
return job;
}
}
配置Trigger Scheduler JobDetail 此处采用注解开发
package cm.wesure.task;
import java.io.IOException;
import java.util.Properties;
import javax.sql.DataSource;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.quartz.spi.JobFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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.transaction.PlatformTransactionManager;
@Configuration
@EnableConfigurationProperties(ShoulderProperties.class)
@ConditionalOnProperty(name = "quartz.enabled")
public class ShoulderConfig {
@Autowired
@Qualifier("cmsTransactionManager")
private PlatformTransactionManager platformTransactionManager;
@Autowired
private ShoulderProperties crons;
@Bean(name="jobFactory")
public JobFactory getJobFactory(ApplicationContext applicationContext){
AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);
return jobFactory;
}
@Bean(name="smsJobDetail")
public JobDetailFactoryBean getJobDetailFactoryBean(){
return creatJobDetail(SelectSmsJob.class);
}
@Bean
public Properties quartzProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
propertiesFactoryBean.afterPropertiesSet();
return propertiesFactoryBean.getObject();
}
@Bean(name="smsCronTrigger")
public CronTriggerFactoryBean getCronTriggerFactoryBean(@Qualifier("smsJobDetail")JobDetail jobDetail){
return creatCronTriggerFactoryBean(jobDetail,crons.getSelectSms());
}
@Bean(name="smsScheduler")
public SchedulerFactoryBean schedulerFactoryBean(@Qualifier("cmsDataSource")DataSource cmsDataSource,@Qualifier("smsCronTrigger")Trigger smsTrigger,
JobFactory jobFactory) throws IOException{
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setTriggers(smsTrigger);
schedulerFactoryBean.setDataSource(cmsDataSource);
schedulerFactoryBean.setJobFactory(jobFactory);
schedulerFactoryBean.setTransactionManager(platformTransactionManager);
schedulerFactoryBean.setQuartzProperties(quartzProperties());
return schedulerFactoryBean;
}
public static CronTriggerFactoryBean creatCronTriggerFactoryBean(JobDetail jobDetail, String cronExpression) {
CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
cronTriggerFactoryBean.setJobDetail(jobDetail);
cronTriggerFactoryBean.setCronExpression(cronExpression);
return cronTriggerFactoryBean;
}
public static JobDetailFactoryBean creatJobDetail(Class class1) {
JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
jobDetailFactoryBean.setJobClass(class1);
jobDetailFactoryBean.setDurability(true);
return jobDetailFactoryBean;
}
}
cronExpression配置
package cm.wesure.task;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix="task.test")
public class ShoulderProperties {
private String selectSms;
public String getSelectSms() {
return selectSms;
}
public void setSelectSms(String selectSms) {
this.selectSms = selectSms;
}
}
任务执行类
package cm.wesure.task;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.fastjson.JSONObject;
import cm.wesure.model.CmsSmsInfo;
import cm.wesure.service.CmsSmsService;
public class SelectSmsJob implements Job {
private static final Logger logger = LoggerFactory.getLogger(SelectSmsJob.class);
@Autowired
private CmsSmsService cmsSmsService;
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
CmsSmsInfo selectByPrimaryKey = cmsSmsService.selectByPrimaryKey(1);
logger.info("task start,This is the smsInfo.{}",JSONObject.toJSONString(selectByPrimaryKey));
}
}
package cm.wesure.springboot;
import java.util.Optional;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.pool.DruidDataSource;
@EnableConfigurationProperties({DataSourceProperties.class})
@Configuration
public class DataSourceBeans {
@Autowired
private DataSourceProperties dataSourceProperties;
@Bean(name="cmsDataSource")
public DataSource createDataSource(){
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName(dataSourceProperties.getDriverclass());
ds.setUrl(dataSourceProperties.getUrl());
ds.setUsername(dataSourceProperties.getUsername());
ds.setPassword(dataSourceProperties.getPassword());
//Optional.ofNullable(dataSourceProperties.getInitialsize()).ifPresent((s) -> {ds.setInitialSize(s);});
return ds;
}
}
项目结构:
2017-09-22 15:25:00.187 INFO 1560 --- [eduler_Worker-2] cm.wesure.task.SelectSmsJob : task start,This is the smsInfo.{"createBy":2,"id":1,"isDelete":0,"productCode":"111","productName":"车险","smsCode":"sms007","smsData":"{\r\n\t\"touser\": \"\",\r\n\t\"sms_id\":\"37888\",\r\n\t\"smsName\":\"\",\r\n\t\"content\":\"您的{1}有新优惠啦,最高享受{2}优惠哦,快来微保看看吧,点击wesure.cn参加活动,回T退订\",\r\n\t\"data\":{\r\n\t\t\"1\": \"车险\",\r\n \"2\": \"68元\"\r\n\t}\r\n}","smsId":"37888","smsName":"车险优惠短信通知","smsSign":"【微保运营】","sysCtime":1503645120000,"sysUtime":1504075924000}
2017-09-22 15:30:00.175 INFO 1560 --- [eduler_Worker-3] cm.wesure.task.SelectSmsJob : task start,This is the smsInfo.{"createBy":2,"id":1,"isDelete":0,"productCode":"111","productName":"车险","smsCode":"sms007","smsData":"{\r\n\t\"touser\": \"\",\r\n\t\"sms_id\":\"37888\",\r\n\t\"smsName\":\"\",\r\n\t\"content\":\"您的{1}有新优惠啦,最高享受{2}优惠哦,快来微保看看吧,点击wesure.cn参加活动,回T退订\",\r\n\t\"data\":{\r\n\t\t\"1\": \"车险\",\r\n \"2\": \"68元\"\r\n\t}\r\n}","smsId":"37888","smsName":"车险优惠短信通知","smsSign":"【微保运营】","sysCtime":1503645120000,"sysUtime":1504075924000}
quartz版本2.2.3 数据库驱动版本要特别注意要用5.1.42之前我用的5.1.6会出现版本冲突问题