diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/launch/JsrJobOperator.java b/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/launch/JsrJobOperator.java index a2af9eafe..709e3b99e 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/launch/JsrJobOperator.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/launch/JsrJobOperator.java @@ -520,6 +520,8 @@ public class JsrJobOperator implements JobOperator, InitializingBean { factoryBean.close(); } + batchContext.close(); + if(semaphore.availablePermits() == 0) { semaphore.release(); } @@ -541,6 +543,10 @@ public class JsrJobOperator implements JobOperator, InitializingBean { jobRepository.update(jobExecution); + if(batchContext.isActive()) { + batchContext.close(); + } + throw new JobRestartException(e); } @@ -656,6 +662,8 @@ public class JsrJobOperator implements JobOperator, InitializingBean { factoryBean.close(); } + batchContext.close(); + if(semaphore.availablePermits() == 0) { semaphore.release(); } @@ -679,6 +687,10 @@ public class JsrJobOperator implements JobOperator, InitializingBean { } jobRepository.update(jobExecution); + if(batchContext.isActive()) { + batchContext.close(); + } + throw new JobStartException(e); } return jobExecution.getId(); diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/launch/JsrJobOperatorTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/launch/JsrJobOperatorTests.java index 52aafee51..cb118f498 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/launch/JsrJobOperatorTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/launch/JsrJobOperatorTests.java @@ -544,6 +544,15 @@ public class JsrJobOperatorTests { } + @Test + public void testApplicationContextClosingAfterJobSuccessful() throws Exception { + for(int i = 0; i < 3; i++) { + javax.batch.runtime.JobExecution execution = runJob("contextClosingTests", new Properties(), TIMEOUT); + + assertEquals(BatchStatus.COMPLETED, execution.getBatchStatus()); + } + } + public static class LongRunningBatchlet implements Batchlet { private boolean stopped = false; @@ -568,4 +577,25 @@ public class JsrJobOperatorTests { throw new RuntimeException("blah"); } } + + public static class MustBeClosedBatchlet extends AbstractBatchlet { + + public static boolean closed = true; + + public MustBeClosedBatchlet() { + if(!closed) { + throw new RuntimeException("Batchlet wasn't closed last time"); + } + } + + public void close() { + closed = true; + } + + @Override + public String process() throws Exception { + closed = false; + return null; + } + } } diff --git a/spring-batch-core/src/test/resources/META-INF/batch-jobs/contextClosingTests.xml b/spring-batch-core/src/test/resources/META-INF/batch-jobs/contextClosingTests.xml new file mode 100644 index 000000000..584a7a938 --- /dev/null +++ b/spring-batch-core/src/test/resources/META-INF/batch-jobs/contextClosingTests.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + \ No newline at end of file