在向大家详细介绍Hibernate Quartz之前,首先让大家了解下我们的系统构架,然后全面介绍Hibernate Quartz。
首先介绍一下我们的系统构架,采用webwork,spring,Hibernate整合架构,现在引入Quartz框架,Hibernate Quartz各自拥有的自己数据库配置和连接池的配置,现在的任务是把两者用同一套配置.
先谈谈Quartz,我用的版本是1.5.1,后面的版本我没去看了,没时间,接下来说的都以这个版本为准.Quartz虽然在提供一个 ConnectionProvider接口,提供给程序扩展,但是看它的StdSchedulerFactory实现类,要对它进行扩展还是一件比较麻烦一件事情,所以我就直接修改了StdSchedulerFactory类,以达到的我的需求.
接下来的问题是既然要结合,那么让Quartz来提供连接池配置呢还是有Hibernate提供.研究了两者的连接池方面的源代码,发现 Hibernate的连接池设计远远好于Quartz,Quartz目前只提供一个连接池提供类PoolingConnectionProvider,而且使用的连接池是dbcp,这个连接池听说问题多多,相反Hibernate提供了更加灵活的配置,所以连接池由Hibernate提供.
首先实现一个Quartz中ConnectionProvider子类HibernateConnectionProvider
- public class HibernateConnectionProvider implements ConnectionProvider {
- private Connection con = null;
- protected static ThreadLocal hibernateHolder = new ThreadLocal();
- public Connection getConnection() throws SQLException {
- con = null;
- SessionFactoryImpl sessionFactory = null;
- sessionFactory = (SessionFactoryImpl) Global.getSessionFacotry();
- con = sessionFactory.getConnectionProvider().getConnection();
- return con;
- }
- public void shutdown() throws SQLException {
- // FIXME Auto-generated method stub
- }
- }
然后修改StdSchedulerFactory,在不破坏其本来的逻辑和可配置性下进行了相应的修改.修改的是private Scheduler instantiate() throws SchedulerException方法,找到//Set up any DataSources段进行修改,代码如下:
- String[] dsNames = cfg.getPropertyGroups(PROP_DATASOURCE_PREFIX);
- for (int i = 0; i < dsNames.length; i++) {
- PropertiesParser pp = new PropertiesParser
(cfg.getPropertyGroup(PROP_DATASOURCE_PREFIX + "." + dsNames[i], true));- String dsDriver = pp.getStringProperty(PROP_DATASOURCE_DRIVER, null);
- String dsURL = pp.getStringProperty(PROP_DATASOURCE_URL, null);
- boolean dsAlwaysLookup = pp.getBooleanProperty
(PROP_DATASOURCE_JNDI_ALWAYS_LOOKUP, false);- String dsUser = pp.getStringProperty(PROP_DATASOURCE_USER, "");
- String dsPass = pp.getStringProperty(PROP_DATASOURCE_PASSWORD, "");
- int dsCnt = pp.getIntProperty(PROP_DATASOURCE_MAX_CONNECTIONS, 3);
- String providerClass = pp.getStringProperty
(PROP_DATASOURCE_PROVIDER_CLASS, null);- String dsJndi = pp.getStringProperty(PROP_DATASOURCE_JNDI_URL, null);
- String dsJndiInitial = pp.getStringProperty(PROP_DATASOURCE_JNDI_INITIAL, null);
- String dsJndiProvider = pp.getStringProperty(PROP_DATASOURCE_JNDI_PROVDER, null);
- String dsJndiPrincipal = pp.getStringProperty(PROP_DATASOURCE_JNDI_PRINCIPAL, null);
- String dsJndiCredentials = pp.getStringProperty
(PROP_DATASOURCE_JNDI_CREDENTIALS, null);- String dsValidation = pp.getStringProperty(PROP_DATASOURCE_VALIDATION_QUERY, null);
- if (providerClass == null
- || providerClass.equals("")
- || providerClass.equals("org.quartz.utils.PoolingConnectionProvider")) {
- if (dsDriver == null) {
- initException = new SchedulerException
("Driver not specified for DataSource: "+ dsNames[i]);- throw initException;
- }
- if (dsURL == null) {
- initException = new SchedulerException
("DB URL not specified for DataSource: "+ dsNames[i]);- throw initException;
- }
- try {
- PoolingConnectionProvider cp = new PoolingConnectionProvider
(dsDriver, dsURL, dsUser, dsPass, dsCnt,dsValidation);- dbMgr = DBConnectionManager.getInstance();
- dbMgr.addConnectionProvider(dsNames[i], cp);
- }
- catch (SQLException sqle) {
- initException = new SchedulerException
("Could not initialize DataSource: " + dsNames[i],sqle);- throw initException;
- }
- }
- else if (providerClass.equals("org.quartz.utils.JNDIConnectionProvider")) {
- Properties props = null;
- if (null != dsJndiInitial || null != dsJndiProvider
- || null != dsJndiPrincipal || null != dsJndiCredentials) {
- props = new Properties();
- if (dsJndiInitial != null)props.put(PROP_DATASOURCE_JNDI_INITIAL, dsJndiInitial);
- if (dsJndiProvider != null)props.put(PROP_DATASOURCE_JNDI_PROVDER, dsJndiProvider);
- if (dsJndiPrincipal != null)props.put(PROP_DATASOURCE_JNDI_PRINCIPAL,dsJndiPrincipal);
- if (dsJndiCredentials != null)props.put
(PROP_DATASOURCE_JNDI_CREDENTIALS,dsJndiCredentials);- }
- JNDIConnectionProvider cp = new JNDIConnectionProvider(dsJndi,props, dsAlwaysLookup);
- dbMgr = DBConnectionManager.getInstance();
- dbMgr.addConnectionProvider(dsNames[i], cp);
- }
- else if (providerClass
- .equals("org.quartz.utils.HibernateConnectionProvider")) {
- HibernateConnectionProvider cp = new HibernateConnectionProvider();
- dbMgr = DBConnectionManager.getInstance();
- dbMgr.addConnectionProvider(dsNames[i], cp);
- }
- else {
- initException = new SchedulerException
("Provider_Class not Supported: " + providerClass);- throw initException;
- }
- }
以上介绍Hibernate Quartz。
新闻名称:全面介绍HibernateQuartz
标题路径:http://www.csdahua.cn/qtweb/news42/371542.html
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网