Spring+Quartz定时任务
2009-09-21 00:00:00 来源:WEB开发网最近研究站内搜索,因为要定时的更新索引库,看了看Spring+Quartz定时任务,用它完成,定时创建索引的任务!!
给大家分享一下helloworld的简单例子,大家可以根据实际情况变化使用
业务方法类
Java代码
package com.task;
/**
* 业务方法
*
*/
public class TestJob {
public void execute() {
try {
System.out.println("我的业务方法被调用了---------!");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
配置文件beans.xml
Java代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- 普通的业务Bean -->
<bean id="testJob" class="com.task.TestJob" />
<!-- 触发器 -->
<bean
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="testTrigger" />
</list>
</property>
<property name="autoStartup" value="true" />
</bean>
<bean id="testTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="testJobDetail" />
<!-- 每隔1秒钟触发一次 -->
<property name="cronExpression" value="*/1 * * * * ?" />
</bean>
<!-- 计划 -->
<bean id="testJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="testJob" />
<property name="targetMethod" value="execute" />
<!-- 是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 -->
<property name="concurrent" value="false" />
</bean>
</beans>
赞助商链接