How do I terminate method called via TimerFactoryBean at shutdown
When I stop my Virgo server using the shutdown.cmd command the Java process continues to run for a long period.
This seems to be due to the fact that the method called when a timer runs out takes a long time to finish. Is there any way I can detect in my code that a shutdown event has occured so I can stop what I am doing ?
Here is a small piece of example code:
Code:
public void getTestData() {
logger.debug("Timer ran out, ready to get data");
for (int i=0; i<30; i++) {
try {
logger.debug("TEST - i=" + i);
System.out.println("TEST - i=" + i);
Thread.sleep(3000);
} catch (Exception e) {
logger.error("TEST - Exception occured. Exception=" + e);
System.out.println(""TEST - Exception occured. Exception=" + e);
}
}
}
logger.debug("Finished getting data");
System.out.println("Finished getting data");
}
Inside my code I have a loop doing some different stuff a number of times. I have simulated that by making a loop with a sleep inside. When I shutdown the Virgo server the process keeps hanging apparently until my method finishes it job. If I remove the sleep I can see that the Java process terminates immediately.
I thought that maybe I will get an InteruptedException or something that I could use, but apparently I dont. So how do I know that Virgo has been shutdown so I can break out of the loop?
From applicationContext.xml
Code:
<bean id="testTask" class="test.testhandler.testDataHandler">
</bean>
<bean id="testSchedulerTask" class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
<property name="targetObject" ref="testTask" />
<property name="targetMethod" value="getTestData" />
</bean>
<bean id="testTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask" ref="testSchedulerTask" />
<property name="delay" value="5000" />
<property name="period" value="60000" />
</bean>
<bean id="testTimerFactoryBean" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref local="testTimerTask" />
</list>
</property>
</bean>