Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 676b9db

Browse files
committed
BATCH-2242: Stopping a job in STARTING state throws OptimisticLockingFailureException
1 parent acee549 commit 676b9db

File tree

4 files changed

+172
-1
lines changed

4 files changed

+172
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ s3.properties
2323
build
2424
.gradle
2525
pom.xml
26+
out
2627

spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/SimpleJobRepository.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2013 the original author or authors.
2+
* Copyright 2006-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -157,6 +157,8 @@ public void update(JobExecution jobExecution) {
157157
Assert.notNull(jobExecution.getId(), "JobExecution must be already saved (have an id assigned).");
158158

159159
jobExecution.setLastUpdated(new Date(System.currentTimeMillis()));
160+
161+
jobExecutionDao.synchronizeStatus(jobExecution);
160162
jobExecutionDao.updateJobExecution(jobExecution);
161163
}
162164

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.batch.core.repository.dao;
18+
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
import org.springframework.batch.core.BatchStatus;
22+
import org.springframework.batch.core.Job;
23+
import org.springframework.batch.core.JobExecution;
24+
import org.springframework.batch.core.JobParametersBuilder;
25+
import org.springframework.batch.core.StepExecution;
26+
import org.springframework.batch.core.launch.JobLauncher;
27+
import org.springframework.batch.core.launch.JobOperator;
28+
import org.springframework.batch.item.ItemWriter;
29+
import org.springframework.beans.factory.annotation.Autowired;
30+
import org.springframework.test.context.ContextConfiguration;
31+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
32+
33+
import java.util.List;
34+
35+
import static org.junit.Assert.assertTrue;
36+
37+
@ContextConfiguration
38+
@RunWith(SpringJUnit4ClassRunner.class)
39+
public class OptimisticLockingFailureTests {
40+
@Autowired
41+
private Job job;
42+
43+
@Autowired
44+
private JobLauncher jobLauncher;
45+
46+
@Autowired
47+
private JobOperator jobOperator;
48+
49+
@Test
50+
public void testAsyncStopOfStartingJob() throws Exception {
51+
JobExecution jobExecution = jobLauncher.run(job, new JobParametersBuilder()
52+
.addLong("test", 1L)
53+
.toJobParameters());
54+
55+
jobOperator.stop(jobExecution.getId());
56+
57+
while(jobExecution.isRunning()) {
58+
// wait for async launched job to complete execution
59+
}
60+
61+
int numStepExecutions = jobExecution.getStepExecutions().size();
62+
StepExecution stepExecution = jobExecution.getStepExecutions().iterator().next();
63+
String stepName = stepExecution.getStepName();
64+
BatchStatus stepExecutionStatus = stepExecution.getStatus();
65+
BatchStatus jobExecutionStatus = jobExecution.getStatus();
66+
67+
assertTrue("Should only be one StepExecution but got: " + numStepExecutions, numStepExecutions == 1);
68+
assertTrue("Step name for execution should be step1 but got: " + stepName, "step1".equals(stepName));
69+
assertTrue("Step execution status should be STOPPED but got: " + stepExecutionStatus, stepExecutionStatus.equals(BatchStatus.STOPPED));
70+
assertTrue("Job execution status should be STOPPED but got:" + jobExecutionStatus, jobExecutionStatus.equals(BatchStatus.STOPPED));
71+
72+
JobExecution restartJobExecution = jobLauncher.run(job, new JobParametersBuilder()
73+
.addLong("test", 1L)
74+
.toJobParameters());
75+
76+
while(restartJobExecution.isRunning()) {
77+
// wait for async launched job to complete execution
78+
}
79+
80+
int restartNumStepExecutions = restartJobExecution.getStepExecutions().size();
81+
assertTrue("Should be two StepExecution's on restart but got: " + restartNumStepExecutions, restartNumStepExecutions == 2);
82+
83+
for(StepExecution restartStepExecution : restartJobExecution.getStepExecutions()) {
84+
BatchStatus restartStepExecutionStatus = restartStepExecution.getStatus();
85+
86+
assertTrue("Step execution status should be COMPLETED but got: " + restartStepExecutionStatus,
87+
restartStepExecutionStatus.equals(BatchStatus.COMPLETED));
88+
}
89+
90+
BatchStatus restartJobExecutionStatus = restartJobExecution.getStatus();
91+
assertTrue("Job execution status should be COMPLETED but got:" + restartJobExecutionStatus,
92+
restartJobExecutionStatus.equals(BatchStatus.COMPLETED));
93+
}
94+
95+
public static class Writer implements ItemWriter<String> {
96+
@Override
97+
public void write(List<? extends String> items) throws Exception {
98+
for(String item : items) {
99+
System.out.println(item);
100+
}
101+
}
102+
}
103+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<beans xmlns="http://www.springframework.org/schema/beans"
4+
xmlns:batch="http://www.springframework.org/schema/batch"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:schemaLocation="http://www.springframework.org/schema/beans
7+
http://www.springframework.org/schema/beans/spring-beans.xsd
8+
http://www.springframework.org/schema/batch
9+
http://www.springframework.org/schema/batch/spring-batch.xsd">
10+
11+
<import resource="classpath:/org/springframework/batch/core/repository/dao/data-source-context.xml" />
12+
13+
<batch:job id="locking">
14+
<batch:step id="step1" next="step2">
15+
<batch:tasklet ref="tasklet"/>
16+
</batch:step>
17+
<batch:step id="step2">
18+
<batch:tasklet>
19+
<batch:chunk reader="itemReader" writer="itemWriter" commit-interval="5"/>
20+
</batch:tasklet>
21+
</batch:step>
22+
</batch:job>
23+
24+
<batch:job-repository/>
25+
26+
<bean id="tasklet" class="org.springframework.batch.core.configuration.xml.NoopTasklet"/>
27+
28+
<bean id="itemReader" class="org.springframework.batch.item.support.ListItemReader">
29+
<constructor-arg>
30+
<list>
31+
<value>1</value>
32+
<value>2</value>
33+
<value>3</value>
34+
<value>4</value>
35+
<value>5</value>
36+
</list>
37+
</constructor-arg>
38+
</bean>
39+
40+
<bean id="itemWriter" class="org.springframework.batch.core.repository.dao.OptimisticLockingFailureTests$Writer"/>
41+
42+
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
43+
<property name="jobRepository" ref="jobRepository"/>
44+
<property name="taskExecutor">
45+
<bean class="org.springframework.core.task.SimpleAsyncTaskExecutor"/>
46+
</property>
47+
</bean>
48+
49+
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
50+
<property name="dataSource" ref="dataSource" />
51+
</bean>
52+
53+
<bean id="jobExplorer" class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean">
54+
<property name="dataSource" ref="dataSource"/>
55+
</bean>
56+
57+
<bean id="jobRegistry" class="org.springframework.batch.core.configuration.support.MapJobRegistry"/>
58+
59+
<bean id="jobOperator" class="org.springframework.batch.core.launch.support.SimpleJobOperator">
60+
<property name="jobLauncher" ref="jobLauncher"/>
61+
<property name="jobRepository" ref="jobRepository"/>
62+
<property name="jobExplorer" ref="jobExplorer"/>
63+
<property name="jobRegistry" ref="jobRegistry"/>
64+
</bean>
65+
</beans>

0 commit comments

Comments
 (0)