首页编程schedulerfactorybean(spring 4 quartz 2.x动态配置triggers)

schedulerfactorybean(spring 4 quartz 2.x动态配置triggers)

编程之家2023-11-0669次浏览

大家好,关于schedulerfactorybean很多朋友都还不太明白,不过没关系,因为今天小编就来为大家分享关于spring 4 quartz 2.x动态配置triggers的知识点,相信应该可以解决大家的一些困惑和问题,如果碰巧可以解决您的问题,还望关注下本站哦,希望对各位有所帮助!

schedulerfactorybean(spring 4 quartz 2.x动态配置triggers)

spring 4 quartz 2.x动态配置triggers

spring 4.x没有配置过,这里有一个spring 3.x的quartz定时配置,你参考参考:

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

<!--

schedulerfactorybean(spring 4 quartz 2.x动态配置triggers)

1、JobDetail:JobDetail是一个具体的类。

2、Trigger:触发器,它用于定义Job何时执行。最常用的是SimpleTrigger和CronTrigger。

一般来说,如果你需要在一个固定的时间和重复次数或者一个固定的间隔时间,那么SimpleTrigger比较合适;

如果你有许多复杂的作业调度,那么CronTrigger比较合适。

CronTrigger和Unix的cron机制基本一样,我们需要的只是一个cron表达式。

比如“0012**?”会在每天中午12点触发执行;“01510?*6L”会在每个月的最后一个星期五的早上10:15触发Job执行。

schedulerfactorybean(spring 4 quartz 2.x动态配置triggers)

3、Scheduler和SchedulerFactory:Scheduler负责管理Trigger、调度Job,

SchedulerFactory则是Scheduler工厂,负责生成Scheduler。

-->

<!--总管理类如果将lazy-init='false'那么容器启动就会执行调度程序-->

<beanclass="org.springframework.scheduling.quartz.SchedulerFactoryBean"lazy-init="false">

<!--通过applicationContextSchedulerContextKey属性配置spring上下文[此属性可省略]-->

<propertyname="applicationContextSchedulerContextKey">

<value>applicationContext</value>

</property>

<propertyname="triggers">

<list>

<!--作业调度器,list下可加入其他的调度器-->

<refbean="testTrigger"/>

</list>

</property>

<propertyname="autoStartup"value="true"/>

</bean>

<!--要调度的对象-->

<beanid="testJob"class="name.zealze.test.JobTest"/>

<beanid="testTrigger"class="org.springframework.scheduling.quartz.CronTriggerBean">

<propertyname="jobDetail"ref="testJobDetail"/>

<!--按cron表达式时间点触发事件http://cron.qqe2.com/-->

<propertyname="cronExpression"value="00/2***?"/>

</bean>

<beanid="testJobDetail"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

<propertyname="targetObject"ref="testJob"/>

<propertyname="targetMethod"value="execute"/>

<!--是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程-->

<propertyname="concurrent"value="false"/>

</bean>

</beans>

quartz定时任务一般都是调用service么

spring的配置文件applicationContext.xml——省略spring的相关配置:

Xml代码

<!--定时任务-->

<!--加入定时任务类-->

<bean id="hand" class="com.fms.web.action.CronJobHand"></bean>

<!--任务调度拦截-->

<bean id="jobHand" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

<property name="targetObject">

<ref bean="hand"/><!--指定具体拦截哪个定时任务-->

</property>

<property name="targetMethod">

<value>jobHand</value><!--指定要执行的类里面的哪个方法-->

</property>

</bean>

<!--用cron表达式定义定时任务执行时间-->

<bean id="dohand" class="org.springframework.scheduling.quartz.CronTriggerBean">

<property name="jobDetail">

<ref bean="jobHand"/><!--指明要调用哪个任务-->

</property>

<!-- cron表达式-->

<property name="cronExpression">

<!--每天10点15点执行一次-->

<value>0 0 10,15,17**?</value>

</property>

</bean>

<bean id="jobCard" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

<property name="targetObject">

<ref bean="hand"/>

</property>

<property name="targetMethod">

<value>jobCard</value>

</property>

</bean>

<bean id="docard" class="org.springframework.scheduling.quartz.CronTriggerBean">

<property name="jobDetail">

<ref bean="jobCard"/>

</property>

<!-- cron表达式-->

<property name="cronExpression">

<!--每天9:30和14:30执行一次-->

<value>0 30 9,14,16**?</value>

</property>

</bean>

<!--让spring来自动管理quartz-->

<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

<property name="triggers">

<list>

<ref bean="dohand"/>

<ref bean="docard"/>

</list>

</property>

</bean>

定时任务类:

补充一点:spring配置文件里面要有context:component-scan扫描到你的定时任务类

而且在类上加@Component就行了。

我的spring版本是2.5,3.X以上更简单。

好了,本文到此结束,如果可以帮助到大家,还望关注本站哦!

initialcontext?InitialContext()是什么意思成都app开发多少钱(开发一个app多少钱)