์ด๋ฒคํธ ํ๋ก๊ทธ๋๋ฐ์ ํ์ํ ์ธํฐํ์ด์ค ์ ๊ณต. ์ต์ ๋ฒ ํจํด ๊ตฌํ์ฒด
ApplicationContext extends ApplicationEventPublisher
- publishEvent(ApplicationEvent event)
[event ์ฒ๋ฆฌ ๊ณผ์ ]
SpringBootApplication์ด ๊ตฌ๋ -> AppRunner๊ฐ ์คํ์ด ๋๋ฉด์ -> event ๋ฐ์!
-> ๋ฐ์ํ event๋ฅผ ๋ฑ๋ก๋์ด์๋ ๋น ์ค์์ MyEvent๊ฐ ๋ฐ์์ ์ฒ๋ฆฌ.
์ด๋ฒคํธ ๋ฐ์ ์ํค๋ ๋ฐฉ๋ฒ - ApplicationEventPublisher.publishEvent();
@Component
public class AppRunner implements ApplicationRunner {
@Autowired
ApplicationContext applicationContext;
@Override
public void run(ApplicationArguments args) throws Exception {
applicationContext.publishEvent(new MyEvent(this,100));
}
}
ApplicationContext ๊ฐ ApplicationEventPublisher๋ฅผ ์์๋ฐ๊ธฐ ๋๋ฌธ์, publishEvent(ApplicationEvent event) ๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
์ด๋ฒคํธ ๋ง๋ค๊ธฐ
- ApplicationEvent ์์
- ์คํ๋ง 4.2 ๋ถํฐ๋ ์ด ํด๋์ค๋ฅผ ์์๋ฐ์ง ์์๋ ์ด๋ฒคํธ๋ก ์ฌ์ฉํ ์ ์๋ค.
public class MyEvent extends ApplicationEvent {
private int data;
public MyEvent(Object source) {
super(source);
}
public MyEvent(Object source, int data) {
super(source);
this.data = data;
}
public int getData() {
return data;
}
}
์๋ ์ฝ๋์ ๊ฐ์ด ์ด๋ฒคํธ๋ *POJO ์คํ์ผ๋ก ๋ง๋ค ์ ์๋ค. ์๋๋ ApplicationEvent๋ฅผ ์์๋ฐ์ ๋ง๋ค์ด์ผ ํ์ง๋ง Spring 4.2๋ถํฐ๋ ํด๋์ค๋ฅผ ์์๋ฐ์ง ์์๋ ์ด๋ฒคํธ๋ก ์ฌ์ฉํ ์ ์๋ค.
*POJO ? ์คํ๋ง ์ฝ๋๊ฐ ๋ ธ์ถ์ด ๋์ง ์๋๊ฒ. ์ด๋ฐ ์ฝ๋๊ฐ ํ ์คํธ ํ๊ธฐ ํธํ๊ณ , ์ ์ง๋ณด์ ํ๊ธฐ๊ฐ ํธํด์ง๋ค.
!! (์ด๋ฐ ์ฝ๋๊ฐ SpringFramework ๊ฐ ์ถ๊ตฌํ๋ ์ฒ ํ(?)์ด๋ค. (๋น์นจํฌ์ฑ, ์ด ์ฝ๋์๋ ์คํ๋งํจํค์ง๊ฐ ์ ํ ๋ค์ด๊ฐ์์ง ์๋ค.)
public class MyEvent {
private int data;
private Object source;
public MyEvent(Object source) {
this.source = source;
}
public MyEvent(Object source, int data) {
this.source = source;
this.data = data;
}
public Object getSource(){
return source;
}
public int getData() {
return data;
}
public void setSource(Object source){
this.source = source;
}
public void setData(int data){
this.data = data;
}
}
์ด๋ฒคํธ ๋ฐ์ ์ค๋ ๋ฐฉ๋ฒ
@Component
public class MyEvenHandler implements ApplicationListener<MyEvent> {
@Override
public void onApplicationEvent(MyEvent event) {
System.out.println("์ด๋ฒคํธ ๋ฐ์๋ค. ๋ฐ์ดํฐ๋ " + event.getData());
}
}
ApplicationListener<MyEvent> ๋ฅผ ๊ตฌํํ๊ณ , MyEvent event ๋ฅผ ์ธ์๋ก ๋ฐ์์์ event์ ๋ฉ์๋๋ฅผ ํธ์ถํด ์ถ๋ ฅํ๋ค.
handler๋ spring 4.2 ์ดํ์๋ ํน์ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ์ง ์์๋ ๋๋ค.
@Component
public class MyEvenHandler { //์คํ๋ง 4.2 ์ดํ๋ถํฐ๋ ๊ตฌํ ํ์ง ์์๋ ๋จ!
@EventListener //๋์ ๋น์ผ๋ก๋ ๋ฑ๋กํด์ค์ผํจ. ์ ๋
ธํ
์ด์
๋ถ์ฌ์ผํจ.
public void handle(MyEvent event) { //๋ฉ์๋ ์ด๋ฆ๋ ๋ง๋๋ก ๊ฐ๋ฅ.
System.out.println("์ด๋ฒคํธ ๋ฐ์๋ค. ๋ฐ์ดํฐ๋ " + event.getData());
}
}
์๋๋ ์ถ๋ ฅ ๊ฒฐ๊ณผ์ด๋ค.
์ด๋ฒคํธ ๋ฐ์๋ค. ๋ฐ์ดํฐ๋ 100
Handler๊ฐ ์ฌ๋ฌ ๊ฐ ๋ผ๋ฉด?
@Component
public class AnotherHandler {
@EventListener
public void handle(MyEvent myEvent){
System.out.println(Thread.currentThread().toString());//Thread๋ฅผ ์ฐ์ด๋ณด๊ธฐ ์ํ ์ฝ๋
System.out.println("Another " + myEvent.getData());
}
}
MyEventHandler์ AnotherHandler ๊ฐ ๋๊ฐ์ ํธ๋ค๋ฌ๊ฐ ์๊ณ , ๋์์ ์คํ์ ์ํค๋ฉด??
์ด๋ค ์ฐ๋ ๋์์ ์ฐํ๋์ง ์ถ๋ ฅ์ ํด๋ณด์๋ค.
Thread[main,5,main]
Another 100
Thread[main,5,main]
์ด๋ฒคํธ ๋ฐ์๋ค. ๋ฐ์ดํฐ๋ 100
๋๋ค main ์ฐ๋ ๋์์ ์คํ์ด ๋์๊ณ , ๋์์ ์คํ์ด ๋๋๊ฑด ์๋๋ค.
์์๋ฅผ ์ ํ๊ณ ์ถ๋ค๋ฉด @Order ๋ฅผ ์ฌ์ฉ
@Component
public class AnotherHandler {
@EventListener
@Order(Ordered.HIGHEST_PRECEDENCE) //๊ฐ์ฅ ๋์ ์ฐ์ ์์ ๋ถ์ฌ
public void handle(MyEvent myEvent){
System.out.println(Thread.currentThread().toString());//Thread๋ฅผ ์ฐ์ด๋ณด๊ธฐ ์ํ ์ฝ๋
System.out.println("Another " + myEvent.getData());
}
}
๋ง์ฝ ์ฐ์ ์์๋ณด๋ค ๋ฎ๊ฒ ์ถ๋ ฅํ๋ ค๋ฉด Ordered.HIGHEST_PRECEDENCE + 2 ์ ๋๋ฅผ ๋ํด์ฃผ๋ฉด ๋ฆ๊ฒ ์ถ๋ ฅ๋๋ค.
๋น๋๊ธฐ์ ์ผ๋ก ์คํผํ๊ณ ์ถ๋ค๋ฉด @Async ๋ฅผ ์ฌ์ฉ
@Component
public class MyEvenHandler {
@EventListener
@Async //๋น๋๊ธฐ์ ์คํ
public void handle(MyEvent event) {
System.out.println(Thread.currentThread().toString());
System.out.println("์ด๋ฒคํธ ๋ฐ์๋ค. ๋ฐ์ดํฐ๋ " + event.getData());
}
}
@SpringBootApplication
@EnableAsync //๋ฐ๋์ @EnableAsync ์ ํจ๊ป ์ฌ์ฉํด์ผํ๋ค. ์๊ทธ๋ฌ๋ฉด ์ ์ฉ์ด ์๋๋ค.
public class Demospring51Application {
public static void main(String[] args) {
SpringApplication.run(Demospring51Application.class, args);
}
}
@EventListener ์ ๋ ธํ ์ด์ ์ด ๋ถ์ ๋ฉ์๋์ @Async ์ ๋ ธํ ์ด์ ์ ๋ถ์ฌ ๋น๋๊ธฐ์ ์ผ๋ก ์คํ ์ํจ๋ค๋ ๊ฒ์ ๋ช ์ํด์ผํ๋ค.
์ด๋ @Order ์ ๋ ธํ ์ด์ ์ ์๋ฏธ๊ฐ ์์ด์ง๊ฒ ๋๋ค.
@Async ๋ฅผ ํ์ฑํ ์ํค๋ ค๋ฉด, @SpringBootApplication ์ ๋ ธํ ์ด์ ์ด ๋ถ์ ํด๋์ค์ @EnableAsync๋ฅผ ๋ถ์ฌ์ค์ผ๋ง ํ๋ค.
Thread[task-1,5,main]
Thread[task-2,5,main]
Another 100
์ด๋ฒคํธ ๋ฐ์๋ค. ๋ฐ์ดํฐ๋ 100
์คํ๋ง์ด ์ ๊ณตํ๋ ๊ธฐ๋ณธ ์ด๋ฒคํธ
ContextRefreshedEvent : ApplicationContext๋ฅผ ์ด๊ธฐํ ํ๊ฑฐ๋, ๋ฆฌํ๋์ ํ์๋ ๋ฐ์ |
ContextStartedEvent : ApplicationContext๋ฅผ start()ํ์ฌ ๋ผ์ดํ ์ฌ์ดํด ๋น๋ค์ด ์์ ์ ํธ๋ฅผ ๋ฐ์ ์์ ์ ๋ฐ์. |
ContextStopedEvent : ApplicationContext๋ฅผ stop()ํ์ฌ ๋ผ์ดํ ์ฌ์ดํด ๋น๋ค์ด ์ ์ง ์ ํธ๋ฅผ ๋ฐ์ ์์ ์ ๋ฐ์. |
ContextClosedEvent : ApplicationContext๋ฅผ close()ํ์ฌ ์ฑ๊ธํค ๋น ์๋ช ๋๋ ์์ ์ ๋ฐ์. |
RequestHandledEvent : HTTP ์์ฒญ์ ์ฒ๋ฆฌํ์ ๋ ๋ฐ์. |
@Component
public class MyEvenHandler {//ํน์ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ์ง ์์๋ ๋๋ค.
@EventListener
@Async
public void handle(MyEvent event) {
System.out.println(Thread.currentThread().toString());
System.out.println("์ด๋ฒคํธ ๋ฐ์๋ค. ๋ฐ์ดํฐ๋ " + event.getData());
}
@EventListener
@Async
public void handle(ContextRefreshedEvent event) {
System.out.println(Thread.currentThread().toString());
System.out.println("ContextRefreshedEvent");
}//ApplicationContext๋ฅผ ์ด๊ธฐํ ํ๊ฑฐ๋ ๋ฆฌํ๋ ์ ํ์๋ ๋ฐ์
@EventListener
@Async
public void handle(ContextClosedEvent event) {
System.out.println(Thread.currentThread().toString());
System.out.println("ContextClosedEvent");
}//ApplicationContext๋ฅผ close()ํ์ฌ ์ฑ๊ธํค ๋น ์๋ฉธ๋๋ ์์ ์ ๋ฐ์.
}
2020-07-07 23:38:54.119 INFO 1467 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-07-07 23:38:54.253 INFO 1467 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
Thread[task-1,5,main]
ContextRefreshedEvent
2020-07-07 23:38:54.267 INFO 1467 --- [ main] c.e.d.Demospring51Application : Started Demospring51Application in 1.087 seconds (JVM running for 1.548)
Thread[task-3,5,main]
์ด๋ฒคํธ ๋ฐ์๋ค. ๋ฐ์ดํฐ๋ 100
Thread[task-2,5,main]
Another 100
Thread[task-4,5,main]
ContextClosedEvent
2020-07-07 23:38:59.537 INFO 1467 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
[์ฐธ์กฐ] https://engkimbs.tistory.com/718