<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Spring `@Scheduled` – 安排任務的 4 種方法 > 原文: [https://howtodoinjava.com/spring-core/spring-scheduled-annotation/](https://howtodoinjava.com/spring-core/spring-scheduled-annotation/) Spring 使用`@Scheduled`注解為基于[ cron 表達式](https://en.wikipedia.org/wiki/Cron)的任務調度和異步方法執行提供了出色的支持。 可以將`@Scheduled`注解與觸發器元數據一起添加到方法中。 在本文中,我將展示以 4 種不同方式使用`@Scheduled`功能的方法。 > 閱讀更多: [Spring timer 任務](https://howtodoinjava.com/spring/spring-core/2-ways-to-execute-timer-tasks-in-spring-3/) ## 1\. Spring `@Scheduled`注解 `@Scheduled`注解用于任務調度。 觸發信息需要與此注解一起提供。 #### 1.1. `fixedDelay` vs `FixedRate` vs `cron` 您可以使用屬性`fixedDelay` / `fixedRate` / `cron`提供觸發信息。 在哪里: 1. `fixedRate`使 Spring 定期運行任務,即使最后一次調用可能仍在運行。 2. `fixedDelay`特別控制最后一次執行完成時的下一個執行時間。 3. `cron`是源自 Unix cron 實用程序的功能,并根據您的要求提供各種選項。 用法示例如下: `@Scheduled Usages` ```java @Scheduled(fixedDelay =30000) public void demoServiceMethod () {... } @Scheduled(fixedRate=30000) public void demoServiceMethod () {... } @Scheduled(cron="0 0 * * * *") public void demoServiceMethod () {... } ``` #### 1.2. 如何啟用`@Scheduled`注解 要在 spring 應用程序中使用`@Scheduled`,必須首先在`applicationConfig.xml`文件中定義 xml 命名空間和模式位置定義。 還添加`task:annotation-driven`以啟用基于注解的任務調度。 `applicationConfig.xml` ```java xmlns:task="http://www.springframework.org/schema/task" http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd <task:annotation-driven> ``` 上面的添加是必要的,因為我們將使用基于注解的配置。 #### 1.3. 使用`@Scheduled`注解 下一步是在類中創建一個類和一個方法,如下所示: `DemoService.java` ```java public class DemoService { @Scheduled(cron="*/5 * * * * ?") public void demoServiceMethod() { System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date()); } } ``` 在上述示例中: 1. 反過來,使用`@Scheduled`注解將使 Spring 容器了解該注解下面的方法將作為作業運行。 2. 請記住,用`@Scheduled`注解的方法不應將參數傳遞給它們。 3. 他們也不應返回任何值。 4. 如果希望在`@Scheduled`方法中使用外部對象,則應使用自動裝配將它們注入`DemoService`類,而不是將它們作為參數傳遞給`@Scheduled`方法。 ## 2\. `fixedDelay`和`fixedRate` 在此方法中,`fixedDelay`屬性與`@Scheduled`注解一起使用。 或者,也可以使用`fixedRate`。 示例類如下所示: `DemoServiceBasicUsageFixedDelay.java` ```java package com.howtodoinjava.service; import java.util.Date; import org.springframework.scheduling.annotation.Scheduled; public class DemoServiceBasicUsageFixedDelay { @Scheduled(fixedDelay = 5000) //@Scheduled(fixedRate = 5000) //Or use this public void demoServiceMethod() { System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date()); } } ``` 應用程序配置如下所示: `applicationContext.xml` ```java < ?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <task:annotation-driven /> <bean id="demoServiceBasicUsageFixedDelay" class="com.howtodoinjava.service.DemoServiceBasicUsageFixedDelay"></bean> </beans> ``` ## 3\. 使用 Cron 表達式的`@scheduled` 在此方法中,`cron`屬性與`@Scheduled`注解一起使用。 此屬性的值必須是 cron 表達式。 示例類如下所示: `DemoServiceBasicUsageCron.java` ```java package com.howtodoinjava.service; import java.util.Date; import org.springframework.scheduling.annotation.Scheduled; public class DemoServiceBasicUsageCron { @Scheduled(cron="*/5 * * * * ?") public void demoServiceMethod() { System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date()); } } ``` 應用程序配置如下所示: `applicationContext.xml` ```java < ?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <task:annotation-driven /> <bean id="demoServiceBasicUsageCron" class="com.howtodoinjava.service.DemoServiceBasicUsageCron"></bean> </beans> ``` ## 4\. 屬性文件中的 Cron 表達式 在此方法中,`cron`屬性與`@Scheduled`注解一起使用。 此屬性的值必須為 cron 表達式,如先前方法 BUT 一樣,該 cron 表達式將在屬性文件中定義,并且相關屬性的鍵將用于`@Scheduled`注解。 **將使 cron 表達式與源代碼**脫鉤,從而使更改變得容易。 `DemoServicePropertiesExample.java` ```java package com.howtodoinjava.service; import java.util.Date; import org.springframework.scheduling.annotation.Scheduled; public class DemoServicePropertiesExample { @Scheduled(cron = "${cron.expression}") public void demoServiceMethod() { System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date()); } } ``` 應用程序配置如下所示: `applicationContext.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <task:annotation-driven /> <util:properties id="applicationProps" location="application.properties" /> <context:property-placeholder properties-ref="applicationProps" /> <bean id="demoServicePropertiesExample" class="com.howtodoinjava.service.DemoServicePropertiesExample"></bean> </beans> ``` ## 5\. 上下文配置中的 Cron 表達式 在這種方法中,在屬性文件中配置了 cron 表達式,并使用屬性密鑰為 cron 表達式在配置文件中配置了作業調度。 主要變化是,您無需在任何方法上使用`@Scheduled`注解。 方法配置也在應用程序配置文件中完成。 示例類如下所示: `DemoServiceXmlConfig.java` ```java package com.howtodoinjava.service; import java.util.Date; public class DemoServiceXmlConfig { public void demoServiceMethod() { System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date()); } } ``` 應用程序配置如下所示: `applicationContext.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <task:annotation-driven /> <util:properties id="applicationProps" location="application.properties" /> <context:property-placeholder properties-ref="applicationProps" /> <bean id="demoServiceXmlConfig" class="com.howtodoinjava.service.DemoServiceXmlConfig" /> <task:scheduled-tasks> <task:scheduled ref="demoServiceXmlConfig" method="demoServiceMethod" cron="#{applicationProps['cron.expression']}"></task:scheduled> </task:scheduled-tasks> </beans> ``` [下載源碼](https://docs.google.com/file/d/0B7yo2HclmjI4Qk5OdkhTeWhxMWc/edit?usp=sharing) 讓我知道我是否遺漏任何東西。 學習愉快! 參考: [http://forum.springsource.org/showthread.php?83053-Feature-Scheduled-with-Value-cron-expression](http://forum.springsource.org/showthread.php?83053-Feature-Scheduled-with-Value-cron-expression)
                  <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>

                              哎呀哎呀视频在线观看