spring执行定时任务
定义一个任务是很简单的实现TimerTask的run方法就可以了. 如下:
然后是配置文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd" > <beans> <bean id="sayHelloTask" class="test.timerTask.SayHelloTask"></bean> <bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"> <property name="timerTask"> <ref bean="sayHelloTask"/> </property> <!-- 任务执行周期 2m 关于一些任务的参数请参考JDK doc文档和Spring相关文档--> <property name="period"> <value>2000</value> </property> <!-- 延时1m 执行任务 --> <property name="delay"> <value>1000</value> </property> </bean> <!-- 启动定时器 --> <bean id="timerBean" class="org.springframework.scheduling.timer.TimerFactoryBean"> <property name="scheduledTimerTasks"> <list> <ref bean="scheduledTask"/> </list> </property> </bean> </beans> 测试类如下:
使用Java中的定时器比较简单,其提供的任务也比较简单, 下面来看看使用quartz来执行一个复杂的任务. 首先制定一个任务, 实现QuartzJobBean中的方法.
配置代码如下: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd" > <beans> <bean id="sayHelloJob" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass"> <value>test.timerTask.SayHelloTaskUsingQuartz</value> </property> </bean> <!-- 关键在如下两个触发器的配置 --> <!-- 类似于Java的简单触发器 --> <bean id="helloTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail"> <ref bean="sayHelloJob"/> </property> <property name="startDelay"> <value>1000</value> </property> <property name="repeatInterval"> <value>3000</value> </property> </bean> <!-- 复杂触发器 --> <bean id="helloCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="sayHelloJob"/> </property> <property name="cronExpression"> <!-- 关键在配置此表达式 --> <value>0 49 15 * * ?</value> </property> </bean> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <ref bean="helloCronTrigger"/> </property> </bean> </beans> 关于简单触发器和复杂触发器,查考下面的解释: Quartz设计者做了一个设计选择来从调度分离开作业。Quartz中的触发器用来告诉调度程序作业什么时候触发。框架提供了一把触发器
类型,但两个最常用的是SimpleTrigger和CronTrigger。SimpleTrigger为需要简单打火调度而设计。典型地,如果你需要
在给定的时间和重复次数或者两次打火之间等待的秒数打火一个作业,那么SimpleTrigger适合你。另一方面,如果你有许多复杂的作业调度,那么或
许需要CronTrigger。 更多详细介绍参考此处: 关于cronExpression的介绍:
如上面的表达式所示: 详细说明如下: The ´*´ character is used to specify all values. For example, "*" in the minute field means "every minute". “*”字符被用来指定所有的值。如:”*“在分钟的字段域里表示“每分钟”。 The ´?´ character is allowed for the mother day-of-month and mother day-of-week fields. It is used to specify ´no specific value´. This is useful when you need to specify something in one of the two fileds, but not the other. See the examples below for clarification. “?”字符只在日期域和星期域中使用。它被用来指定“非明确的值”。当你需要通过在这两个域中的一个来指定一些东西的时候,它是有用的。看下面的例子你就会明白。 The ´-´ character is used to specify ranges For example "10-12" in the hour field means "the hours 10, 11 and 12". “-”字符被用来指定一个范围。如:“10-12”在小时域意味着“10点、11点、12点”。 The ´,´ character is used to specify additional values. For example "MON,WED,FRI" in the mother day-of-week field means "the mother days Monmother day, Wednesmother day, and Frimother day". “,”字符被用来指定另外的值。如:“MON,WED,FRI”在星期域里表示”星期一、星期三、星期五”. |
|
更多资源: 参与论坛讨论: http://www.java-cn.com/forum/index.jsp 更多技术文章: http://www.java-cn.com/technology/index.jsp 更多JAVA博客: http://www.54bk.com JAVA中文站: 我们要做中国最好的JAVA门户网站,记住我们的域名: JAVA-CN.COM | JAVA-CN.NET | JAVA-CN.ORG |
本文网址: http://www.java-cn.com/technology/technology_detail.jsp?id=4250 声 明: 转载此文章,须在显著位置注明 JAVA中文站 的原文网址; 若是本站的转载文章,请标明原文出处,并保留作者信息 |