package com.zoneland.quartz.support;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.StatefulJob;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.util.MethodInvoker;
/**
* This is a cluster safe Quartz/Spring FactoryBean implementation, which produces a JobDetail implementation that can invoke any no-arg method on any Class.
* <p>
* Use this Class instead of the MethodInvokingJobDetailBeanFactory Class provided by Spring when deploying to a web environment like Tomcat.
* <p>
* <b>Implementation</b><br>
* Instead of associating a MethodInvoker with a JobDetail or a Trigger object, like Spring's MethodInvokingJobDetailFactoryBean does, I made the [Stateful]MethodInvokingJob, which is not persisted in the database, create the MethodInvoker when the [Stateful]MethodInvokingJob is created and executed.
* <p>
* A method can be invoked one of several ways:
* <ul>
* <li>The name of the Class to invoke (targetClass) and the static method to invoke (targetMethod) can be specified.
* <li>The Object to invoke (targetObject) and the static or instance method to invoke (targetMethod) can be specified (the targetObject must be Serializable when concurrent=="false").
* <li>The Class and static Method to invoke can be specified in one property (staticMethod). example: staticMethod = "example.ExampleClass.someStaticMethod"
* <br><b>Note:</b> An Object[] of method arguments can be specified (arguments), but the Objects must be Serializable if concurrent=="false".
* </ul>
* <p>
* I wrote MethodInvokingJobDetailFactoryBean, because Spring's MethodInvokingJobDetailFactoryBean does not produce Serializable
* JobDetail objects, and as a result cannot be deployed into a clustered environment like Tomcat (as is documented within the Class).
* <p>
* <b>Example</b>
* <code>
* <ul>
* <bean id="<i>exampleTrigger</i>" class="org.springframework.scheduling.quartz.CronTriggerBean">
* <ul>
<i><!-- Execute example.ExampleImpl.fooBar() at 2am every day --></i><br>
<property name="<a href="http://www.opensymphony.com/quartz/api/org/quartz/CronTrigger.html">cronExpression</a>" value="0 0 2 * * ?" /><br>
<property name="jobDetail">
<ul>
<bean class="frameworkx.springframework.scheduling.quartz.<b>MethodInvokingJobDetailFactoryBean</b>">
<ul>
<property name="concurrent" value="<i>false</i>"/><br>
<property name="targetClass" value="<i>example.ExampleImpl</i>" /><br>
<property name="targetMethod" value="<i>fooBar</i>" />
</ul>
</bean>
</ul>
</property>
</ul>
</bean>
<p>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<ul>
<property name="triggers">
<ul>
<list>
<ul>
<ref bean="<i>exampleTrigger</i>" />
</ul>
</list>
</ul>
</property>
</ul>
</bean>
</ul>
* </code>
* In this example we created a MethodInvokingJobDetailFactoryBean, which will produce a JobDetail Object with the jobClass property set to StatefulMethodInvokingJob.class (concurrent=="false"; Set to MethodInvokingJob.class when concurrent=="true"), which will in turn invoke the static <code>fooBar</code>() method of the "<code>example.ExampleImpl</code>" Class. The Scheduler is the heart of the whole operation; without it, nothing will happen.
* <p>
* For more information on <code>cronExpression</code> syntax visit <a href="http://www.opensymphony.com/quartz/api/org/quartz/CronTrigger.html">http://www.opensymphony.com/quartz/api/org/quartz/CronTrigger.html</a>
*
* @author Stephen M. Wick
*
* @see #afterPropertiesSet()
*/
public class MethodInvokingJobDetailFactoryBean implements FactoryBean, BeanNameAware, InitializingBean
{
private Log logger = LogFactory.getLog(getClass());
/**
* The JobDetail produced by the <code>afterPropertiesSet</code> method of this Class will be assigned to the Group specified by this property. Default: Scheduler.DEFAULT_GROUP
* @see #afterPropertiesSet()
* @see Scheduler#DEFAULT_GROUP
*/
private String group = Scheduler.DEFAULT_GROUP;
/**
* Indicates whether or not the Bean Method should be invoked by more than one Scheduler at the specified time (like when deployed to a cluster, and/or when there are multiple Spring ApplicationContexts in a single JVM<i> - Tomcat 5.5 creates 2 or more instances of the DispatcherServlet (a pool), which in turn creates a separate Spring ApplicationContext for each instance of the servlet</i>)
* <p>
* Used by <code>afterPropertiesSet</code> to set the JobDetail.jobClass to MethodInvokingJob.class or StatefulMethodInvokingJob.class when true or false, respectively. Default: true
* @see #afterPropertiesSet()
*/
private boolean concurrent = true;
/** Used to set the JobDetail.durable property. Default: false
* <p>Durability - if a job is non-durable, it is automatically deleted from the scheduler once there are no longer any active triggers associated with it.
* @see <a href="http://www.opensymphony.com/quartz/wikidocs/TutorialLesson3.html">http://www.opensymphony.com/quartz/wikidocs/TutorialLesson3.html</a>
* @see #afterPropertiesSet()
*/
private boolean durable = false;
/**
* Used by <code>afterPropertiesSet</code> to set the JobDetail.volatile property. Default: false
* <p>Volatility - if a job is volatile, it is not persisted between re-starts of the Quartz scheduler.
* <p>I set the default to false to be the same as the default for a Quartz Trigger. An exception is thrown
* when the Trigger is non-volatile and the Job is volatile. If you want volatility, then you must set this property, and the Trigger's volatility property, to true.
* @see <a href="http://www.opensymphony.com/quartz/wikidocs/TutorialLesson3.html">http://www.opensymphony.com/quartz/wikidocs/TutorialLesson3.html</a>
* @see #afterPropertiesSet()
*/
private boolean volatility = false;
/**
* Used by <code>afterPropertiesSet</code> to set the JobDetail.requestsRecovery property. Default: false<BR>
* <p>RequestsRecovery - if a job "requests recovery", and it is executing during the time of a 'hard shutdown' of the scheduler (i.e. the process it is running within crashes, or the machine is shut off), then it is re-executed when the scheduler is started again. In this case, the JobExecutionContext.isRecovering() method will return true.
* @see <a href="http://www.opensymphony.com/quartz/wikidocs/TutorialLesson3.html">http://www.opensymphony.com/quartz/wikidocs/TutorialLesson3.html</a>
* @see #afterPropertiesSet()
*/
private boolean shouldRecover = false;
/**
* A list of names of JobListeners to associate with the JobDetail object created by this FactoryBean.
*
* @see #afterPropertiesSet()
**/
private String[] jobListenerNames;
/** The name assigned to this bean in the Spring ApplicationContext.
* Used by <code>afterPropertiesSet</code> to set the JobDetail.name property.
* @see afterPropertiesSet()
* @see JobDetail#setName(String)
**/
private String beanName;
/**
* The JobDetail produced by the <code>afterPropertiesSet</code> method, and returned by the <code>getObject</code> method of the Spring FactoryBean interface.
* @see #afterPropertiesSet()
* @see #getObject()
* @see FactoryBean
**/
private JobDetail jobDetail;
/**
* The name of the Class to invoke.
**/
private String targetClass;
/**
* The Object to invoke.
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
实例是基于MyEclipse开发的。针对Quartz与Spring做集群的Demo实例,主要解决了Quartz的JOB序列化问题。 源代码说明: support pkg:扩展Spring与Quartz集成的不足之处。 core pkg: 是自身调度业务的封装 实例运行依赖Oracle数据库,根据quartz框架包docs/dbTables/目录下的sql脚本文件,创建表结构,修改工程中的Spring数据源配置。 Start.java启动程序。
资源推荐
资源详情
资源评论
收起资源包目录
QuartzClusterTest.rar (43个子文件)
QuartzClusterTest
bin
quartz.properties 3KB
com
zoneland
quartz
core
CustService.class 906B
MyBusiJob.class 991B
support
MethodInvokingJobDetailFactoryBean$StatefulMethodInvokingJob.class 684B
MethodInvokingJobDetailFactoryBean$MethodInvokingJob.class 3KB
MethodInvokingJobDetailFactoryBean.class 5KB
Job2.class 177B
BootstrapJob.class 1KB
Start.class 537B
log4j.xml 514B
applicationContext-quartz.xml 2KB
src
quartz.properties 3KB
com
zoneland
quartz
core
MyBusiJob.java 504B
CustService.java 380B
support
MethodInvokingJobDetailFactoryBean.java 20KB
Job2.java 712B
BootstrapJob.java 676B
Start.java 365B
log4j.xml 514B
applicationContext-quartz.xml 2KB
.project 393B
.classpath 2KB
lib
org.springframework.beans-3.0.0.RELEASE.jar 539KB
log4j-1.2.14.jar 359KB
org.springframework.web-3.0.0.RELEASE.jar 351KB
db2jcc_javax.jar 23KB
commons-pool-1.5.4.jar 94KB
org.springframework.transaction-3.0.0.RELEASE.jar 226KB
org.springframework.core-3.0.0.RELEASE.jar 351KB
org.springframework.asm-3.0.0.RELEASE.jar 52KB
org.springframework.jdbc-3.0.0.RELEASE.jar 373KB
quartz-all-1.8.3.jar 450KB
db2jcc.jar 1.97MB
slf4j-log4j12-1.5.10.jar 9KB
org.springframework.context.support-3.0.0.RELEASE.jar 99KB
slf4j-api-1.5.10.jar 23KB
org.springframework.aop-3.0.0.RELEASE.jar 317KB
db2jcc_license_cu.jar 1015B
commons-logging-1.1.jar 52KB
org.springframework.expression-3.0.0.RELEASE.jar 151KB
commons-dbcp-1.2.2.jar 119KB
classes12.jar 1.53MB
org.springframework.context-3.0.0.RELEASE.jar 642KB
共 43 条
- 1
baoziyatou
- 粉丝: 18
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页