Spring 3.X 定时器配置几种方式

1.【第一种使用方式】需要加入两段XML声明

xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd

 

 创建类

 

package org.wittysource.test.task;

public class TestTask {
    public void testShow(){
	System.out.println("Task run......");
    }	
}

 

定义Bean

 

<bean id="testTask" class="org.wittysource.task.TestTask"></bean>

<task:scheduled-tasks>  
    <task:scheduled ref="testTask" method="testShow" cron="0 46 14 * * ?"/>  
</task:scheduled-tasks> 

 

2.【第二种方式 extends QuartzJobBean】

 

<bean name="job.vgic" class="org.springframework.scheduling.quartz.JobDetailBean">
		<property name="jobClass" value="org.wittysource.task.TestTask" />
		<property name="jobDataAsMap">
			<map>
				<entry key="triggerMessage" value="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" />
			</map>
		</property>
	</bean>
	
	<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
		<property name="jobDetail" ref="job.vgic" />
		<property name="startDelay" value="10000"/>
		<!-- repeat every 10 seconds -->
		<property name="repeatInterval" value="1000"/>
	</bean>
	
	<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="job.vgic" />
		<!--                                   s m h d M -->
		<property name="cronExpression" value="0 32 16 * * ?" />
	</bean>
	
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>				
				<ref bean="cronTrigger" />
			</list>
		</property>
	</bean>
 
3.第三种方式
<bean id="obj" class="org.wittysource.test.TestBatch" />

	<bean id="jobDetail"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="obj" />
		<property name="targetMethod" value="doShow" />
		<property name="concurrent" value="true" />
	</bean>
	
	<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="jobDetail" />
		<!-- run every morning at 6 AM -->
		<property name="cronExpression" value="0 38 17 * * ?" />
	</bean>

	<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
		<!-- see the example of method invoking job above -->
		<property name="jobDetail" ref="jobDetail" />
		<!-- 10 seconds -->
		<property name="startDelay" value="10000" />
		<!-- repeat every 50 seconds -->
		<property name="repeatInterval" value="1000" />
	</bean>

	<!-- 指定触发器 -->
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>				
				<ref bean="cronTrigger" />
				<ref bean="simpleTrigger" />
			</list>
		</property>
	</bean>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值