SpringBoot에서 스케쥴링 사용하기
이런 작업들을 "스케쥴링 된 작업" 혹은 "배치작업" 이라고 하는데 Spring에서 지원하는 방법은 크게 두 가지가 있다.
이번에는 스프링의 스케쥴링 어노테이션을 사용한 스케쥴 작업을 생성 해 보려고 한다.
These tasks are called "scheduled tasks" or "batch jobs," and Spring supports two main approaches.
This time, I'm going to create a scheduled task using Spring's scheduling annotations.
스케줄링은 사람의 개입 없이 특정 시간 간격으로 작업을 실행할 수 있는 프로세스이다.
예를 들어 매일 오전 9시 30분에 보고서를 생성하는 작업이 있다고 가정하면, 여기에 우리가 말하고 있는 스케쥴링 작업을 적용하여 요구 사항을 적절하게 충족할 수 있다.
Scheduling is a process that can execute tasks at specific time intervals without human intervention.
For example, if there's a task to generate a report every day at 9:30 AM, we can apply the scheduling task we're discussing to appropriately meet this requirement.
- 스프링 프로젝트 생성
- 스프링부트에 기본적으로 내장되어있는 스케쥴링 어노테이션을 사용– 따로 Dependency를 추가하지 않아도 됨
- SpringBoot를 실행시켜주는 어플리케이션 실행 클래스에
@EnableScheduling어노테이션 적용- 스프링부트에게 스케쥴링 이벤트를 사용한다고 알려줌
- 클래스를 정의하고 스프링이 해당 내역을 판단 할 수 있도록
@Component어노테이션을 추가 - 작업을 실행 & 모니터링
SpringBoot 기본 내장 된 스케쥴러에는 크게 3가지 기능이 존재한다
- FixedRate()
- FixedDelay()
- Cron()
위의 세가지 예를 다뤄보려고 한다
새로운 scheduler 패키지를 만들고 그 안에 스케쥴러 클래스들을 생성하려고 한다.
아래는 나의 프로젝트 구조이다
- Create a Spring project
- Use the scheduling annotation built into SpringBoot - no need to add separate dependencies
- Apply the
@EnableSchedulingannotation to the application startup class that runs SpringBoot- This tells SpringBoot that scheduling events will be used
- Define classes and add the
@Componentannotation so Spring can recognize them - Execute and monitor tasks
SpringBoot's built-in scheduler has three main features:
- FixedRate()
- FixedDelay()
- Cron()
I'm going to cover these three examples.
I'll create a new scheduler package and create scheduler classes inside it.
Below is my project structure.
.
├── HELP.md
├── README.md
├── build.gradle
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── example
│ │ └── springbootsimplescheduler
│ │ ├── SpringBootSimpleSchedulerApplication.java
│ │ └── scheduler
│ │ ├── CronScheduler.java
│ │ ├── FixedDelayScheduler.java
│ │ └── FixedRateScheduler.java
│ └── resources
│ └── application.properties
└── test
└── java
└── com
└── example
└── springbootsimplescheduler
└── SpringBootSimpleSchedulerApplicationTests.java
SpringBoot Scheduler - FixedDelay
@Scheduled(initialDelay = 5000, fixedDelay = 9000) // (a)
@Scheduled(initialDelayString = "5000", fixedDelayString = "9000") // (b)
public void myMethod(){
System.out.println("FixedDelayScheduler - "+new Date());
}SpringBoot Scheduler - FixedRate
@Scheduled(fixedRate = 1000)
//@Scheduled(fixedRateString = "1000")
public void myMethod(){
System.out.println("FixedRateScheduler - "+new Date());
}SpringBoot Scheduler - Cron
@Scheduled(cron = "* * * * * *")
public void myMethod(){
System.out.println("Hello cron Scheduler Three :"+new Date());
}위의 3가지 작업이 모두 끝났다면, 스프링부트 앱을 실행시켜 Syslog가 잘 나오는지 확인하면 된다.
잘 나오지 않는다면, @EnableScheduling 어노테이션을 한번 더 확인 해 주거나 @Component를 잘 등록했는지 확인 해 주면 된다.
Once all three tasks above are completed, run the SpringBoot app and check if the Syslog outputs correctly.
If it doesn't work properly, check the @EnableScheduling annotation again or verify that @Component is properly registered.