Quartz持久化使用自定义的DataSource方法

1、继承StdSchedulerFactory:

public class SpringSchedulerFactory extends StdSchedulerFactory {

	public void setConnectionProvider(SpringConnectionProvider connectionProvider){
		DBConnectionManager dbMgr = DBConnectionManager.getInstance();
		dbMgr.addConnectionProvider(connectionProvider.getDataSourceName(), connectionProvider);
	}
	
}

 2、编写自己的ConnectionProvider:

public class SpringConnectionProvider implements ConnectionProvider {
	private static final Logger LOG = LoggerFactory.getLogger(SpringConnectionProvider.class);

	private DataSource dataSource;
	
	private String dataSourceName;
	
	@Override
	public Connection getConnection() throws SQLException {
		return dataSource.getConnection();
	}

	@Override
	public void shutdown() throws SQLException {
		LOG.debug("==========do shutdown===========");
		dataSource = null;
	}

	@Override
	public void initialize() throws SQLException {
		LOG.debug("==========do initialize===========");
	}

	/**
	 * @param dataSource the dataSource to set
	 */
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	/**
	 * @param dataSourceName the dataSourceName to set
	 */
	public void setDataSourceName(String dataSourceName) {
		this.dataSourceName = dataSourceName;
	}

	/**
	 * @return the dataSourceName
	 */
	public String getDataSourceName() {
		return dataSourceName;
	}

}

 3、编写Spring配置文件:

<bean name="springCollectionProvider" class="xgt.easy.qtz.service.SpringConnectionProvider">
    <property name="dataSource" ref="dataSource"></property>
    <property name="dataSourceName" value="myDataSource"></property>
</bean>
<bean name="springSchedulerFactory" class="xgt.easy.qtz.service.SpringSchedulerFactory">
    <property name="connectionProvider" ref="springCollectionProvider"></property>
</bean>

 4、quartz.properties配置:

org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties:false

org.quartz.jobStore.dataSource=myDataSource 

#org.quartz.dataSource.myDataSource.connectionProvider.class=xgt.easy.qtz.service.ConnectionProviderService

#数据库表前缀
org.quartz.jobStore.tablePrefix=QRTZ_

org.quartz.jobStore.isClustered=false 

注意:

quartz.properties里面的org.quartz.jobStore.dataSource的值必须配置成和SpringConnectionProvider里面的dataSourceName一样。参见quartz(JobStoreSupport.java)里面获取connection的方法源码如下:

 try {
      conn = DBConnectionManager.getInstance().getConnection(
                    getDataSource());
 } catch (SQLException sqle) {
      throw new JobPersistenceException(
                    "Failed to obtain DB connection from data source '"
                    + getDataSource() + "': " + sqle.toString(), sqle);
 } catch (Throwable e) {
      throw new JobPersistenceException(
                    "Failed to obtain DB connection from data source '"
                    + getDataSource() + "': " + e.toString(), e);
 }

 getDataSource():这里获取的dsName就是quartz.properties里面配置的。

public String getDataSource() {
     return dsName;
}

猜你喜欢

转载自xgtxxxx.iteye.com/blog/2229973