diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/CommandLineJobRunner.java b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/CommandLineJobRunner.java index ea02a2695..795a566f4 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/CommandLineJobRunner.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/CommandLineJobRunner.java @@ -29,7 +29,7 @@ import org.springframework.batch.core.launch.JobLauncher; import org.springframework.batch.repeat.ExitStatus; import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.config.AutowireCapableBeanFactory; -import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.util.StringUtils; @@ -178,8 +178,10 @@ public class CommandLineJobRunner { */ int start(String jobPath, String jobName, String[] parameters) { + ConfigurableApplicationContext context = null; + try { - ApplicationContext context = new ClassPathXmlApplicationContext(jobPath); + context = new ClassPathXmlApplicationContext(jobPath); context.getAutowireCapableBeanFactory().autowireBeanProperties( this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false); @@ -200,6 +202,10 @@ public class CommandLineJobRunner { } catch (Throwable e) { logger.error("Job Terminated in error:", e); return exitCodeMapper.intValue(ExitStatus.FAILED.getExitCode()); + } finally { + if (context!=null) { + context.close(); + } } } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/CommandLineJobRunnerTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/CommandLineJobRunnerTests.java index 17e4cf93b..5ad52f959 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/CommandLineJobRunnerTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/CommandLineJobRunnerTests.java @@ -85,6 +85,12 @@ public class CommandLineJobRunnerTests extends TestCase { assertEquals(new JobParameters(), StubJobLauncher.jobParameters); } + public void testDestroyCallback() throws Throwable { + String[] args = new String[] { jobPath, jobName }; + CommandLineJobRunner.main(args); + assertTrue(StubJobLauncher.destroyed); + } + protected void tearDown() throws Exception { super.tearDown(); @@ -112,6 +118,8 @@ public class CommandLineJobRunnerTests extends TestCase { public static JobParameters jobParameters; + private static boolean destroyed = false; + public JobExecution run(Job job, JobParameters jobParameters) throws JobExecutionAlreadyRunningException { StubJobLauncher.jobParameters = jobParameters; @@ -122,11 +130,16 @@ public class CommandLineJobRunnerTests extends TestCase { return jobExecution; } + + public void destroy() { + destroyed = true; + } public static void tearDown() { jobExecution = null; throwExecutionRunningException = false; jobParameters = null; + destroyed = false; } } diff --git a/spring-batch-core/src/test/resources/org/springframework/batch/core/launch/support/test-environment.xml b/spring-batch-core/src/test/resources/org/springframework/batch/core/launch/support/test-environment.xml index bb6b43dd0..d6ec3e0a5 100644 --- a/spring-batch-core/src/test/resources/org/springframework/batch/core/launch/support/test-environment.xml +++ b/spring-batch-core/src/test/resources/org/springframework/batch/core/launch/support/test-environment.xml @@ -7,8 +7,10 @@ http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> - - + +