24
Apr
12

Shutting Down Spring Batch Jobs Gracefully


This page describes how to exit batch jobs gracefully when the system sends the SIGINT to the JVM. If we ignore these signals the jobs will be left hanging. The solution is to request the Spring Job-operator to gracefully stop the job and in the process save any intermediate data to the spring batch status tables.

Requirements

  • Fully working Spring Batch application
  • Configured Spring batch Job Repository

Start by adding the following to the spring configuration file:

<beans:bean id="jobRegistry" 
	class="org.springframework.batch.core.configuration.support.MapJobRegistry" />
<beans:bean id="jobRegistryBeanPostProcessor"
	class="org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor">
    <beans:property name="jobRegistry" ref="jobRegistry"/>
</beans:bean>	
<beans:bean id="jobOperator" 
	class="org.springframework.batch.core.launch.support.SimpleJobOperator">
		<beans:property name="jobExplorer" ref="jobExplorer"/>
		<beans:property name="jobRepository" ref="jobRepository" />
		<beans:property name="jobRegistry" ref="jobRegistry" />
		<beans:property name="jobLauncher" ref="jobLauncher" />
</beans:bean>
<beans:bean id="jobExplorer" 
	class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean">
	<beans:property name="dataSource" ref="dataSource"/>
</beans:bean>
<beans:bean name="processShutdownListener" 
	class="com.test.batch.listeners.ProcessShutdownListener">
	<beans:property name="jobOperator" ref="jobOperator"/>
</beans:bean>

Inside the job.xml

	<listeners>
		<listener ref="processShutdownListener"/>
	</listeners>

And finally the listener

package com.test.batch.listeners;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.batch.core.launch.JobExecutionNotRunningException;
import org.springframework.batch.core.launch.JobOperator;
import org.springframework.batch.core.launch.NoSuchJobExecutionException;

/**
 * This class listens to events from the Operating System requesting the Batch
 * to shutdown. For example when the user hits CTRL-C or the system is shutting
 * down. If we ignore these signals the jobs will be left hanging. This class
 * attempts to remedy this situation by requesting the JobOperator to gracefully
 * stop a job when the JVM calls the shutdown hook.
 */
public class ProcessShutdownListener implements JobExecutionListener {
	private static final Log logger = 
		LogFactory.getLog(ProcessShutdownListener.class);
	
	private JobOperator jobOperator;
	
	@Override
	public void afterJob(JobExecution jobExecution) { /* do nothing. */ }

	@Override
	public void beforeJob(final JobExecution jobExecution) {
		Runtime.getRuntime().addShutdownHook(new Thread() {
			@Override
			public void run() {
				super.run();
				try {
					jobOperator.stop(jobExecution.getId());
					while(jobExecution.isRunning()) {
						logger.info("waiting for job to stop...");
						try {Thread.sleep(100);} catch (InterruptedException e) {}
					}
				} catch (NoSuchJobExecutionException e) { // ignore
				} catch (JobExecutionNotRunningException e) { // ignore
				}
			}
		});
	}

	public void setJobOperator(JobOperator jobOperator) {
		this.jobOperator = jobOperator;
	}

}
Advertisements

0 Responses to “Shutting Down Spring Batch Jobs Gracefully”



  1. Leave a Comment

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 77 other followers

April 2012
S M T W T F S
« Feb   Jun »
1234567
891011121314
15161718192021
22232425262728
2930  

Blog Stats

  • 846,580 hits

%d bloggers like this: