浅谈spring时序调度Quartz

浅谈spring时序调度Quartz,第1张

近期接到一个开发任务 业务方面不再详述 要求每天定时执行一段应用程序用来同步俩个数据库

正好近期一直在用spring做相关开发 尝试了下Quartz 感觉不错 目前也正在学习中

Quartz使用Trigger Job以及JobDetail等对象来进行各种类型的任务调度 Spring对其提供了很好的支持

建立一个时序调度程序的过程如下 ) 配置Job及JobDetail Bean 定义执行某个类里的特定方法 ) 配置Trigger Bean 定义触发JobDetail的时间规律 ) 配置SchedulerFactoryBean负责调度实际的Trigger 时序调度的运行模式有两种 ) 一种是在某个特定时间自动运行 例如每天凌晨 点备份数据 每月初 号统计上月的数据等 我们称之为定时调度

)另一种是在服务启动一段时间后开始运行 再每隔一段时间再次运行 如系统监控程序每个 分钟就要测试一下数据库是否连接正常 我们称之为重复调度

下面举出两个简单的范例 详细原理和配置方法请参考 Spring 官方文档和程序范例的ApplicationContext quartz xml 文件

一 定时调度参数配置是在每天的 : 的时间自动执行CronSimpleJob java 类中的 executeInternal()方法 在系统控制台上输 出日志内容

ApplicationContext quartz xml内容

view plaincopy to clipboardprint    <bean id= scheduler class= springframework scheduling quartz SchedulerFactoryBean >

<property name= triggers >

<list>

<ref local= cronSimpleTrigger />

</list>

</property>

</bean>

! 定时简单JobDetail >

<bean id= cronSimpleJob class= springframework scheduling quartz JobDetailBean >

<property name= jobClass value= test quartz CronSimpleJob />

</bean>

<! 定时简单Trigger >

<bean id= cronSimpleTrigger class= springframework scheduling quartz CronTriggerBean >

<property name= jobDetail ref= cronSimpleJob />

<property name= cronExpression value= />

</bean>

<bean id= scheduler class= springframework scheduling quartz SchedulerFactoryBean >

<property name= triggers >

<list>

<ref local= cronSimpleTrigger />

</list>

</property>

</bean>

<! 定时简单JobDetail >

<bean id= cronSimpleJob class= springframework scheduling quartz JobDetailBean >

<property name= jobClass value= test quartz CronSimpleJob />

</bean>

<! 定时简单Trigger >

<bean id= cronSimpleTrigger class= springframework scheduling quartz CronTriggerBean >

<property name= jobDetail ref= cronSimpleJob />

<property name= cronExpression value= />

</bean>CronSimpleJob java文件内容

view plaincopy to clipboardprint    package test quartz;

import mons logging Log;

import mons logging LogFactory;

import quartz JobExecutionContext;

import springframework scheduling quartz QuartzJobBean;

public class CronSimpleJob extends QuartzJobBean

{

private final Log logger = LogFactory getLog(getClass());

protected void executeInternal(JobExecutionContext context)

{

logger warn(getClass() getName() + out it! );

}

}

package test quartz;

import mons logging Log;

import mons logging LogFactory;

import quartz JobExecutionContext;

import springframework scheduling quartz QuartzJobBean;

public class CronSimpleJob extends QuartzJobBean

{

private final Log logger = LogFactory getLog(getClass());

protected void executeInternal(JobExecutionContext context)

{

logger warn(getClass() getName() + out it! );

}

}

二 重复调度 view plaincopy to clipboardprint    在服务器启动 毫秒即 秒钟后 调度程序首次执行 以后每隔 毫秒即 分钟再次执行

在服务器启动 毫秒即 秒钟后 调度程序首次执行 以后每隔 毫秒即 分钟再次执行 view plaincopy to clipboardprint    自动执行RepeatMethodJob java类中的testRepeatMethod()方法 在系统控制台上输出日志内容

