hoony's web study

728x90
반응형

 

1. @EnableScheduling 
Springboot 는 공부를 하면 할수록 간편한 부분이 많다고 생각이 드네요.
예전에 scheculer를 생성하고 돌릴려면 정말 불편했었는데 간단하게 적용하게 되어있는 부분이 마음에 듭니다.
물론 제가 이 부분에 대해서 다 알고 포스팅을 하는 것은 아니구요. 
기본 코드를 만들고 테스트하면서 느낀것을 적어보고자 합니다.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class ScheduleDemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(ScheduleDemoApplication.class, args);
	}

}

위의 예시처럼 @EnableScheduling annotion 만 넣으시면 scheduling이 가능하답니다.

2. @Schedule 

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ScheduleTasks {
    private static final Logger log = LoggerFactory.getLogger(ScheduleTasks.class);

    //1second 주기로 돌아가는 함수임.
    @Scheduled(fixedDelay = 1000)
    public void schedule1Second(){
        log.info("1second");
    }

    @Scheduled(fixedRate = 1000)
    public void scheduleFixedRate1Second(){
        log.info("fixedRate : 1 second");
    }

    //1시간에 한번 돌아가는 함수
    @Scheduled(cron = "0 0 0/1 * * * ")
    public void schedule1Hour(){
        log.info("1hour");
    }
}

여기서 주의할 사항은 모든 schedule method 는 void 이며 어떠한 arguments 를 허용하지 않습니다.

2.1 Fixed Rate : 이전 task 가 종료되지 않아도 또 실행이 됩니다. 
2.2 Fixed Delay : 이전 task 가 종료되고 나서 실행이 됩니다. 
2.3 위의 예제 소스에는 없지만 initial Delay 를 사용하실수 있습니다.

@Scheduled(fixedDelay = 1000, initialDelay=5000)
    public void schedule1Second(){
        log.info("1second");
    }

초기 5초후에 1초에 한번씩 실행이 되는 형식입니다.

2.4 cron type 

https://spring.io/blog/2020/11/10/new-in-spring-5-3-improved-cron-expressions

 

New in Spring 5.3: Improved Cron Expressions

<div class="paragraph"> <p>If you regularly listen to <a href="https://bootifulpodcast.fm">A Bootiful Podcast</a>, you might have heard about the improvements we made to Spring Framework’s cron support. Cron expressions are mostly used in Spring applicat

spring.io

위의 블로그가 spring boot cron에 대해서는 아주 잘 설명이 되어있습니다.


3. springboot schedule 은 1개의 thread를 사용합니다. 여러개의 task를 사용해도 내부적으로 1개의 thread 를 사용하는데 이 부분에 대한것은 @Configuration을 이용해서 조절이 가능하네요.

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

@Configuration
public class SchedulerConfig implements SchedulingConfigurer {
    private final int POOL_SIZE = 10;

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();

        threadPoolTaskScheduler.setPoolSize(POOL_SIZE);
        threadPoolTaskScheduler.setThreadNamePrefix("task-pool-");
        threadPoolTaskScheduler.initialize();

        scheduledTaskRegistrar.setTaskScheduler(threadPoolTaskScheduler);
    }
}


저도 이정도로 이해를 하고 현재 소스를 구성하고 있습니다.
제가 참고한 URL은 아래와 같습니다.
https://www.callicoder.com/spring-boot-task-scheduling-with-scheduled-annotation/

 

How to Schedule Tasks with Spring Boot

In this article, you'll learn how to schedule tasks in Spring Boot using the @Scheduled annotation. You'll also configure a custom thread pool for executing all the scheduled tasks.

www.callicoder.com

 

728x90

공유하기

facebook twitter kakaoTalk kakaostory naver band
loading