锁定老帖子 主题:Quartz任务监控管理 (1)
该帖已经被评为精华帖
|
|
---|---|
作者 | 正文 |
发表时间:2009-08-31
我换了oracle驱动就可以了,
原来是9.0.2.0.0版的不行, 换成10.2.0.1.0版的就可以了 |
|
返回顶楼 | |
发表时间:2009-09-12
我还想问一下,我做的数据库备份操作话在testMethod里面,服务器启动的时候时间没到也会调用一次,这样不就乱了吗?这个问题怎么解决?
还有就是我把bakcupDao注入到simpleSerivce里了,我的backupDao里是这么写的: public boolean backupDateabase(String dbname, String bfname) { final String dbName = dbname; final String bfname1 = bfname; return (Boolean) super.getHibernateTemplate().execute( new HibernateCallback() { public Object doInHibernate(Session session) { boolean flag = true; PreparedStatement pstmt = null; try { pstmt = session.connection().prepareStatement( "{call p_Backup_Or_Restore(?,?,?)}"); pstmt.setString(1, bfname1); pstmt.setString(2, dbName); pstmt.setInt(3, 1); pstmt.execute(); System.out.println("数据库已备份"); } catch (Exception e) { flag = false; e.printStackTrace(); } return flag; } }); } 执行这个方法的,super.getHibernateTemplate()得到的hibernateTemplate为空,是什么原因? 我是初学者,有很多问题不懂,还请LZ多多指导,谢啦! |
|
返回顶楼 | |
发表时间:2009-09-13
最后修改:2009-09-14
yanyu510 写道 我还想问一下,我做的数据库备份操作话在testMethod里面,服务器启动的时候时间没到也会调用一次,这样不就乱了吗?这个问题怎么解决?
还有就是我把bakcupDao注入到simpleSerivce里了,我的backupDao里是这么写的: public boolean backupDateabase(String dbname, String bfname) { final String dbName = dbname; final String bfname1 = bfname; return (Boolean) super.getHibernateTemplate().execute( new HibernateCallback() { public Object doInHibernate(Session session) { boolean flag = true; PreparedStatement pstmt = null; try { pstmt = session.connection().prepareStatement( "{call p_Backup_Or_Restore(?,?,?)}"); pstmt.setString(1, bfname1); pstmt.setString(2, dbName); pstmt.setInt(3, 1); pstmt.execute(); System.out.println("数据库已备份"); } catch (Exception e) { flag = false; e.printStackTrace(); } return flag; } }); } 执行这个方法的,super.getHibernateTemplate()得到的hibernateTemplate为空,是什么原因? 我是初学者,有很多问题不懂,还请LZ多多指导,谢啦! 服务器起来时不会调用一次的,调不调用跟你的配置时间有关系,时间不到绝不会自动调用的,这个你放心好了 上面说过simpleService和其中注入各属性需要实现Serializable序列化接口,你的BakcupDao继承自HibernateDaoSupport虽然也实现了序列化接口,但是HibernateDaoSupport里的HibernateTemplate并没有实现序列化接口,所以你取得的HibernateTemplate永远为null。因此获取HibernateTemplate必须换一种方式,你的BakcupDao不能继承自HibernateDaoSupport。HibernateTemplate没有实现序列化接口,而SessionFactory是实现序列化接口的,在bakcupDao注入SessionFactory,通过SessionFactory获取HibernateTemplate。 你的bakcupDao可以这样写 import java.io.Serializable; import org.hibernate.SessionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import com.sundoctor.example.service.SimpleService; @Repository("bakcupDao") public class BakcupDao implements Serializable { /** * */ private static final long serialVersionUID = 1L; private SessionFactory sessionFactory; @Autowired public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public boolean backupDateabase(String dbname, String bfname) { final String dbName = dbname; final String bfname1 = bfname; HibernateTemplate hibernateTemplate = new HibernateTemplate(sessionFactory); return (Boolean)hibernateTemplate.execute(new HibernateCallback() { public Object doInHibernate(Session session) { boolean flag = true; PreparedStatement pstmt = null; try { pstmt = session.connection().prepareStatement("{call p_Backup_Or_Restore(?,?,?)}"); pstmt.setString(1, bfname1); pstmt.setString(2, dbName); pstmt.setInt(3, 1); pstmt.execute(); System.out.println("数据库已备份"); } catch (Exception e) { flag = false; e.printStackTrace(); } return flag; } }); } } |
|
返回顶楼 | |
发表时间:2009-09-13
谢谢了。。很久没用spring框架。单独使用quartz。这个框架做起来还是比较麻烦。
|
|
返回顶楼 | |
发表时间:2009-09-13
ningmenglovesoft 写道 谢谢了。。很久没用spring框架。单独使用quartz。这个框架做起来还是比较麻烦。
不用spring,单独使用quartz一样简单。 |
|
返回顶楼 | |
发表时间:2009-09-14
你说的那种方法还是不行,因为注入进去的sessionFactory为空,所以,HibernateTemplate也为空。还有就是按照你的方法改了以后,我原来添加一个tigger是可以的,现在还抛异常了,异常信息如下: Couldn't store job: Unable to serialize JobDataMap for insertion into database because the value of property 'simpleService' is not serializable: org.springframework.orm.hibernate3.AbstractSessionFactoryBean$TransactionAwareInvocationHandler 我已经实现了序列化接口。不明白怎么回事。 这是我的simpleService类: package com.sundoctor.example.service; import java.io.Serializable; import com.dao.BackupDao; public class SimpleService implements Serializable { private static final long serialVersionUID = 8972659512920450831L; private BackupDao backupDao; public BackupDao getBackupDao() { return backupDao; } public void setBackupDao(BackupDao backupDao) { this.backupDao = backupDao; } public void testMethod(String triggerName) { System.out.println(triggerName); } } 这是我的backupDao: package com.dao; import java.io.Serializable; import java.sql.PreparedStatement; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.orm.hibernate3.HibernateCallback; public class BackupDao implements Serializable { /** * */ private static final long serialVersionUID = 8933456923281729867L; private MyHibernateTemplate hibernateTemlate; private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public boolean backupDateabase(String dbname, String bfname) { final String dbName = dbname; final String bfname1 = bfname; return (Boolean) hibernateTemlate.execute(new HibernateCallback() { public Object doInHibernate(Session session) { boolean flag = true; PreparedStatement pstmt = null; try { pstmt = session.connection().prepareStatement( "{call p_Backup_Or_Restore(?,?,?)}"); pstmt.setString(1, bfname1); pstmt.setString(2, dbName); pstmt.setInt(3, 1); pstmt.execute(); System.out.println("数据库已备份"); } catch (Exception e) { flag = false; e.printStackTrace(); } return flag; } }); } } 这是我的myHibernateTemplate: public class MyHibernateTemplate extends HibernateTemplate implements Serializable { /** * */ private static final long serialVersionUID = -6309692794988265208L; public MyHibernateTemplate() { super(); } public MyHibernateTemplate(SessionFactory sessionFactory) { super(sessionFactory); } public MyHibernateTemplate(SessionFactory sessionFactory, boolean allowCreate) { super(sessionFactory, allowCreate); } } 这是我的配置文件(applicationContext-quartz.xml): <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="applicationContextSchedulerContextKey" value="applicationContextKey" /> <property name="configLocation" value="classpath:quartz.properties" /> <property name="autoStartup" value="true" /> </bean> <bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass"> <value>com.sundoctor.example.service.MyQuartzJobBean</value> </property> <property name="jobDataAsMap"> <map> <entry key="simpleService"> <ref bean="simpleService" /> </entry> </map> </property> </bean> <bean id="simpleService" class="com.sundoctor.example.service.SimpleService"> <property name="backupDao"> <ref bean="backupDao" /> </property> </bean> <bean id="schedulerService" class="com.sundoctor.quartz.service.SchedulerServiceImpl"> <property name="scheduler"> <ref bean="quartzScheduler" /> </property> <property name="jobDetail"> <ref local="jobDetail" /> </property> <property name="quartzDao"> <ref bean="quartzDao" /> </property> </bean> <!-- 容器启动的时候,; <bean id="simpleTrigger" class="com.internal.InitializingCronTrigger" lazy-init="false"> <property name="jobDetail" ref="jobDetail" /> <property name="schedulerService" ref="schedulerService" /> </bean> --> <bean id="myTemplate" class="com.dao.MyHibernateTemplate"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean name="/simpleServiceTest" class="com.sundoctor.example.test.SimpleServiceTest"> <property name="schedulerService"> <ref local="schedulerService" /> </property> </bean> </beans> 这是另一个配置文件(applicationContext.xml): <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" default-lazy-init="false"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value> </property> <property name="url"> <value> jdbc:sqlserver://localhost:1433;DatabaseName=test;SelectMethod=cursor </value> </property> <property name="username"> <value>sa</value> </property> <property name="password"> <value>sa</value> </property> <property name="maxIdle" value="30"></property> <property name="minIdle" value="5"></property> <!-- <property name="validationQuery" value="true"></property> <property name="loginTimeout" value="1800"></property> <property name="poolPreparedStatements" value="true"></property> --> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.SQLServerDialect </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.jdbc.fetch_size">50</prop> <prop key="hibernate.jdbc.batch_size">50</prop> </props> </property> <property name="mappingResources"> <list> <value>com/pojo/QrtzTriggerListeners.hbm.xml</value> <value>com/pojo/QrtzJobDetails.hbm.xml</value> <value>com/pojo/QrtzSchedulerState.hbm.xml</value> <value>com/pojo/QrtzFiredTriggers.hbm.xml</value> <value>com/pojo/QrtzPausedTriggerGrps.hbm.xml</value> <value>com/pojo/QrtzCronTriggers.hbm.xml</value> <value>com/pojo/QrtzJobListeners.hbm.xml</value> <value>com/pojo/QrtzBlobTriggers.hbm.xml</value> <value>com/pojo/QrtzLocks.hbm.xml</value> <value>com/pojo/QrtzSimpleTriggers.hbm.xml</value> <value>com/pojo/QrtzTriggers.hbm.xml</value> <value>com/pojo/QrtzCalendars.hbm.xml</value> </list> </property> </bean> <bean id="quartzDao" class="com.dao.QuartzDao"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> <bean id="backupDao" class="com.dao.BackupDao"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> </beans> InitializingCronTrigger类(容器启动的时候,读取tigger的信息,得到cronExpression): package com.internal; import java.text.ParseException; import org.springframework.scheduling.quartz.CronTriggerBean; import com.sundoctor.quartz.service.SchedulerService; public class InitializingCronTrigger extends CronTriggerBean { /** * */ private static final long serialVersionUID = 2608510718984483084L; private SchedulerService schedulerService; public void setSchedulerService(SchedulerService schedulerService) { this.schedulerService = schedulerService; String cronExpression = schedulerService.getExpression(this.getName(), this.getGroup()); if (cronExpression != null && !cronExpression.equals("")) { try { setCronExpression(cronExpression); } catch (ParseException e) { e.printStackTrace(); } } } } 楼主麻烦了,帮我看看,现在晕的不行,我不明白序列化有什么作用,加上序列化后为什么就不能注入了?还有帮我看看为什么WEB容器启动的会调用一次,怎么配置才是最好的配置,楼主太感谢了,这个模块我做了将近一个星期了,就是这个quartz框上卡住了,还有有楼主你啊。如果我的写乱的话,可以给我一个例子吗?582443115@qq.com,可以发到我的邮箱。感谢!· ] |
|
返回顶楼 | |
发表时间:2009-09-14
最后修改:2009-09-14
简单的说序列化就是一种用来处理对象流的机制,所谓对象流也就是将对象的内容进行流化。可以对流化后的对象进行读写操作,也可将流化后的对象传输于网络之间。序列化是为了解决在对对象流进行读写操作时所引发的问题。
序列化的实现:将需要被序列化的类实现Serializable接口,该接口没有需要实现的方法,implements Serializable只是为了标注该对象是可被序列化的,然后使用一个输出流(如:FileOutputStream)来构造一个ObjeCTOutputStream(对象流)对象,接着,使用ObjectOutputStream对象的writeObject(Object obj)方法就可以将参数为obj的对象写出(即保存其状态),要恢复的话则用输入流。 想要更详细的了解序列化,要google一下就有了。 yanyu510 写道 public class BackupDao implements Serializable { /** * */ private static final long serialVersionUID = 8933456923281729867L; private MyHibernateTemplate hibernateTemlate; private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } ... 我看你这段代码里并没有实例化MyHibernateTemplate,hibernateTemlate应该为null,应该这样写 引用 public class BackupDao implements Serializable { /** * */ private static final long serialVersionUID = 8933456923281729867L; private MyHibernateTemplate hibernateTemlate; public void setSessionFactory(SessionFactory sessionFactory) { this.hibernateTemlate = new MyHibernateTemplate(sessionFactory); } ... 就行了,注意红色地方,实例化MyHibernateTemplate。注入一个属性只要有set方法就行了,不一定要写属性(private SessionFactory sessionFactory; )。 如果你觉得方便,可以把你的代码发我(sundoctor@21cn.com),我帮你看看。 |
|
返回顶楼 | |
发表时间:2009-09-14
我把代码发过去,你给我看看吧。谢啦!
|
|
返回顶楼 | |
发表时间:2009-09-14
yanyu510 写道 我把代码发过去,你给我看看吧。谢啦!
可能是网络原因,还没有收到,你发另外一个邮箱(bowen868@163.com)试试 |
|
返回顶楼 | |
发表时间:2009-09-14
不知道什么原因,现在可能添加tigger了,然后HibernateTemplate也能得到值了,就是执行backupDateabase()这个方法的时候还抛异常:
2009-9-14 11:56:07 org.apache.catalina.core.AprLifecycleListener lifecycleEvent 信息: The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: D:\Program Files\MyEclipse 5.5 GA\bin;D:\Program Files\Apache Software Foundation\Tomcat 5.5\bin 2009-9-14 11:56:07 org.apache.coyote.http11.Http11BaseProtocol init 信息: Initializing Coyote HTTP/1.1 on http-8080 2009-9-14 11:56:07 org.apache.catalina.startup.Catalina load 信息: Initialization processed in 1172 ms 2009-9-14 11:56:08 org.apache.catalina.core.StandardService start 信息: Starting service Catalina 2009-9-14 11:56:08 org.apache.catalina.core.StandardEngine start 信息: Starting Servlet Engine: Apache Tomcat/5.5.20 2009-9-14 11:56:08 org.apache.catalina.core.StandardHost start 信息: XML validation disabled 2009-9-14 11:56:21 org.apache.coyote.http11.Http11BaseProtocol start 信息: Starting Coyote HTTP/1.1 on http-8080 2009-9-14 11:56:22 org.apache.jk.common.ChannelSocket init 信息: JK: ajp13 listening on /0.0.0.0:8009 2009-9-14 11:56:22 org.apache.jk.server.JkMain start 信息: Jk running ID=0 time=0/94 config=null 2009-9-14 11:56:22 org.apache.catalina.storeconfig.StoreLoader load 信息: Find registry server-registry.xml at classpath resource 2009-9-14 11:56:22 org.apache.catalina.startup.Catalina start 信息: Server startup in 14406 ms com.dao.MyHibernateTemplate@1f18cbe [2009-09-14 11:56:58]ERROR org.quartz.core.JobRunShell(line:211) -Job DEFAULT.jobDetail threw an unhandled Exception: java.lang.IllegalArgumentException: No SessionFactory specified at org.springframework.util.Assert.notNull(Assert.java:112) at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:281) at org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.java:233) at org.springframework.orm.hibernate3.HibernateTemplate.getSession(HibernateTemplate.java:425) at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:361) at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:338) at com.dao.BackupDao.backupDateabase(BackupDao.java:33) at com.sundoctor.example.service.SimpleService.testMethod(SimpleService.java:26) at com.sundoctor.example.service.MyQuartzJobBean.executeInternal(MyQuartzJobBean.java:22) at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86) at org.quartz.core.JobRunShell.run(JobRunShell.java:202) at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525) [2009-09-14 11:56:58]ERROR org.quartz.core.ErrorLogger(line:2185) -Job (DEFAULT.jobDetail threw an exception. org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: java.lang.IllegalArgumentException: No SessionFactory specified] at org.quartz.core.JobRunShell.run(JobRunShell.java:213) at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525) Caused by: java.lang.IllegalArgumentException: No SessionFactory specified at org.springframework.util.Assert.notNull(Assert.java:112) at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:281) at org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.java:233) at org.springframework.orm.hibernate3.HibernateTemplate.getSession(HibernateTemplate.java:425) at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:361) at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:338) at com.dao.BackupDao.backupDateabase(BackupDao.java:33) at com.sundoctor.example.service.SimpleService.testMethod(SimpleService.java:26) at com.sundoctor.example.service.MyQuartzJobBean.executeInternal(MyQuartzJobBean.java:22) at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86) at org.quartz.core.JobRunShell.run(JobRunShell.java:202) ... 1 more com.dao.MyHibernateTemplate@5364 [2009-09-14 11:57:00]ERROR org.quartz.core.JobRunShell(line:211) -Job DEFAULT.jobDetail threw an unhandled Exception: java.lang.IllegalArgumentException: No SessionFactory specified at org.springframework.util.Assert.notNull(Assert.java:112) at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:281) at org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.java:233) at org.springframework.orm.hibernate3.HibernateTemplate.getSession(HibernateTemplate.java:425) at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:361) at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:338) at com.dao.BackupDao.backupDateabase(BackupDao.java:33) at com.sundoctor.example.service.SimpleService.testMethod(SimpleService.java:26) at com.sundoctor.example.service.MyQuartzJobBean.executeInternal(MyQuartzJobBean.java:22) at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86) at org.quartz.core.JobRunShell.run(JobRunShell.java:202) at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525) [2009-09-14 11:57:00]ERROR org.quartz.core.ErrorLogger(line:2185) -Job (DEFAULT.jobDetail threw an exception. org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: java.lang.IllegalArgumentException: No SessionFactory specified] at org.quartz.core.JobRunShell.run(JobRunShell.java:213) at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525) Caused by: java.lang.IllegalArgumentException: No SessionFactory specified at org.springframework.util.Assert.notNull(Assert.java:112) at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:281) at org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.java:233) at org.springframework.orm.hibernate3.HibernateTemplate.getSession(HibernateTemplate.java:425) at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:361) at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:338) at com.dao.BackupDao.backupDateabase(BackupDao.java:33) at com.sundoctor.example.service.SimpleService.testMethod(SimpleService.java:26) at com.sundoctor.example.service.MyQuartzJobBean.executeInternal(MyQuartzJobBean.java:22) at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86) at org.quartz.core.JobRunShell.run(JobRunShell.java:202) ... 1 more |
|
返回顶楼 | |