프로그래밍/Java
Java Virtual Thread (11), @scheduled 어노테이션
개발정리
2024. 6. 29. 11:23
스케줄러에서 virtual thread가 잘 종료되는지 확인해보자.
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class VirtualScheduler {
@Scheduled(fixedRate = 5000)
public void fixedRate() {
log.info("fixedRate. thread: {}", Thread.currentThread());
}
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableAsync
@EnableScheduling
@SpringBootApplication
public class VirtualthreadApplication {
public static void main(String[] args) {
SpringApplication.run(VirtualthreadApplication.class, args);
}
}
@Scheduled 선언 후 fixed rate를 5초로 설정하여 5초마다 수행이 되도록 설정하였다. 그리고 @Scheduled 가 동작하기 위해서는 @EnableScheduling 을 넣어줘야 한다.
실행 해보면 5초마다 트리거가 발생하여 log 가 찍히는 것을 볼 수 있다. scheduling-1 이라는 이름도 아래 설정을 통해 바꿔 줄 수 있다.
spring:
output:
ansi:
enabled: ALWAYS
threads:
virtual:
enabled: true
task:
scheduling:
thread-name-prefix: mySch-
# task:
# execution:
# thread-name-prefix: myTask-
# scheduling:
# thread-name-prefix: myScheduler-
# main:
# keep-alive: true
만약 config 를 커스텀하고 싶다면 위와 같이 thread-name-prefix 를 설정하면 된다.