Results 1 to 5 of 5

Thread: How do I terminate method called via TimerFactoryBean at shutdown

  1. #1
    Join Date
    Jun 2011
    Posts
    5

    Default 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>

  2. #2
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    imho a good application server should, when a server shutdown is issued, autodetect hanging threads and brute-force terminate them. I know for sure Weblogic/Oracle server and IBM Websphere both do. There is no way you can handle a server shutdown event from an application deployed on that server: after all, the server is the container of the application and not the other way around, and when you issue a shutdown applications running on the server "exist" no more, or at least they shouldn't.

  3. #3
    Join Date
    Jun 2011
    Posts
    5

    Default

    I agree that the server should force shutdown, preferable after a configurable amount of time.

    However I would expect that my app would in some way be able to request the running state of either the server or the timer bean (I can see in the log file that it says Cancelling timer), so I from my app can make a clean shutdown (unless it takes more than the configured max shutdown time).

    Maybe I can request the timer bean and check if the timer is still active or something. I will play a little with that.

  4. #4
    Join Date
    Jun 2011
    Posts
    5

    Default

    You can shutdown the Virgo Web Server using the -immediate flag.

    Code:
    shutdown -immediate
    However I could not find a way to detect whether the Virgo Web Server was shutting down or the timer cancelled, so I could make a cleaner exit.

  5. #5
    Join Date
    Oct 2010
    Posts
    3

    Lightbulb

    You can implement DisposableBean interface in your class and override its destroy() method to do your shut down related things.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •