您的位置:首页 > 编程语言 > Java开发

Spring任务调度,Quartz Scheduler

2011-04-20 14:04 429 查看
新建bean.xml文件,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> 
<!-- 定义一个任务调度器 -->
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="estimateLogTriggerQuartz"/>
</list>
</property>
</bean>

<!-- 配置触发器调度的bean -->
<bean id="estimateLog" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 引用一个bean -->
<property name="targetObject" ref="estimateLogTrigger"/>
<!-- 指定目标bean的方法 -->
<property name="targetMethod" value="doSaveEstimateLog"/>
</bean>

<!-- 配置触发器 -->
<bean id="estimateLogTriggerQuartz" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="estimateLog"/>
<!-- 每隔3秒执行一次 -->
<property name="cronExpression" value="0/3 * * * * ?"/>
</bean>

<!-- 被调度的bean -->
<bean id="estimateLogTrigger" class="mypack.EstimateLogTrigger">
</bean>
</beans>


新建类EstimateLogTrigger,代码如下:

package mypack;

public class EstimateLogTrigger {

public void doSaveEstimateLog(){
System.out.println("test...");
}
}


新建测试类,代码如下:

package mypack;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestMain {

public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"bean.xml"});
}
}


测试执行。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: