🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Spring 定时器任务 > 原文: [https://howtodoinjava.com/spring-core/spring-timer-tasks/](https://howtodoinjava.com/spring-core/spring-timer-tasks/) `Timer`是一种实用程序类,用于安排**一次和重复执行**的任务。 通过计时器任务,Spring 框架为执行调度定期执行多次甚至是一次执行的任务提供支持。 还有其他方法可以在 Java 中实现调度程序功能,例如使用[执行程序服务](https://howtodoinjava.com/java-5/how-to-use-blockingqueue-and-threadpoolexecutor-in-java/)或使用 Quartz 等第三方 API 来无限循环地运行线程。 `Timer`恰好是其中之一。 请注意,`Timer`取决于系统时钟,因此将系统时钟设置为 n 小时将使下一次执行计时器任务的时间也减少 n 小时。 在 Spring 中,有两种使用计时器任务的方法: 1. 配置`MethodInvokingTimerTaskFactoryBean` 2. 扩展`java.util.TimerTask` 让我们使用示例演示每个用法。 ## 1\. 配置`MethodInvokingTimerTaskFactoryBean` 在此方法中,在[`org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean`](http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/scheduling/timer/MethodInvokingTimerTaskFactoryBean.html)定义中配置了计时器任务 bean 及其内部要执行的方法。 这是一个工厂 Bean,它公开 [`TimerTask`](http://java.sun.com/javase/6/docs/api/java/util/TimerTask.html) 对象,该对象将作业执行委派给指定的(静态或非静态)方法。 这避免了实现仅调用现有业务方法的单行`TimerTask`的需要。 Spring 配置示例如下所示: `applicationContext.xml` ```java <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` ```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 configured public void execute() { System.out.println("Executed task on :: " + new Date()); } } ``` 现在,让我们测试计时器任务。 `TestDemoTimerTask.java` ```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` ```java Executed task on :: Mon Apr 22 09:53:39 IST 2017 Executed task on :: Mon Apr 22 09:53:44 IST 2017 ``` ## 2\. 扩展`java.util.TimerTask` 通过这种方式,我们通过扩展 [java.util.TimerTask](https://docs.oracle.com/javase/6/docs/api/java/util/TimerTask.html) 来定义定时器任务,并将其传递给 spring 配置以便重复执行。 让我们来看看如何做: `applicationContext.xml` ```java <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` ```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` ```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` ```java DemoTimerTask2 running at: Mon Apr 22 10:01:33 IST 2013 DemoTimerTask2 running at: Mon Apr 22 10:01:36 IST 2013 DemoTimerTask2 running at: Mon Apr 22 10:01:39 IST 2013 ``` [下载源码](https://docs.google.com/file/d/0B7yo2HclmjI4ZVFSNWxpSWM0dGc/edit?usp=sharing) 学习愉快!