티스토리 뷰

Spring Application Context Events

1. ContextRefreshedEvent

ApplicationContext를 초기화하거나 새로 고칠 때 Spring은 ContextRefreshedEvent를 발생시킵니다. 

 

2. ContextStartedEvent

ConfigurableApplicationContext에서 start() 메서드를 호출하여 이 이벤트를 트리거하고 ApplicationContext를 시작합니다. 

이 메서드는 일반적으로 명시적 중지 후 빈을 다시 시작하는 데 사용됩니다. 

 

3. ContextStoppedEvent

ContextStoppedEvent는 ConfigurableApplicationContext에서 stop() 메서드를 호출하여 ApplicationContext가 중지될 때 게시됩니다. 앞에서 논의한 것처럼 start() 메서드를 사용하여 중지된 이벤트를 다시 시작할 수 있습니다.

 

4. ContextClosedEvent

이 이벤트는 ConfigurableApplicationContext의 close() 메서드를 사용하여 ApplicationContext가 닫힐 때 게시됩니다.
실제로 컨텍스트를 닫은 후에는 다시 시작할 수 없습니다.

 

Spring Application Events

1. ApplicationContextInitializedEvent

2. ApplicationEnvironmentPreparedEvent

3. ApplicationPreparedEvent

4. ApplicationReadyEvent
5. ApplicationStartedEvent
6. ApplicationFailedEvent
7. ApplicationStartingEvent

테스트 코드 

@Slf4j
@Component
public class EbloEventHandler{

    @EventListener(ContextRefreshedEvent.class)
    public void onContextRefreshedEvent(ContextRefreshedEvent event) {
        log.info("@@@ onContextRefreshedEvent !!!");        
    }
    
    @EventListener(ContextStartedEvent.class)
    public void onContextStartedEvent(ContextStartedEvent event) {
        log.info("@@@ onContextStartedEvent !!!");        
    }
    
    @EventListener(ContextStoppedEvent.class)
    public void onContextStoppedEvent(ContextStoppedEvent event) {
        log.info("@@@ onContextStoppedEvent !!!");        
    }

    @EventListener(ContextClosedEvent.class)
    public void onContextClosedEvent(ContextClosedEvent event) {
        log.info("@@@ onContextClosedEvent !!!");        
    }

    @EventListener(ApplicationContextInitializedEvent.class)
    public void onApplicationContextInitializedEvent(ApplicationContextInitializedEvent event) {
        log.info("@@@ onApplicationContextInitializedEvent !!!");        
    }
    
    @EventListener(ApplicationEnvironmentPreparedEvent.class)
    public void onApplicationEnvironmentPreparedEvent(ApplicationEnvironmentPreparedEvent event) {
        log.info("@@@ onApplicationEnvironmentPreparedEvent !!!");        
    }
    
    @EventListener(ApplicationPreparedEvent.class)
    public void onApplicationPreparedEvent(ApplicationPreparedEvent event) {
        log.info("@@@ onApplicationPreparedEvent !!!");        
    }
    
    @EventListener(ApplicationStartedEvent.class)
    public void onApplicationStartedEvent(ApplicationStartedEvent event) {
        log.info("@@@ onApplicationStartedEvent !!!");        
    }
    
    @EventListener(ApplicationReadyEvent.class)
    public void onApplicationReadyEvent(ApplicationReadyEvent event) {
        log.info("@@@ onApplicationReadyEvent !!!");        
    }

    @EventListener(ApplicationFailedEvent.class)
    public void onApplicationFailedEvent(ApplicationFailedEvent event) {
        log.info("@@@ onApplicationFailedEvent !!!");        
    }
    
    @EventListener(ApplicationStartingEvent.class)
    public void onApplicationStartingEvent(ApplicationStartingEvent event) {
        log.info("@@@ onApplicationStartingEvent !!!");        
    }

}

개별 class로 처리 할 때 

@Slf4j
@Component
public class EbloApplicationReadyEvent implements ApplicationListener<ApplicationReadyEvent>{

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        log.info("@@@ ApplicationReadyEvent !!!");        
    }

}

Spring boot log 

[2021-12-15 10:35:52] INFO  - Tomcat started on port(s): 9090 (http) with context path ''
[2021-12-15 10:35:52] INFO  - @@@ onApplicationPreparedEvent !!!
[2021-12-15 10:35:56] INFO  - @@@ onContextRefreshedEvent !!!
[2021-12-15 10:35:56] INFO  - Started AdminApplication in 13.054 seconds (JVM running for 13.511)
[2021-12-15 10:35:56] INFO  - @@@ onApplicationStartedEvent !!!
[2021-12-15 10:35:56] INFO  - @@@ onApplicationReadyEvent !!!

// 아래는 서비스를 stop 시켰울 때 발생하는 로그입니다. 
[2021-12-15 10:37:16] INFO  - @@@ onContextClosedEvent !!!
[2021-12-15 10:37:16] INFO  - Commencing graceful shutdown. Waiting for active requests to complete
[2021-12-15 10:37:16] INFO  - Graceful shutdown complete
[2021-12-15 10:37:19] WARN  - No URLs will be polled as dynamic configuration sources.
[2021-12-15 10:37:19] INFO  - To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
[2021-12-15 10:37:19] INFO  - Closing JPA EntityManagerFactory for persistence unit 'default'
[2021-12-15 10:37:19] INFO  - Closing JPA EntityManagerFactory for persistence unit 'default'
[2021-12-15 10:37:19] INFO  - Closing JPA EntityManagerFactory for persistence unit 'default'

이벤트 흐름 

일반적인 실행에서 밣생하는 이벤트입니다. 

서비스 시작 이벤트

1. Contentext Loading 완료

2. ContextRefreshedEvent

3. ApplicationPreparedEvent

4. ApplicationStartedEvent

5. ApplicationReadyEvent

서비스 중지 이벤트

1. ContextClosedEvent

이벤트 활용 예

1. Warm Up 코드 추가 

ApplicationReadyEvent 발생 시 서비스 관련된 부분(DB, Cache, Elastic Search, MQ 등)의 상태를 확인합니다. 

등록된 서비스를 테스트하고 L4 에서 확인하는 Health check 를 활성화 시켜 줍니다. 

 

2. 서비스 중지 이벤트 처리 

서비스를 중지 시키면 ContextClosedEvent 발생합니다. 이때 필요한 코드를 추가하면 됩니다. 

MQ를 사용하는 실 업무에서 이용하고 있습니다. Warm up 시점에 서비스를 등록하고 중지 이벤트에서 해제해 주도록 하였습니다. 

 

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함