2017-09-05 107 views
0

加載的內容我有這樣的實用方法來加載內容我的web應用程序@Cacheable按預期啓動時

@Cacheable(value="dataStrore") 
public MarketJson retrieveMarketJson(DataRequest request) throws Exception{  
      try { 

.....調用時這

緩存工作正常不工作通過正常的Web請求,但第一個請求確實需要很長時間,因爲這涉及很多處理。

所以我添加了一個組件,我嘗試在服務器啓動時加載它。

@EventListener(ContextRefreshedEvent.class) 
public void contextRefreshedEvent() { 
    //call retrieveMarketJson method for all possible requests 
} 

這表明罰款日誌文件起作用,但是當web請求進來,它出現在高速緩存尚未發生,它的負荷,並在這一點上緩存,比前進正常工作。

我正在使用EHCache。

+0

你能分享DataRequest類的代碼嗎? –

+0

你在同一個班上展示的兩種方法? –

回答

1

您可以嘗試以下操作。方法initialize()將在應用程序初始化時運行。

@Component 
public class InitializeCachedData { 

    @Autowired 
    private SampleService sampleService; 

    @PostConstruct 
    public void initialize() { 
     // Call retrieveMarketJson method for all possible requests 
     sampleService.callMethod(); 
    } 
} 

UPDATE

您可以嘗試修改使用ContextRefreshedEvent你的代碼,以避免運行根上下文。

@EventListener(ContextRefreshedEvent.class) 
public void contextRefreshedEvent(ContextRefreshedEvent e) { 
    if (e.getApplicationContext().getParent() != null) { 
     //call retrieveMarketJson method for all possible requests 
    } 
} 
+0

當PostConstruct正在執行時,tomcat會保持服務客戶端請求嗎? – Mark1234

+0

正如文檔中所述,PostConstruct註釋用於需要在依賴注入完成以執行任何初始化之後執行的方法。在應用程序初始化繼續之前,Tomcat將保持完成作業的方法。 – lzagkaretos

+0

這工作正常,但它再次沒有幫助緩存,也許緩存bean沒有初始化或準備存儲緩存,我如何使它作爲應用程序啓動的最後一件事情運行? – Mark1234