自动执行RepeatMethodJob java类中的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    ApplicationContext quartz xml内容

ApplicationContext quartz xml内容     view plaincopy to clipboardprint    <! 逻辑bean >

<bean id= repeatMethodJob class= test quartz RepeatMethodJob />

<! 间隔方法JobDetail >

<bean id= repeatMethodJobDetail class= springframework scheduling quartz MethodInvokingJobDetailFactoryBean >

<property name= targetObject ref= repeatMethodJob />

<property name= targetMethod value= testRepeatMethod />

<property name= concurrent value= false />

</bean>

<! 间隔方法Trigger >

<bean id= repeatMethodTrigger class= springframework scheduling quartz SimpleTriggerBean >

<property name= jobDetail ref= repeatMethodJobDetail />

<property name= startDelay value= />

<property name= repeatInterval value= />

</bean>

<! 逻辑bean >

<bean id= repeatMethodJob class= test quartz RepeatMethodJob />

<! 间隔方法JobDetail >

<bean id= repeatMethodJobDetail class= springframework scheduling quartz MethodInvokingJobDetailFactoryBean >

<property name= targetObject ref= repeatMethodJob />

<property name= targetMethod value= testRepeatMethod />

<property name= concurrent value= false />

</bean>

<! 间隔方法Trigger >

<bean id= repeatMethodTrigger class= springframework scheduling quartz SimpleTriggerBean >

<property name= jobDetail ref= repeatMethodJobDetail />

<property name= startDelay value= />

<property name= repeatInterval value= />

</bean>view plaincopy to clipboardprint    RepeatMethodJob java文件内容

RepeatMethodJob java文件内容 view plaincopy to clipboardprint    <PRE class=java name= code >package test quartz;

import mons logging Log;

import mons logging LogFactory;

public class RepeatMethodJob

{

private final Log logger = LogFactory getLog(getClass());

public void testRepeatMethod(){

logger warn(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= ffan= done ></PRE>    <PRE class=java name= code ><IMG alt= src= ffan= done ></PRE>

view plaincopy to clipboardprintpackage test quartz;     import mons logging Log;     import mons logging LogFactory;         public class RepeatMethodJob     {     private final Log logger = LogFactory getLog(getClass());         public void testRepeatMethod(){     logger warn(getClass() getName()+ out it! );     }     }  package test quartz;

import mons logging Log;

import mons logging LogFactory;

public class RepeatMethodJob

{

private final Log logger = LogFactory getLog(getClass());

public void testRepeatMethod(){

logger warn(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= ffan= done >    view plaincopy to clipboardprint<IMG alt= src= ffan= done >    view plaincopy to clipboardprint

lishixinzhi/Article/program/Java/ky/201311/28087

public class SpringWiredBean implements ApplicationContextAware {

private ApplicationContext context;

public void setApplicationContext(

orgspringframeworkcontextApplicationContext context)

throws BeansException {

thiscontext = context;

}

public void quartz() {

Map<String, Trigger> beans = context

getBeansOfType(orgquartzTriggerclass);

Set<String> set = beanskeySet();

for (String key : set) {

Trigger cronTrigger = beansget(key);

if (cronTrigger instanceof CronTrigger) {

CronTrigger _cronTrigger = (CronTrigger) cronTrigger;

} else if (cronTrigger instanceof SimpleTrigger) {

SimpleTrigger _cronTrigger = (SimpleTrigger) cronTrigger;

} else if (cronTrigger instanceof NthIncludedDayTrigger) {

NthIncludedDayTrigger _cronTrigger = (NthIncludedDayTrigger) cronTrigger;

}

}

}

}

以上就是关于浅谈spring时序调度Quartz全部的内容,包括:浅谈spring时序调度Quartz、spring中配置了quartz定时任务,怎么在后台用java代码获取定时任务的运行状态、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9720962.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存