【Windows 10】Spring Boot2 で 定期実行する方法


概要

Spring Bootで定期実行をする方法。
基本的に Spring Bootでtaskを定期実行する方法 - Qiita の通りにやればできる。
後発のバージョンでもできたという記録。

検証環境

  • Windows10
  • Java : 1.8.0_221
  • Spring Boot : 2.1.8

設定方法

Mainクラス

@EnableScheduling を付与する。

RestApplication.java

package com.aky.restapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class RestApplication extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(RestApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(RestApplication.class);
    }
}

Controller

定期実行したいメソッドに、@Scheduledを付与し起動タイミングを指定する。

ScheduledController.java
package com.aky.restapp.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ScheduledController {
    Logger logger = LoggerFactory.getLogger("");


    @GetMapping("/hoge")
    //毎秒.
    //cron記法で動作
    @Scheduled(cron = "* * * * * *", zone = "Asia/Tokyo")
    public void printHoge(){
        logger.info("----------- hoge start. -----------");
        logger.info("5 seconds delay...");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        logger.info("----------- hoge end. -----------");
    }

    @GetMapping("/fuga")
    //printFuga()実行完了後3秒
    @Scheduled(fixedDelay = 3000)
    public void printFuga() {
        logger.info("----------- fuga start. -----------");
        logger.info("5 seconds delay...");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        logger.info("----------- fuga end. -----------");
    }

    @GetMapping("/piyo")
    //printPiyo()実行開始後5秒
    @Scheduled(fixedRate = 5000)
    public void printPiyo(){
        logger.info("----------- piyo start. -----------");
        logger.info("3 seconds delay...");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        logger.info("----------- piyo end. -----------");
    }
}

実行結果

実行すると以下のようなログになる。

2020-03-15 10:45:05.042  INFO 13684 --- [   scheduling-1]                                          : ----------- fuga start. -----------
2020-03-15 10:45:05.042  INFO 13684 --- [   scheduling-1]                                          : fuga 5 seconds delay...
2020-03-15 10:45:10.047  INFO 13684 --- [   scheduling-1]                                          : ----------- fuga end. -----------
2020-03-15 10:45:10.047  INFO 13684 --- [   scheduling-1]                                          : ----------- hoge start. -----------
2020-03-15 10:45:10.047  INFO 13684 --- [   scheduling-1]                                          : hoge 5 seconds delay...
2020-03-15 10:45:15.061  INFO 13684 --- [   scheduling-1]                                          : ----------- hoge end. -----------
2020-03-15 10:45:15.061  INFO 13684 --- [   scheduling-1]                                          : ----------- piyo start. -----------
2020-03-15 10:45:15.061  INFO 13684 --- [   scheduling-1]                                          : piyo 3 seconds delay...
2020-03-15 10:45:18.061  INFO 13684 --- [   scheduling-1]                                          : ----------- piyo end. -----------
2020-03-15 10:45:18.061  INFO 13684 --- [   scheduling-1]                                          : ----------- piyo start. -----------
2020-03-15 10:45:18.061  INFO 13684 --- [   scheduling-1]                                          : piyo 3 seconds delay...
2020-03-15 10:45:21.073  INFO 13684 --- [   scheduling-1]                                          : ----------- piyo end. -----------
2020-03-15 10:45:21.073  INFO 13684 --- [   scheduling-1]                                          : ----------- fuga start. -----------
2020-03-15 10:45:21.073  INFO 13684 --- [   scheduling-1]                                          : fuga 5 seconds delay...
2020-03-15 10:45:26.083  INFO 13684 --- [   scheduling-1]                                          : ----------- fuga end. -----------
2020-03-15 10:45:26.083  INFO 13684 --- [   scheduling-1]                                          : ----------- hoge start. -----------
2020-03-15 10:45:26.083  INFO 13684 --- [   scheduling-1]                                          : hoge 5 seconds delay...
2020-03-15 10:45:31.096  INFO 13684 --- [   scheduling-1]                                          : ----------- hoge end. -----------
2020-03-15 10:45:31.096  INFO 13684 --- [   scheduling-1]                                          : ----------- piyo start. -----------
2020-03-15 10:45:31.096  INFO 13684 --- [   scheduling-1]                                          : piyo 3 seconds delay...
2020-03-15 10:45:34.098  INFO 13684 --- [   scheduling-1]                                          : ----------- piyo end. -----------
2020-03-15 10:45:34.098  INFO 13684 --- [   scheduling-1]                                          : ----------- piyo start. -----------
2020-03-15 10:45:34.098  INFO 13684 --- [   scheduling-1]                                          : piyo 3 seconds delay...
2020-03-15 10:45:37.103  INFO 13684 --- [   scheduling-1]                                          : ----------- piyo end. -----------
2020-03-15 10:45:37.103  INFO 13684 --- [   scheduling-1]                                          : ----------- piyo start. -----------
2020-03-15 10:45:37.103  INFO 13684 --- [   scheduling-1]                                          : piyo 3 seconds delay...
2020-03-15 10:45:40.106  INFO 13684 --- [   scheduling-1]                                          : ----------- piyo end. -----------
2020-03-15 10:45:40.106  INFO 13684 --- [   scheduling-1]                                          : ----------- fuga start. -----------
2020-03-15 10:45:40.106  INFO 13684 --- [   scheduling-1]                                          : fuga 5 seconds delay...

解説

@Scheduled の使用した設定についてざっくり説明。

@Scheduled(cron = "* * * * * *", zone = "Asia/Tokyo")

cronのような記法で書くことができる。
今回の設定は毎秒実行。

@Scheduled(fixedDelay = 3000)

メソッド処理終了後、指定したミリ秒待ち実行。
今回は3秒待機。

@Scheduled(fixedRate = 5000)

メソッド実行開始から、指定したミリ秒後に実行。
今回は5秒後に実行。

詳細は、参考の公式リファレンスを参照。

参考