本篇废话不多,直接上代码,quartz和spring整合的其他相关知识可以看
spring4和quartz2.x整合,对任务做动态操作(增删改查)
spring4整合quartz2.x集群配置,并实现任务计数功能
quartz当机器挂掉重启后定时任务后执行的策略
使用的jar pom配置添加如下:quartz版本是2.x
org.quartz-scheduler
quartz
2.2.3
org.springframework
spring-context-support
quartz配置如下:红色标记的是一个难点,需要注意理解
package com.zyc.springboot.config;
import java.util.Properties;
import javax.sql.DataSource;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import com.zyc.springboot.quartz.MyJobFactory;
/**
* quartz配置类
*
* @author Administrator
*
*/
@Configuration
public class QuartzConfig {
@Autowired
Environment ev;
@Autowired
DataSource dataSource;
@Bean
public SchedulerFactoryBean schedulerFactoryBean(MyJobFactory myJobFactory) {
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setDataSource(dataSource);
// 使job实例(本文中job实例是MyJobBean)支持spring 容器管理
schedulerFactoryBean.setOverwriteExistingJobs(true);
schedulerFactoryBean.setJobFactory(myJobFactory);
// schedulerFactoryBean.setApplicationContextSchedulerContextKey("applicationContext");
// org.springframework.core.io.Resource res=new
// ClassPathResource("quartz.properties");
// schedulerFactoryBean.setConfigLocation(res);
schedulerFactoryBean.setQuartzProperties(quartzProperties());
// 延迟60s启动quartz
schedulerFactoryBean.setStartupDelay(60);
return schedulerFactoryBean;
}
@Bean
public Scheduler scheduler(SchedulerFactoryBean schedulerFactoryBean) {
Scheduler scheduler = schedulerFactoryBean.getScheduler();
try {
scheduler.start();
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return scheduler;
}
public Properties quartzProperties() {
Properties prop = new Properties();
prop.put("quartz.scheduler.instanceName", "schedulerFactoryBean");
prop.put("org.quartz.scheduler.instanceId", "AUTO");
prop.put("org.quartz.scheduler.skipUpdateCheck", "true");
// prop.put("org.quartz.scheduler.jobFactory.class",
// "org.quartz.simpl.SimpleJobFactory");
// JobStoreTX
// prop.put("org.quartz.jobStore.class",
// "org.quartz.impl.jdbcjobstore.JobStoreTX");
prop.put("org.quartz.jobStore.class",
"org.quartz.impl.jdbcjobstore.JobStoreCMT");
prop.put("org.quartz.jobStore.driverDelegateClass",
"org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
prop.put("org.quartz.jobStore.dataSource", "quartzDataSource");
prop.put("org.quartz.jobStore.tablePrefix", "QRTZ_");
prop.put("org.quartz.jobStore.isClustered", "true");
prop.put("org.quartz.threadPool.class",
"org.quartz.simpl.SimpleThreadPool");
prop.put("org.quartz.threadPool.threadCount", "5");
prop.put("org.quartz.jobStore.maxMisfiresToHandleAtATime", "1");
prop.put("org.quartz.jobStore.misfireThreshold", "50000");
// org.quartz.jobStore.txIsolationLevelSerializable 如果为true 会出现无法连续事物的错误
prop.put("org.quartz.jobStore.txIsolationLevelSerializable", "false");
prop.put("org.quartz.jobStore.useProperties", "true");
// prop.put("org.quartz.dataSource.quartzDataSource.driver",
// "oracle.jdbc.driver.OracleDriver");
// prop.put("org.quartz.dataSource.quartzDataSource.URL",
// "jdbc:oracle:thin:@127。0.0.1/orcl");
// prop.put("org.quartz.dataSource.quartzDataSource.user", "spcp");
// prop.put("org.quartz.dataSource.quartzDataSource.password", "spcp");
// prop.put("org.quartz.dataSource.quartzDataSource.maxConnections",
// "10");
prop.put("org.quartz.jobStore.dontSetAutoCommitFalse", "false");
return prop;
}
}
MyJobFactory实现如下:注意代码中红字标记的注释
package com.zyc.springboot.quartz;
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.scheduling.quartz.SpringBeanJobFactory;
import org.springframework.stereotype.Component;
import com.zyc.springboot.util.SpringContext;
/**
* ClassName: MyJobFactory
*
* @author zyc-admin
* @date 2018年3月5日
* @Description: TODO
*/
@Component("myJobFactory")
public class MyJobFactory extends AdaptableJobFactory {
@Autowired
private AutowireCapableBeanFactory beanFactory;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle)
throws Exception {
// TODO Auto-generated method stub
Object jobInstance = super.createJobInstance(bundle);
//AutowireCapableBeanFactory只是让jobInstance具备了自动配置属性的功能,并不是把jobInstance放入到了spring容器中
beanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
对quartz进行增删改查请看 :spring4和quartz2.x整合,对任务做动态操作(增删改查)
可以在MyJob2中直接使用@Autowired注入属性