Skip to content

Commit 0d7a160

Browse files
committed
Fix annotation based listener registration in FaultTolerantStepBuilder
Resolves #4137
1 parent be4c22f commit 0d7a160

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/FaultTolerantStepBuilder.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2021 the original author or authors.
2+
* Copyright 2006-2022 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.
@@ -195,8 +195,7 @@ protected Tasklet createTasklet() {
195195
* @return this for fluent chaining
196196
*/
197197
@Override
198-
@SuppressWarnings("unchecked")
199-
public SimpleStepBuilder<I, O> listener(Object listener) {
198+
public FaultTolerantStepBuilder<I, O> listener(Object listener) {
200199
super.listener(listener);
201200

202201
Set<Method> skipListenerMethods = new HashSet<>();
@@ -210,9 +209,7 @@ public SimpleStepBuilder<I, O> listener(Object listener) {
210209
skipListeners.add((SkipListener) factory.getObject());
211210
}
212211

213-
@SuppressWarnings("unchecked")
214-
SimpleStepBuilder<I, O> result = this;
215-
return result;
212+
return this;
216213
}
217214

218215

@@ -432,7 +429,7 @@ public AbstractTaskletStepBuilder<SimpleStepBuilder<I, O>> stream(ItemStream str
432429
}
433430
return this;
434431
}
435-
432+
436433
/**
437434
* Override parent method to prevent creation of a new FaultTolerantStepBuilder
438435
*/

spring-batch-core/src/test/java/org/springframework/batch/core/step/builder/FaultTolerantStepBuilderTests.java

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 the original author or authors.
2+
* Copyright 2021-2022 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.
@@ -15,15 +15,38 @@
1515
*/
1616
package org.springframework.batch.core.step.builder;
1717

18+
import org.junit.Assert;
1819
import org.junit.Test;
1920

20-
import static org.junit.Assert.assertEquals;
21+
import org.springframework.batch.core.Step;
22+
import org.springframework.batch.core.configuration.xml.DummyItemReader;
23+
import org.springframework.batch.core.configuration.xml.DummyItemWriter;
24+
import org.springframework.batch.core.configuration.xml.DummyJobRepository;
25+
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
2126

27+
import static org.junit.Assert.assertEquals;
2228
public class FaultTolerantStepBuilderTests {
2329

24-
@Test
25-
public void faultTolerantReturnsSameInstance() {
26-
FaultTolerantStepBuilder<Object, Object> builder = new FaultTolerantStepBuilder<>(new StepBuilder("test"));
27-
assertEquals(builder, builder.faultTolerant());
28-
}
30+
@Test
31+
public void faultTolerantReturnsSameInstance() {
32+
FaultTolerantStepBuilder<Object, Object> builder = new FaultTolerantStepBuilder<>(new StepBuilder("test"));
33+
assertEquals(builder, builder.faultTolerant());
34+
}
35+
36+
@Test
37+
public void testAnnotationBasedStepExecutionListenerRegistration() {
38+
// given
39+
FaultTolerantStepBuilder<Object, Object> faultTolerantStepBuilder = new StepBuilder("myStep")
40+
.repository(new DummyJobRepository())
41+
.transactionManager(new ResourcelessTransactionManager())
42+
.<Object, Object>chunk(5).reader(new DummyItemReader()).writer(new DummyItemWriter()).faultTolerant()
43+
.listener(new StepBuilderTests.AnnotationBasedStepExecutionListener());
44+
45+
// when
46+
Step step = faultTolerantStepBuilder.build();
47+
48+
// then
49+
Assert.assertNotNull(step);
50+
}
51+
2952
}

0 commit comments

Comments
 (0)