=========================================
=========================================
QuartzConfig.java
=========================================
@Configuration
public class QuartzConfig {
/**
* 设置属性
*
* @return
* @throws IOException
*/
private Properties quartzProperties() throws IOException {
Properties prop = new Properties();
//Configure Main Scheduler Properties
prop.put("org.quartz.scheduler.instanceName", "DefaultQuartzScheduler");
prop.put("org.quartz.scheduler.wrapJobExecutionInUserTransaction", "false");
prop.put("org.quartz.scheduler.instanceId", "AUTO");
//Configure rmi
prop.put("org.quartz.scheduler.rmi.export", "false");
prop.put("org.quartz.scheduler.rmi.proxy", "false");
//Configure ThreadPool
prop.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
prop.put("org.quartz.threadPool.threadCount", "50");
prop.put("org.quartz.threadPool.threadPriority", "5");
prop.put("org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread", "true");
//Configure JobStore
prop.put("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
prop.put("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
prop.put("org.quartz.jobStore.tablePrefix", "QRTZ_");
prop.put("org.quartz.jobStore.dataSource", "myDS");
prop.put("org.quartz.jobStore.misfireThreshold", "60000");
prop.put("org.quartz.jobStore.isClustered", "true");
prop.put("org.quartz.jobStore.clusterCheckinInterval", "1000");
//Configure datasource
prop.put("org.quartz.dataSource.myDS.connectionProvider.class", "org.spring.springboot.config.QuartzDBConnection");
prop.put("org.quartz.dataSource.myDS.driver", "com.mysql.jdbc.Driver");
prop.put("org.quartz.dataSource.myDS.url", "jdbc:mysql://host:3306/movikr_quartz?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull");
prop.put("org.quartz.dataSource.myDS.user", "");
prop.put("org.quartz.dataSource.myDS.password", "");
prop.put("org.quartz.dataSource.myDS.maxConnections", "10");
prop.put("org.quartz.dataSource.myDS.validationQuery", "select 0");
return prop;
}
//
//
//
//
//
@Bean(name = "schedulerFactoryBean")
@Lazy(true)
public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
// this allows to update triggers in DB when updating settings in config file:
//用于quartz集群,QuartzScheduler 启动时更新己存在的Job,这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了
factory.setOverwriteExistingJobs(true);
//用于quartz集群,加载quartz数据源
//factory.setDataSource(dataSource);
//QuartzScheduler 延时启动,应用启动完10秒后 QuartzScheduler 再启动
factory.setStartupDelay(5);
//用于quartz集群,加载quartz数据源配置
factory.setQuartzProperties(quartzProperties());
factory.setAutoStartup(true);
factory.setApplicationContextSchedulerContextKey("applicationContextKey");
//factory.setConfigLocation(new FileSystemResource(this.getClass().getResource("/quartz.properties").getPath()));
return factory;
}
@Bean(name = "schedule-service-QuartzManager", initMethod="initJobs")
//@DependsOn("springContextHolder")
public QuartzManager quartzManager() {
QuartzManager quartzManager = new QuartzManager();
return quartzManager;
}
}
=========================================
QuartzDBConnection.java
=========================================
public class QuartzDBConnection implements ConnectionProvider {
//JDBC驱动
public String driver;
//JDBC连接串
public String url;
//数据库用户名
public String user;
//数据库用户密码
public String password;
//数据库最大连接数
public int maxConnections;
//数据库SQL查询每次连接返回执行到连接池,以确保它仍然是有效的。
public String validationQuery;
private boolean validateOnCheckout;
private int idleConnectionValidationSeconds;
public String maxCachedStatementsPerConnection;
private String discardIdleConnectionsSeconds;
public static final int DEFAULT_DB_MAX_CONNECTIONS = 10;
public static final int DEFAULT_DB_MAX_CACHED_STATEMENTS_PER_CONNECTION = 120;
//Druid连接池
private DruidDataSource datasource;
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* 接口实现
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
public Connection getConnection() throws SQLException {
return datasource.getConnection();
}
public void shutdown() throws SQLException {
datasource.close();
}
public void initialize() throws SQLException{
if (this.url == null) {
throw new SQLException("DBPool could not be created: DB URL cannot be null");
}
if (this.driver == null) {
throw new SQLException("DBPool driver could not be created: DB driver class name cannot be null!");
}
if (this.maxConnections < 0) {
throw new SQLException("DBPool maxConnectins could not be created: Max connections must be greater than zero!");
}
datasource = new DruidDataSource();
try{
datasource.setDriverClassName(this.driver);
} catch (Exception e) {
try {
throw new SchedulerException("Problem setting driver class name on datasource: " + e.getMessage(), e);
} catch (SchedulerException e1) {
}
}
datasource.setUrl(this.url);
datasource.setUsername(this.user);
datasource.setPassword(this.password);
datasource.setMaxActive(this.maxConnections);
datasource.setMinIdle(1);
datasource.setMaxWait(0);
datasource.setMaxPoolPreparedStatementPerConnectionSize(this.DEFAULT_DB_MAX_CACHED_STATEMENTS_PER_CONNECTION);
if (this.validationQuery != null) {
datasource.setValidationQuery(this.validationQuery);
if(!this.validateOnCheckout)
datasource.setTestOnReturn(true);
else
datasource.setTestOnBorrow(true);
datasource.setValidationQueryTimeout(this.idleConnectionValidationSeconds);
}
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getMaxConnections() {
return maxConnections;
}
public void setMaxConnections(int maxConnections) {
this.maxConnections = maxConnections;
}
public String getValidationQuery() {
return validationQuery;
}
public void setValidationQuery(String validationQuery) {
this.validationQuery = validationQuery;
}
public boolean isValidateOnCheckout() {
return validateOnCheckout;
}
public void setValidateOnCheckout(boolean validateOnCheckout) {
this.validateOnCheckout = validateOnCheckout;
}
public int getIdleConnectionValidationSeconds() {
return idleConnectionValidationSeconds;
}
public void setIdleConnectionValidationSeconds(int idleConnectionValidationSeconds) {
this.idleConnectionValidationSeconds = idleConnectionValidationSeconds;
}
public DruidDataSource getDatasource() {
return datasource;
}
public void setDatasource(DruidDataSource datasource) {
this.datasource = datasource;
}
}
================================================
QuartzManager.java
================================================
public class QuartzManager {
private static final Logger LOG = LoggerFactory.getLogger(QuartzManager.class);
@Resource(name = "schedulerFactoryBean")
private Scheduler scheduler;
/**
* 初始化(spring启动 或其他)可运行的job 立即执行
* 配置成bean 指定init-method="initJobs" 后于springContextHolder bean初始化
*/
public void initJobs() throws SchedulerException {
if (!scheduler.isShutdown()) {
scheduler.start();
}
}
}