<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 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) 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看