BATCH-2078: Close application contexts as batch jobs finish
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/jobXML_1_0.xsd">
|
||||
|
||||
<bean id="mustBeClosedBatchlet" class="org.springframework.batch.core.jsr.launch.JsrJobOperatorTests$MustBeClosedBatchlet"
|
||||
destroy-method="close"/>
|
||||
|
||||
<job id="jsrUniqueInstanceTests" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0">
|
||||
<step id="step1">
|
||||
<batchlet ref="mustBeClosedBatchlet"/>
|
||||
</step>
|
||||
</job>
|
||||
</beans>
|
||||
Reference in New Issue
Block a user