0

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.

4

2 回答 2

0

Thank you for ur support. I am not able to rectify the error infact i have changed my approach. I have used the JDK TIMER with spring approach. you can look for "Using JDK Timer support" this phrase in following spring documentation Spring documentation for JDK timer

Since i wanted to control the delay and period times using properties file i have used

<context:property-placeholder location="classpath:partyrequestws/Timer.properties"/>

This resolved my problem in getting the exception when timer is called for the first time.

I hope this will be helpful. if any one interested i will post the entire code base nut it is very straight forward.

Thank you.

于 2013-05-21T09:34:13.147 回答
0

尝试在 @PostCount 方法中运行这段代码,此时所有的 spring bean 都应该被初始化。

于 2013-05-16T09:51:54.233 回答