I am using JDKtimer(in spring project) to reload the bean method to refresh the meta data values. I have written following lines to fetch the 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();
and JDK timer is called in this way
TimerTask partyRequestSchedulertask = new DataCacheRefreshScheduler();
Timer timer = new Timer();
timer.schedule(partyRequestSchedulertask,
schedulerDelayTime,
schedulerRepeatTime);
I am getting following exception
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)
after printing this error in console it is executing the refreshDataCache(). I can use try and catch block to capture this error but is there way to avoid this. why this is coming?
There is another way to do JDK Timer using spring-servlet.xml file but here i can not pass the values for repeatInterval and startDelay from a file.