定时任务quartz与spring整合(springboot)
来源:互联网 发布:浙江大学软件学院就业 编辑:程序博客网 时间:2024/05/21 01:28
设置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;@Overridepublic 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;@Autowiredprivate 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);@Autowiredprivate CmsSmsService cmsSmsService;@Overridepublic 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})@Configurationpublic class DataSourceBeans {@Autowiredprivate 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会出现版本冲突问题
阅读全文
0 0
- 定时任务quartz与spring整合(springboot)
- Quartz 定时任务与 Spring 整合要点
- 【spring-boot】 springboot整合quartz实现定时任务(转载)
- SpringBoot的定时任务两种(Spring Schedule 与 Quartz 整合 )实现
- spring整合quartz定时任务
- 定时任务 Spring整合Quartz
- spring-quartz定时任务整合
- Spring整合Quartz定时任务
- springboot整合quartz实现定时任务
- 6. springboot整合quartz实现定时任务
- springboot整合quartz实现定时任务
- Spring与Quartz的整合实现定时任务
- Spring与Quartz的整合实现定时任务调度
- Spring与Quartz的整合实现定时任务调度
- Spring与Quartz的整合实现定时任务调度
- Spring与Quartz的整合实现定时任务调度
- Spring与Quartz的整合实现定时任务调度
- Spring与Quartz的整合实现定时任务调度
- 怎么形象的理解三次握手与四次挥手
- log4j.properties配置详解与实例
- Cesium应用篇:3控件(6) FullScreen/ VR / Home
- java client 连接hbase报错,超时
- 弹幕给我们带来了什么
- 定时任务quartz与spring整合(springboot)
- WebView之简识
- java.lang.NoClassDefFoundError: org/springframework/boot/context/embedded/FilterRegistrationBean
- 解决tomcat的stdout.log文件暴增方法
- 修改密码期限
- Unity C# 随机生成的不重复的几个数字
- 重新安装mysql时,彻底卸载之前的MySQL
- 【vim】【statusline】【状态栏】设置,显示更多有用的信息
- python学习之路-文本操作