原文: https://howtodoinjava.com/spring-core/spring-timer-tasks/

Timer是一种实用程序类,用于安排一次和重复执行的任务。 通过计时器任务,Spring 框架为执行调度定期执行多次甚至是一次执行的任务提供支持。

还有其他方法可以在 Java 中实现调度程序功能,例如使用执行程序服务或使用 Quartz 等第三方 API 来无限循环地运行线程。 Timer恰好是其中之一。

请注意,Timer取决于系统时钟,因此将系统时钟设置为 n 小时将使下一次执行计时器任务的时间也减少 n 小时。

在 Spring 中,有两种使用计时器任务的方法:

  1. 配置MethodInvokingTimerTaskFactoryBean
  2. 扩展java.util.TimerTask

让我们使用示例演示每个用法。

1. 配置MethodInvokingTimerTaskFactoryBean

在此方法中,在org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean定义中配置了计时器任务 bean 及其内部要执行的方法。 这是一个工厂 Bean,它公开 TimerTask 对象,该对象将作业执行委派给指定的(静态或非静态)方法。 这避免了实现仅调用现有业务方法的单行TimerTask的需要。

Spring 配置示例如下所示:

applicationContext.xml

  1. <beans>
  2. <bean id="demoTimerTask" class="com.howtodoinjava.task.DemoTimerTask"></bean>
  3. <bean id="timerTaskFactoryBean"
  4. class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
  5. <property name="targetObject" ref="demoTimerTask"></property>
  6. <property name="targetMethod" value="execute"></property>
  7. </bean>
  8. <bean id="scheduledTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
  9. <property name="timerTask" ref="timerTaskFactoryBean"></property>
  10. <property name="period" value="5000"></property>
  11. </bean>
  12. <bean class="org.springframework.scheduling.timer.TimerFactoryBean">
  13. <property name="scheduledTimerTasks">
  14. <list>
  15. <ref local="scheduledTimerTask"></ref>
  16. </list>
  17. </property>
  18. </bean>
  19. </beans>

上面的计时器任务将获得executed in every 5 seconds。 让我们写出演示计时器任务并对其进行测试。

DemoTimerTask.java

  1. package com.howtodoinjava.task;
  2. import java.util.Date;
  3. /**
  4. * No need to implement any interface
  5. * */
  6. public class DemoTimerTask {
  7. //Define the method to be called as configured
  8. public void execute()
  9. {
  10. System.out.println("Executed task on :: " + new Date());
  11. }
  12. }

现在,让我们测试计时器任务。

TestDemoTimerTask.java

  1. package com.howtodoinjava.timer;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;
  3. public class TestDemoTimerTask {
  4. public static void main(String[] args) {
  5. new ClassPathXmlApplicationContext("application-config.xml");
  6. }
  7. }

Console

  1. Executed task on :: Mon Apr 22 09:53:39 IST 2017
  2. Executed task on :: Mon Apr 22 09:53:44 IST 2017

2. 扩展java.util.TimerTask

通过这种方式,我们通过扩展 java.util.TimerTask 来定义定时器任务,并将其传递给 spring 配置以便重复执行。

让我们来看看如何做:

applicationContext.xml

  1. <beans>
  2. <bean id="demoTimerTask" class="com.howtodoinjava.task.DemoTimerTask2"></bean>
  3. <bean id="scheduledTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
  4. <!-- run every 3 secs -->
  5. <property name="period" value="3000"></property>
  6. <property name="timerTask" ref="demoTimerTask"></property>
  7. </bean>
  8. <bean class="org.springframework.scheduling.timer.TimerFactoryBean">
  9. <property name="scheduledTimerTasks">
  10. <list>
  11. <ref local="scheduledTimerTask"></ref>
  12. </list>
  13. </property>
  14. </bean>
  15. </beans>

以上任务将得到executed in every 3 seconds。 让我们从 java 提供的TimerTask中扩展计时器任务。

DemoTimerTask2.java

  1. package com.howtodoinjava.task;
  2. import java.util.Date;
  3. import java.util.TimerTask;
  4. public class DemoTimerTask2 extends TimerTask
  5. {
  6. public void run()
  7. {
  8. System.out.println("DemoTimerTask2 running at: "
  9. + new Date(this.scheduledExecutionTime()));
  10. }
  11. }

让我们测试配置:

TestDemoTimerTask2.java

  1. package com.howtodoinjava.timer;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;
  3. public class TestDemoTimerTask2
  4. {
  5. public static void main(String[] args)
  6. {
  7. new ClassPathXmlApplicationContext("application-config2.xml");
  8. }
  9. }

Console

  1. DemoTimerTask2 running at: Mon Apr 22 10:01:33 IST 2013
  2. DemoTimerTask2 running at: Mon Apr 22 10:01:36 IST 2013
  3. DemoTimerTask2 running at: Mon Apr 22 10:01:39 IST 2013

下载源码

学习愉快!