近期接到一个开发任务业务方面不再详述要求每天定时执行一段应用程序用来同步俩个数据库
正好近期一直在用spring做相关开发尝试了下Quartz感觉不错目前也正在学习中
Quartz使用TriggerJob以及JobDetail等对象来进行各种类型的任务调度Spring对其提供了很好的支持
建立一个时序调度程序的过程如下 ) 配置Job及JobDetail Bean定义执行某个类里的特定方法 ) 配置Trigger Bean定义触发JobDetail的时间规律 ) 配置SchedulerFactoryBean负责调度实际的Trigger时序调度的运行模式有两种 ) 一种是在某个特定时间自动运行例如每天凌晨点备份数据每月初号统计上月的数据等我们称之为定时调度
)另一种是在服务启动一段时间后开始运行再每隔一段时间再次运行如系统监控程序每个分钟就要测试一下数据库是否连接正常我们称之为重复调度
下面举出两个简单的范例详细原理和配置方法请参考 Spring 官方文档和程序范例的ApplicationContextquartzxml 文件
一定时调度参数配置是在每天的:的时间自动执行CronSimpleJobjava 类中的 executeInternal()方法在系统控制台上输 出日志内容
ApplicationContextquartzxml内容
view plaincopy to clipboardprint?
<bean id=scheduler class=orgspringframeworkschedulingquartzSchedulerFactoryBean>
<property name=triggers>
<list>
<ref local=cronSimpleTrigger/>
</list>
</property>
</bean>
!定时简单JobDetail>
<bean id=cronSimpleJob class=orgspringframeworkschedulingquartzJobDetailBean>
<property name=jobClass value=testquartzCronSimpleJob/>
</bean>
<!定时简单Trigger>
<bean id=cronSimpleTrigger class=orgspringframeworkschedulingquartzCronTriggerBean>
<property name=jobDetail ref=cronSimpleJob/>
<property name=cronExpression value= * * ?/>
</bean>
<bean id=scheduler class=orgspringframeworkschedulingquartzSchedulerFactoryBean>
<property name=triggers>
<list>
<ref local=cronSimpleTrigger/>
</list>
</property>
</bean>
<!定时简单JobDetail>
<bean id=cronSimpleJob class=orgspringframeworkschedulingquartzJobDetailBean>
<property name=jobClass value=testquartzCronSimpleJob/>
</bean>
<!定时简单Trigger>
<bean id=cronSimpleTrigger class=orgspringframeworkschedulingquartzCronTriggerBean>
<property name=jobDetail ref=cronSimpleJob/>
<property name=cronExpression value= * * ?/>
</bean>CronSimpleJobjava文件内容
view plaincopy to clipboardprint?
package testquartz;
import monsloggingLog;
import monsloggingLogFactory;
import orgquartzJobExecutionContext;
import orgspringframeworkschedulingquartzQuartzJobBean;
public class CronSimpleJob extends QuartzJobBean
{
private final Log logger = LogFactorygetLog(getClass());
protected void executeInternal(JobExecutionContext context)
{
loggerwarn(getClass()getName() + out it!);
}
}
package testquartz;
import monsloggingLog;
import monsloggingLogFactory;
import orgquartzJobExecutionContext;
import orgspringframeworkschedulingquartzQuartzJobBean;
public class CronSimpleJob extends QuartzJobBean
{
private final Log logger = LogFactorygetLog(getClass());
protected void executeInternal(JobExecutionContext context)
{
loggerwarn(getClass()getName() + out it!);
}
}
二重复调度view plaincopy to clipboardprint?
在服务器启动毫秒即秒钟后调度程序首次执行以后每隔毫秒即分钟再次执行
在服务器启动毫秒即秒钟后调度程序首次执行以后每隔毫秒即分钟再次执行view plaincopy to clipboardprint?
自动执行RepeatMethodJobjava类中的testRepeatMethod()方法在系统控制台上输出日志内容
自动执行RepeatMethodJobjava类中的testRepeatMethod()方法在系统控制台上输出日志内容view plaincopy to clipboardprint?
对于相同的JobDetail当指定多个Trigger时 很可能第一个job完成之前第二个job就开始了
对于相同的JobDetail当指定多个Trigger时 很可能第一个job完成之前第二个job就开始了view plaincopy to clipboardprint?
指定concurrent设为false多个job不会并发执行第二个job将不会在第一个job完成之前开始
指定concurrent设为false多个job不会并发执行第二个job将不会在第一个job完成之前开始view plaincopy to clipboardprint?
ApplicationContextquartzxml内容
ApplicationContextquartzxml内容
view plaincopy to clipboardprint?
<!逻辑bean>
<bean id=repeatMethodJob class=testquartzRepeatMethodJob/>
<!间隔方法JobDetail>
<bean id=repeatMethodJobDetail class=orgspringframeworkschedulingquartzMethodInvokingJobDetailFactoryBean>
<property name=targetObject ref=repeatMethodJob/>
<property name=targetMethod value=testRepeatMethod/>
<property name=concurrent value=false/>
</bean>
<!间隔方法Trigger>
<bean id=repeatMethodTrigger class=orgspringframeworkschedulingquartzSimpleTriggerBean>
<property name=jobDetail ref=repeatMethodJobDetail/>
<property name=startDelay value=/>
<property name=repeatInterval value=/>
</bean>
<!逻辑bean>
<bean id=repeatMethodJob class=testquartzRepeatMethodJob/>
<!间隔方法JobDetail>
<bean id=repeatMethodJobDetail class=orgspringframeworkschedulingquartzMethodInvokingJobDetailFactoryBean>
<property name=targetObject ref=repeatMethodJob/>
<property name=targetMethod value=testRepeatMethod/>
<property name=concurrent value=false/>
</bean>
<!间隔方法Trigger>
<bean id=repeatMethodTrigger class=orgspringframeworkschedulingquartzSimpleTriggerBean>
<property name=jobDetail ref=repeatMethodJobDetail/>
<property name=startDelay value=/>
<property name=repeatInterval value=/>
</bean>view plaincopy to clipboardprint?
RepeatMethodJobjava文件内容
RepeatMethodJobjava文件内容view plaincopy to clipboardprint?
<PRE class=java name=code>package testquartz;
import monsloggingLog;
import monsloggingLogFactory;
public class RepeatMethodJob
{
private final Log logger = LogFactorygetLog(getClass());
public void testRepeatMethod(){
loggerwarn(getClass()getName()+out it!);
}
}
</PRE>
<PRE class=java name=code> </PRE>
<PRE class=java name=code> </PRE>
<PRE class=java name=code>关于cronExpression</PRE>
<PRE class=java name=code><IMG height= alt= src= width= twffan=done></PRE>
<PRE class=java name=code><IMG alt= src= twffan=done></PRE>
view plaincopy to clipboardprint?package testquartz; import monsloggingLog; import monsloggingLogFactory; public class RepeatMethodJob { private final Log logger = LogFactorygetLog(getClass()); public void testRepeatMethod(){ loggerwarn(getClass()getName()+out it!); } } package testquartz;
import monsloggingLog;
import monsloggingLogFactory;
public class RepeatMethodJob
{
private final Log logger = LogFactorygetLog(getClass());
public void testRepeatMethod(){
loggerwarn(getClass()getName()+out it!);
}
}
view plaincopy to clipboardprint?
view plaincopy to clipboardprint?
view plaincopy to clipboardprint?关于cronExpression 关于cronExpression
view plaincopy to clipboardprint?<IMG height= alt= src= width= twffan=done>
view plaincopy to clipboardprint?<IMG alt= src= twffan=done>
view plaincopy to clipboardprint?
view plaincopy to clipboardprint?
关于Quartz详解可参考<A >>;/A>写的比较深入