728x90
힘차게 또 공부를 😂
https://docs.spring.io/spring-framework/reference/integration/scheduling.html
Task Execution and Scheduling :: Spring Framework
All Spring cron expressions have to conform to the same format, whether you are using them in @Scheduled annotations, task:scheduled-tasks elements, or someplace else. A well-formed cron expression, such as * * * * * *, consists of six space-separated time
docs.spring.io
Spring 은 다양한 task들을 실행할 수 있도록 추상화한 TaskExecutor 인터페이스를 제공한다.
TaskExecutor Types
- SyncTaskExecutor: 이 구현은 호출을 비동기적으로 실행하지 않는다. 대신 각 호출은 호출 스레드에서 발생한다. 간단한 테스트 케이스처럼 멀티스레딩이 필요하지 않은 상황에서 주로 사용된다.
- SimpleAsyncTaskExecutor: 이 구현은 스레드를 재사용하지 않는다. 오히려 호출할 때마다 새 스레드를 시작한다. 그러나 슬롯이 확보될 때까지 제한을 초과하는 모든 호출을 차단하는 동시성 제한을 지원한다. pooling 이 필요하면 ThreadPoolTaskExecutor 를 쓸것. (하단에)
- ConcurrentTaskExecutor: 이 구현은 java.util.concurrent.Executor 인스턴스에 대한 어댑터이다. Executor 구성 매개변수를 Bean 속성으로 노출하는 대안(ThreadPoolTaskExecutor)이 있다. ConcurrentTaskExecutor를 직접 사용할 필요는 거의 없으나 ThreadPoolTaskExecutor가 요구 사항에 비해 충분히 유연하지 않은 경우 ConcurrentTaskExecutor를 쓰면 된다.
- ThreadPoolTaskExecutor: 이 구현은 가장 일반적으로 사용된다. java.util.concurrent.ThreadPoolExecutor를 구성하기 위한 Bean 속성을 노출하고 이를 TaskExecutor에 래핑한다. 다른 종류의 java.util.concurrent.Executor에 적응해야 하는 경우 ConcurrentTaskExecutor를 대신 사용하는 것이 좋다.
- DefaultManagedTaskExecutorManagedExecutorService: 이 구현 에서는 JSR-236 호환 런타임 환경(예: Jakarta EE 애플리케이션 서버)에서 얻은 JNDI를 사용하여 해당 목적으로 CommonJ WorkManager를 대체한다.
public class TaskExecutorExample {
private class MessagePrinterTask implements Runnable {
private String message;
public MessagePrinterTask(String message) {
this.message = message;
}
public void run() {
System.out.println(message);
}
}
private TaskExecutor taskExecutor;
public TaskExecutorExample(TaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}
public void printMessages() {
for(int i = 0; i < 25; i++) {
taskExecutor.execute(new MessagePrinterTask("Message" + i));
}
}
}
The Spring TaskScheduler Abstraction
public interface TaskScheduler {
Clock getClock();
ScheduledFuture schedule(Runnable task, Trigger trigger);
ScheduledFuture schedule(Runnable task, Instant startTime);
ScheduledFuture scheduleAtFixedRate(Runnable task, Instant startTime, Duration period);
ScheduledFuture scheduleAtFixedRate(Runnable task, Duration period);
ScheduledFuture scheduleWithFixedDelay(Runnable task, Instant startTime, Duration delay);
ScheduledFuture scheduleWithFixedDelay(Runnable task, Duration delay);
Trigger Interface
public interface Trigger {
Instant nextExecution(TriggerContext triggerContext);
}
public interface TriggerContext {
Clock getClock();
Instant lastScheduledExecution();
Instant lastActualExecution();
Instant lastCompletion();
}
Trigger Implementations
scheduler.schedule(task, new CronTrigger("0 15 9-17 * * MON-FRI"));
Annotation Support for Scheduling and Asynchronous Execution
Enable Scheduling Annotations
@Configuration
@EnableAsync
@EnableScheduling
public class AppConfig {
}
728x90
'Web > spring' 카테고리의 다른 글
[File Extensions] 업로드 파일 통제 (0) | 2023.11.24 |
---|---|
[Task Execution and Scheduling] @Scheduled annotation (2) | 2023.11.22 |
JPA 프레임워크의 개념과 구조 (0) | 2023.11.12 |
[Spring] Assertions in JUnit 4 and JUnit 5 (0) | 2023.04.25 |
[Spring] A Spring Custom Annotation for a Better DAO (0) | 2023.04.24 |