原文: https://howtodoinjava.com/spring-core/spring-timer-tasks/
Timer是一种实用程序类,用于安排一次和重复执行的任务。 通过计时器任务,Spring 框架为执行调度定期执行多次甚至是一次执行的任务提供支持。
还有其他方法可以在 Java 中实现调度程序功能,例如使用执行程序服务或使用 Quartz 等第三方 API 来无限循环地运行线程。 Timer恰好是其中之一。
请注意,Timer取决于系统时钟,因此将系统时钟设置为 n 小时将使下一次执行计时器任务的时间也减少 n 小时。
在 Spring 中,有两种使用计时器任务的方法:
- 配置
MethodInvokingTimerTaskFactoryBean - 扩展
java.util.TimerTask
让我们使用示例演示每个用法。
1. 配置MethodInvokingTimerTaskFactoryBean
在此方法中,在org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean定义中配置了计时器任务 bean 及其内部要执行的方法。 这是一个工厂 Bean,它公开 TimerTask 对象,该对象将作业执行委派给指定的(静态或非静态)方法。 这避免了实现仅调用现有业务方法的单行TimerTask的需要。
Spring 配置示例如下所示:
applicationContext.xml
<beans><bean id="demoTimerTask" class="com.howtodoinjava.task.DemoTimerTask"></bean><bean id="timerTaskFactoryBean"class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean"><property name="targetObject" ref="demoTimerTask"></property><property name="targetMethod" value="execute"></property></bean><bean id="scheduledTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"><property name="timerTask" ref="timerTaskFactoryBean"></property><property name="period" value="5000"></property></bean><bean class="org.springframework.scheduling.timer.TimerFactoryBean"><property name="scheduledTimerTasks"><list><ref local="scheduledTimerTask"></ref></list></property></bean></beans>
上面的计时器任务将获得executed in every 5 seconds。 让我们写出演示计时器任务并对其进行测试。
DemoTimerTask.java
package com.howtodoinjava.task;import java.util.Date;/*** No need to implement any interface* */public class DemoTimerTask {//Define the method to be called as configuredpublic void execute(){System.out.println("Executed task on :: " + new Date());}}
现在,让我们测试计时器任务。
TestDemoTimerTask.java
package com.howtodoinjava.timer;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestDemoTimerTask {public static void main(String[] args) {new ClassPathXmlApplicationContext("application-config.xml");}}
Console
Executed task on :: Mon Apr 22 09:53:39 IST 2017Executed task on :: Mon Apr 22 09:53:44 IST 2017
2. 扩展java.util.TimerTask
通过这种方式,我们通过扩展 java.util.TimerTask 来定义定时器任务,并将其传递给 spring 配置以便重复执行。
让我们来看看如何做:
applicationContext.xml
<beans><bean id="demoTimerTask" class="com.howtodoinjava.task.DemoTimerTask2"></bean><bean id="scheduledTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"><!-- run every 3 secs --><property name="period" value="3000"></property><property name="timerTask" ref="demoTimerTask"></property></bean><bean class="org.springframework.scheduling.timer.TimerFactoryBean"><property name="scheduledTimerTasks"><list><ref local="scheduledTimerTask"></ref></list></property></bean></beans>
以上任务将得到executed in every 3 seconds。 让我们从 java 提供的TimerTask中扩展计时器任务。
DemoTimerTask2.java
package com.howtodoinjava.task;import java.util.Date;import java.util.TimerTask;public class DemoTimerTask2 extends TimerTask{public void run(){System.out.println("DemoTimerTask2 running at: "+ new Date(this.scheduledExecutionTime()));}}
让我们测试配置:
TestDemoTimerTask2.java
package com.howtodoinjava.timer;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestDemoTimerTask2{public static void main(String[] args){new ClassPathXmlApplicationContext("application-config2.xml");}}
Console
DemoTimerTask2 running at: Mon Apr 22 10:01:33 IST 2013DemoTimerTask2 running at: Mon Apr 22 10:01:36 IST 2013DemoTimerTask2 running at: Mon Apr 22 10:01:39 IST 2013
学习愉快!
