[Annotation] Spring Scheduling Annotations
https://www.baeldung.com/spring-scheduling-annotations
org.springframework.scheduling.annotation
단일 thread 보다 더 복잡한 실행이 필요할 경우에 사용함
@EnableAsync
이 애노테이션은 Spring에서 비동기 기능을 활성화 한다
@Configuration 과 함께 사용해야 한다 ***
@Configuration
@EnableAsync
class VehicleFactoryConfig {}
이렇게 비동기 호출을 활성화 시킨 후 원하는 메서드에 @Async 를 붙여서 정의하면 된다
@EnableScheduling
이 애노테이션을 사용하여 스케쥴링을 활성화 한다
@Configuration 과 함께 사용해야 한다 ***
@Configuration
@EnableScheduling
class VehicleFactoryConfig {}
이렇게 스케쥴링을 활성화 시킨 후 원하는 메서드에 @Scheduled 를 붙여서 정의하면 된다
보면 위의 두 애노테이션은 config 하는 부분에 붙여준다는 점<<<
@Async
다른 thread에서 실행하려는 method를 정의하여 비동기식으로 실행시킨다
@Async를 붙이면 되는데, 알아둬야할게있다!!!!!
1. Spring @ComponentScan annotation or created inside a class marked @Configuration.
** 위의 Configuration을 선언 후 사용할 것
2. Never use @Async on top of a private method. In runtime, it will not able to create a proxy and, therefore, not work.
** public 메소드에서만 적용할 것 (private 작동 안됨)
3. Never write an Async method in the same class where the caller method invokes the same Async methodAsync method in the same class where the caller method invokes the same Async method.
세번째는 그림으로 보는게 쉽다
package com.example.ask2shamik.springAsync.demo;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
public class AsyncCaller {
@Autowired
AsyncMailTrigger asyncMailTriggerObject;
public void rightWayToCall() {
System.out.println("Calling From rightWayToCall Thread " + Thread.currentThread().getName());
asyncMailTriggerObject.senMail(populateMap());
}
public void wrongWayToCall() {
System.out.println("Calling From wrongWayToCall Thread " + Thread.currentThread().getName());
this.senMail(populateMap());
}
private Map<String,String> populateMap(){
Map<String,String> mailMap= new HashMap<String,String>();
mailMap.put("body", "A Ask2Shamik Article");
return mailMap;
}
@Async
public void senMail(Map<String,String> properties) {
System.out.println("Trigger mail in a New Thread :: " + Thread.currentThread().getName());
properties.forEach((K,V)->System.out.println("Key::" + K + " Value ::" + V));
}
}
4. Please note that we use the @EnableAsync annotation. 이건 위에 있듯이 @EnableAsync를 꼭 붙이라는 뜻
아래의 자세한 설명을 보면 된다
https://dzone.com/articles/effective-advice-on-spring-async-part-1
Effective Advice on Spring Async: Part 1 - DZone
In this post, we explore some of the biggest misconceptions and limitations when working with Spring's Async annotation.
dzone.com
https://www.baeldung.com/spring-async
@Scheduled
주기적으로 실행할 메서드가 필요할 경우 사용한다
@Scheduled(fixedRate = 10000)
void checkVehicle() {
// ...
}
fixedRate 또는 cron 과 같은 표현식을 쓸 수 있다
@Scheduled는 Java 8 반복 주석 기능을 활용할 수 있다, 메소드에 여러번 추가할 수도 있다
@Scheduled(fixedRate = 10000)
@Scheduled(cron = "0 * * * * MON-FRI")
void checkVehicle() {
// ...
}
@Scheduled 어노테이션이 있는 메서드는 void 반환 유형을 가져야 한다 <<<
https://www.baeldung.com/spring-scheduled-tasks
@Schedules
여러 @Scheduled 규칙을 지정해준다
@Schedules({
@Scheduled(fixedRate = 10000),
@Scheduled(cron = "0 * * * * MON-FRI")
})
void checkVehicle() {
// ...
}
Java8 부터 반복 주석 기능으로 결과를 얻을 수 있다
예제도 있고.. 좋은 세상~~!
https://github.com/eugenp/tutorials/tree/master/spring-boot-modules/spring-boot-annotations
GitHub - eugenp/tutorials: Just Announced - "Learn Spring Security OAuth":
Just Announced - "Learn Spring Security OAuth": . Contribute to eugenp/tutorials development by creating an account on GitHub.
github.com