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

Skip to content

Commit bf525de

Browse files
committed
XD-1622: Add tasklet and step type to Step's ExecutionContext for XD usage
1 parent 5fb7421 commit bf525de

File tree

6 files changed

+28
-14
lines changed

6 files changed

+28
-14
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/Step.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
public interface Step {
2626

27+
static final String STEP_TYPE_KEY = "batch.stepType";
2728
/**
2829
* @return the name of this step.
2930
*/

spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/FlowStep.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public void afterPropertiesSet() throws Exception {
8484
@Override
8585
protected void doExecute(StepExecution stepExecution) throws Exception {
8686
try {
87+
stepExecution.getExecutionContext().put(STEP_TYPE_KEY, this.getClass().getName());
8788
StepHandler stepHandler = new SimpleStepHandler(getJobRepository(), stepExecution.getExecutionContext());
8889
FlowExecutor executor = new JobFlowExecutor(getJobRepository(), stepHandler, stepExecution.getJobExecution());
8990
executor.updateJobExecutionStatus(flow.start(executor).getStatus());

spring-batch-core/src/main/java/org/springframework/batch/core/partition/support/PartitionStep.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.batch.core.partition.support;
1818

19-
import java.util.Collection;
20-
2119
import org.springframework.batch.core.BatchStatus;
2220
import org.springframework.batch.core.JobExecutionException;
2321
import org.springframework.batch.core.Step;
@@ -28,6 +26,8 @@
2826
import org.springframework.batch.item.ExecutionContext;
2927
import org.springframework.util.Assert;
3028

29+
import java.util.Collection;
30+
3131
/**
3232
* Implementation of {@link Step} which partitions the execution and spreads the
3333
* load using a {@link PartitionHandler}.
@@ -100,6 +100,7 @@ public void afterPropertiesSet() throws Exception {
100100
*/
101101
@Override
102102
protected void doExecute(StepExecution stepExecution) throws Exception {
103+
stepExecution.getExecutionContext().put(STEP_TYPE_KEY, this.getClass().getName());
103104

104105
// Wait for task completion and then aggregate the results
105106
Collection<StepExecution> executions = partitionHandler.handle(stepExecutionSplitter, stepExecution);

spring-batch-core/src/main/java/org/springframework/batch/core/step/job/JobStep.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ protected void doExecute(StepExecution stepExecution) throws Exception {
103103

104104
ExecutionContext executionContext = stepExecution.getExecutionContext();
105105

106+
executionContext.put(STEP_TYPE_KEY, this.getClass().getName());
107+
106108
JobParameters jobParameters;
107109
if (executionContext.containsKey(JOB_PARAMETERS_KEY)) {
108110
jobParameters = (JobParameters) executionContext.get(JOB_PARAMETERS_KEY);

spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/TaskletStep.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ public boolean rollbackOn(Throwable ex) {
100100

101101
private Tasklet tasklet;
102102

103+
public static final String TASKLET_TYPE_KEY = "batch.taskletType";
104+
103105
/**
104106
* Default constructor.
105107
*/
@@ -242,6 +244,8 @@ public void setInterruptionPolicy(StepInterruptionPolicy interruptionPolicy) {
242244
*/
243245
@Override
244246
protected void doExecute(StepExecution stepExecution) throws Exception {
247+
stepExecution.getExecutionContext().put(TASKLET_TYPE_KEY, tasklet.getClass().getName());
248+
stepExecution.getExecutionContext().put(STEP_TYPE_KEY, this.getClass().getName());
245249

246250
stream.update(stepExecution.getExecutionContext());
247251
getJobRepository().updateExecutionContext(stepExecution);

spring-batch-core/src/test/java/org/springframework/batch/core/step/item/TaskletStepExceptionTests.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,14 @@
1515
*/
1616
package org.springframework.batch.core.step.item;
1717

18-
import static org.junit.Assert.assertEquals;
19-
import static org.junit.Assert.assertFalse;
20-
import static org.junit.Assert.assertTrue;
21-
import static org.springframework.batch.core.BatchStatus.COMPLETED;
22-
import static org.springframework.batch.core.BatchStatus.FAILED;
23-
import static org.springframework.batch.core.BatchStatus.STOPPED;
24-
import static org.springframework.batch.core.BatchStatus.UNKNOWN;
25-
26-
import java.util.Collection;
27-
2818
import org.junit.Before;
2919
import org.junit.Test;
3020
import org.springframework.batch.core.ExitStatus;
3121
import org.springframework.batch.core.JobExecution;
3222
import org.springframework.batch.core.JobInstance;
3323
import org.springframework.batch.core.JobInterruptedException;
3424
import org.springframework.batch.core.JobParameters;
25+
import org.springframework.batch.core.Step;
3526
import org.springframework.batch.core.StepContribution;
3627
import org.springframework.batch.core.StepExecution;
3728
import org.springframework.batch.core.StepExecutionListener;
@@ -54,6 +45,16 @@
5445
import org.springframework.transaction.support.DefaultTransactionStatus;
5546
import org.springframework.transaction.support.TransactionSynchronizationManager;
5647

48+
import java.util.Collection;
49+
50+
import static org.junit.Assert.assertEquals;
51+
import static org.junit.Assert.assertFalse;
52+
import static org.junit.Assert.assertTrue;
53+
import static org.springframework.batch.core.BatchStatus.COMPLETED;
54+
import static org.springframework.batch.core.BatchStatus.FAILED;
55+
import static org.springframework.batch.core.BatchStatus.STOPPED;
56+
import static org.springframework.batch.core.BatchStatus.UNKNOWN;
57+
5758
/**
5859
* Tests for the behavior of TaskletStep in a failure scenario.
5960
*
@@ -245,7 +246,9 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext attribut
245246
assertEquals(1, stepExecution.getRollbackCount()); // Failed transaction
246247
// counts as
247248
// rollback
248-
assertEquals(0, stepExecution.getExecutionContext().size());
249+
assertEquals(2, stepExecution.getExecutionContext().size());
250+
assertTrue(stepExecution.getExecutionContext().containsKey(Step.STEP_TYPE_KEY));
251+
assertTrue(stepExecution.getExecutionContext().containsKey(TaskletStep.TASKLET_TYPE_KEY));
249252
}
250253

251254
@SuppressWarnings("serial")
@@ -278,7 +281,9 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext attribut
278281
assertEquals(1, stepExecution.getRollbackCount()); // Failed transaction
279282
// counts as
280283
// rollback
281-
assertEquals(0, stepExecution.getExecutionContext().size());
284+
assertEquals(2, stepExecution.getExecutionContext().size());
285+
assertTrue(stepExecution.getExecutionContext().containsKey(Step.STEP_TYPE_KEY));
286+
assertTrue(stepExecution.getExecutionContext().containsKey(TaskletStep.TASKLET_TYPE_KEY));
282287
}
283288

284289
@Test

0 commit comments

Comments
 (0)