2016-05-12 423 views
1

我的應用程序中有一些Quartz作業,如果我的應用程序環境關閉,我需要立即終止。爲此,我試圖實現ContextStoppedEvent。多次嘗試後我無法觸發此事件。我需要一些幫助來調用這個事件。 以下是我的代碼。Spring引導ContextStoppedEvent未調用

@Component 
public class StopContextListener implements ApplicationListener<ContextStoppedEvent> { 

    private static final Logger LOG = LoggerFactory.getLogger(StopContextListener.class.getName()); 

    @Override 
    public void onApplicationEvent(ContextStoppedEvent arg0) { 
      LOG.info("Data base closed successfully"); 

    } 


} 

更新時間:

@Component 
public class StopContextListener implements ApplicationListener<ContextClosedEvent> { 

    private static final Logger LOG = LoggerFactory.getLogger(StopContextListener.class.getName()); 

    @Override 
    public void onApplicationEvent(ContextClosedEvent arg0) { 
      LOG.info("Data base closed successfully"); 

    } 


} 
+0

「StopContextListener」包和你的應用程序的主類是什麼包?它可能無法通過組件掃描找到。 –

+0

org.springframework.context.event.StopContextListener和我在我的應用程序中使用相同的包。 –

回答

1

鑑於您的要求「立​​即終止,如果我的應用程序上下文獲得封閉」,我建議你聽ContextClosedEvent而非ContextStoppedEvent。 A ContextStoppedEvent未發佈爲正在關閉的上下文的一部分。

+0

我也嘗試過使用ContextStoppedEvent。很遺憾沒有成功。我是否缺少任何啓用ContextStoppedEvent的註解? –

+0

你把你的事件搞混了嗎?我建議你聽'ContextClosedEvent'。 –

+0

感謝您的回覆。根據你的評論,這是我如何做到這一點。我已更新我的帖子。請現在建議。 –