일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- marksense.ai
- matplotlib
- YOLOv5
- C++
- Expo
- 졸프
- yolo
- google cloud
- Loss Function
- Spring Boot
- javascript
- OG tag
- STT
- Spring
- 양방향 매핑
- 코드업
- 순환참조
- skt fellowship 3기
- 커스텀 데이터 학습
- google 로그인
- idToken
- @Transactional
- react native
- JPA
- AWS
- google login
- pandas
- oauth
- html
- 2021 제9회 문화공공데이터 활용경진대회
Archives
- Today
- Total
민팽로그
[spring] scheduler - cron 본문
https://spring.io/guides/gs/scheduling-tasks/
Scheduling Tasks
this guide is designed to get you productive as quickly as possible and using the latest Spring project releases and techniques as recommended by the Spring team
spring.io
Scheduler
일정 주기마다 반복해서 주어진 작업 자동 실행
- @Scheduled 어노테이션을 사용하여 특정 메소드가 실행되는 시기, 주기를 설정함
cron
@Scheduled 옵션 중 하나. 초(0~59), 분(0~59), 시(0~23), 일(0~31), 월(1~12), 요일(0(일)~6(토))[, 연도] 순으로 주기를 설정할 수 있음
ex) * * 1 * * * : 매월 매일 1시 매분 매초마다 실행
* | 모든 조건에서 실행 - 매 초, 매 분, 매 시간 등 |
/ | 특정 간격마다 실행 - 초 부분에 0/20 으로 표기하면 0초부터 시작하여 20초마다 실행 |
- | 특정 조건에서 실행 - 시 부분에 21-23 으로 표기하면 21시부터 23시까지 실행 |
사용 예시
1. 스케줄러 클래스를 만든 후 @Component 어노테이션을 추가하여 스프링이 자동으로 생성 및 실행을 할 수 있도록 함
2. @Scheduled 어노테이션 옵션으로 cron 선택 -> 설정한 구체적인 주기에 맞춰 작업 실행
3. application 클래스에 @EnableScheduling 어노테이션을 추가하여 스프링이 스케줄링을 할 수 있도록 해줌
- Scheduler 클래스
@Component // 스프링이 필요 시 자동으로 생성하는 클래스 목록에 추가
public class Scheduler {
// 초, 분, 시, 일, 월, 요일 순서
@Scheduled(cron = "0 0 1 * * *")
public void updatePrice() throws InterruptedException {
//자동화 할 코드
...
//과부하 방지를 위해 필요 시 sleep을 적절히 사용
TimeUnit.SECONDS.sleep(1); //1초마다 쉼
...
}
}
- Application 클래스
@EnableScheduling // 스프링 부트에서 스케줄러가 작동하게 함
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
'🍃spring boot' 카테고리의 다른 글
스프링 AOP 개념 간단 이해 (0) | 2021.09.14 |
---|---|
[OAuth] 웹에서 카카오 소셜 로그인 (0) | 2021.09.13 |
DI(Dependency Injection)와 스프링 IoC 컨테이너 (0) | 2021.09.12 |
servlet과 객체지향 프로그래밍(OOP:Object-Oriented Programming) (0) | 2021.09.12 |
스프링 구조 - Controller, Service, Repository (0) | 2021.07.31 |
Comments