diff --git a/spring-batch-samples/.settings/jmxLauncher.launch b/spring-batch-samples/.settings/jmxLauncher.launch index 671eaa23c..b854074c2 100644 --- a/spring-batch-samples/.settings/jmxLauncher.launch +++ b/spring-batch-samples/.settings/jmxLauncher.launch @@ -2,7 +2,7 @@ - + diff --git a/spring-batch-samples/.springBeans b/spring-batch-samples/.springBeans index bc007b60a..7285cbd34 100644 --- a/spring-batch-samples/.springBeans +++ b/spring-batch-samples/.springBeans @@ -1,7 +1,7 @@ 1 - + @@ -35,6 +35,9 @@ src/main/resources/simple-job-launcher-context.xml src/main/resources/jobs/batchUpdateJob.xml src/test/resources/org/springframework/batch/sample/item/writer/staging-test-context.xml + src/main/resources/adhoc-job-launcher-context.xml + src/main/resources/alt-data-source-context.xml + src/main/resources/quartz-job-launcher-context.xml @@ -261,5 +264,25 @@ src/test/resources/org/springframework/batch/sample/item/writer/staging-test-context.xml + + + true + false + + src/main/resources/data-source-context.xml + src/main/resources/quartz-job-launcher-context.xml + src/main/resources/simple-job-launcher-context.xml + + + + + true + false + + src/main/resources/adhoc-job-launcher-context.xml + src/main/resources/data-source-context.xml + src/main/resources/simple-job-launcher-context.xml + + diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/ClassPathXmlApplicationContextJobFactory.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/ClassPathXmlApplicationContextJobFactory.java index ed20f99b4..bcbb86248 100644 --- a/spring-batch-samples/src/main/java/org/springframework/batch/sample/ClassPathXmlApplicationContextJobFactory.java +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/ClassPathXmlApplicationContextJobFactory.java @@ -15,9 +15,14 @@ */ package org.springframework.batch.sample; +import java.util.List; + import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.JobExecutionException; import org.springframework.batch.core.configuration.JobFactory; import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** @@ -54,7 +59,8 @@ public class ClassPathXmlApplicationContextJobFactory implements JobFactory { */ public Job createJob() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { path }, parent); - return (Job) context.getBean(beanName, Job.class); + Job job = (Job) context.getBean(beanName, Job.class); + return new ContextClosingJob(job, context); } /** @@ -67,5 +73,57 @@ public class ClassPathXmlApplicationContextJobFactory implements JobFactory { public String getJobName() { return beanName; } + + /** + * @author Dave Syer + * + */ + private static class ContextClosingJob implements Job { + private Job delegate; + private ConfigurableApplicationContext context; + /** + * @param delegate + * @param context + */ + public ContextClosingJob(Job delegate, ConfigurableApplicationContext context) { + super(); + this.delegate = delegate; + this.context = context; + } + /** + * @param execution + * @throws JobExecutionException + * @see org.springframework.batch.core.Job#execute(org.springframework.batch.core.JobExecution) + */ + public void execute(JobExecution execution) throws JobExecutionException { + try { + delegate.execute(execution); + } finally { + context.close(); + } + } + /** + * @return + * @see org.springframework.batch.core.Job#getName() + */ + public String getName() { + return delegate.getName(); + } + /** + * @return + * @see org.springframework.batch.core.Job#getSteps() + */ + public List getSteps() { + return delegate.getSteps(); + } + /** + * @return + * @see org.springframework.batch.core.Job#isRestartable() + */ + public boolean isRestartable() { + return delegate.isRestartable(); + } + + } } diff --git a/spring-batch-samples/src/main/resources/adhoc-job-launcher-context.xml b/spring-batch-samples/src/main/resources/adhoc-job-launcher-context.xml index 503829fc5..50be87399 100644 --- a/spring-batch-samples/src/main/resources/adhoc-job-launcher-context.xml +++ b/spring-batch-samples/src/main/resources/adhoc-job-launcher-context.xml @@ -7,14 +7,14 @@ http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> - + + class="org.springframework.batch.core.launch.support.SimpleExportedJobLauncher"> @@ -35,7 +35,7 @@ + value="org.springframework.batch.core.launch.support.ExportedJobLauncher" /> @@ -48,7 +48,7 @@ class="org.springframework.batch.sample.advice.JobExecutionNotificationPublisher" /> + class="org.springframework.batch.core.launch.support.SimpleJobLauncher"> - +