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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Spring Boot `@Scheduled`注解示例 > 原文: [https://howtodoinjava.com/spring-boot/enable-scheduling-scheduled-job-example/](https://howtodoinjava.com/spring-boot/enable-scheduling-scheduled-job-example/) 為了使 **Spring Boot** 應用程序中的作業定期運行,Spring Boot 提供了[`@EnableScheduling`](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/annotation/EnableScheduling.html)和[`@Scheduled`](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/annotation/Scheduled.html)注解。 讓我們學習如何使用 **Spring boot @Scheduled** 注解。 假設您要每隔 10 秒運行一次作業。 您可以通過以下步驟實現此作業調度: ## 1\. 將`@EnableScheduling`添加到`SpringBootApplication`類 在您的 Spring Boot 應用程序類中添加`@EnableScheduling`注解。 `@EnableScheduling`是一個 Spring 上下文模塊注解。 它通過`@Import(SchedulingConfiguration.class)`指令在內部導入[`SchedulingConfiguration`](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/annotation/SchedulingConfiguration.html) ```java @SpringBootApplication @EnableScheduling public class SpringBootWebApplication { } ``` > 閱讀更多:[在 Spring 中安排任務的 4 種方法](https://howtodoinjava.com/spring-core/4-ways-to-schedule-tasks-in-spring-3-scheduled-example/) ## 2\. 將 Spring boot `@Scheduled`注解添加到方法中 現在,您可以在要調度的方法上添加`@Scheduled`注解。 唯一的條件是方法應**沒有參數**。 導入的`SchedulingConfiguration`將創建的`ScheduledAnnotationBeanPostProcessor`將掃描所有已聲明的 bean,以查看是否存在`@Scheduled`注解。 對于每個沒有參數的帶注解的方法,將創建適當的執行程序線程池。 該線程池將管理帶注解的方法的調度調用。 #### 2.1. 以固定速率安排任務 在固定的速率執行任務: ```java @Scheduled(initialDelay = 1000, fixedRate = 10000) public void run() { logger.info("Current time is :: " + Calendar.getInstance().getTime()); } ``` 現在,在控制臺中觀察輸出: ```java 2017-03-08 15:02:55 - Current time is :: Wed Mar 08 15:02:55 IST 2017 2017-03-08 15:03:05 - Current time is :: Wed Mar 08 15:03:05 IST 2017 2017-03-08 15:03:15 - Current time is :: Wed Mar 08 15:03:15 IST 2017 2017-03-08 15:03:25 - Current time is :: Wed Mar 08 15:03:25 IST 2017 2017-03-08 15:03:35 - Current time is :: Wed Mar 08 15:03:35 IST 2017 ``` #### 2.2. 固定延遲安排任務 將任務配置為在固定延遲后運行。 在給定的示例中,上一次執行的結束與下一次執行的開始之間的持續時間是固定的。 該任務始終等待直到上一個任務完成。 ```java @Scheduled(fixedDelay = 10000) public void run() { logger.info("Current time is :: " + Calendar.getInstance().getTime()); } ``` #### 2.3. Spring Boot Cron 工作示例 `@Scheduled`注解非常靈活,也可以采用 **cron 表達式**。 ```java @Scheduled(cron = "0 10 10 10 * ?") public void run() { logger.info("Current time is :: " + Calendar.getInstance().getTime()); } ``` 在這個 Spring 任務調度程序注解示例中,向我提出您的問題。 學習愉快!
                  <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>

                              哎呀哎呀视频在线观看