私は JDKtimer (春のプロジェクト) を使用して Bean メソッドをリロードし、メタデータ値を更新しています。Bean をフェッチするために次の行を書きました。
Long schedulerDelayTime = AppParametersHelper.getLong(SCHEDULER_DELAY_TIME);
Long schedulerRepeatTime = AppParametersHelper.getLong(SCHEDULER_REPEAT_TIME);
ApplicationContext ctx = ApplicationContextUtil.getContext();
IPartyRequestDataCache partyRequestPingService =
(IPartyRequestDataCache) ctx.getBean("partyRequestDataCache");
partyRequestPingService.refreshDataCache();
JDKタイマーはこのように呼び出されます
TimerTask partyRequestSchedulertask = new DataCacheRefreshScheduler();
Timer timer = new Timer();
timer.schedule(partyRequestSchedulertask,
schedulerDelayTime,
schedulerRepeatTime);
次の例外が発生しています
Exception in thread "Timer-2" java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'ref
resh' before accessing beans via the ApplicationContext
at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicat
ionContext.java:171)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1045)
at org.tiaa.partyrequest.listener.DataCacheRefreshScheduler.run(DataCacheRefreshScheduler.java:20)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)
このエラーをコンソールに出力した後、refreshDataCache() を実行しています。try と catch ブロックを使用してこのエラーをキャプチャできますが、これを回避する方法はありますか。なぜこれが来るのですか?
spring-servlet.xml ファイルを使用して JDK タイマーを実行する別の方法がありますが、ここではファイルから repeatInterval と startDelay の値を渡すことができません。