Skip to content

Commit 67309c3

Browse files
committed
[WIP] Use chunk API consistently
Issue spring-projects#3954
1 parent 9211c2d commit 67309c3

File tree

41 files changed

+221
-89
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+221
-89
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/step/item/ChunkOrientedTasklet.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2019 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.
@@ -21,13 +21,15 @@
2121
import org.springframework.batch.core.StepContribution;
2222
import org.springframework.batch.core.scope.context.ChunkContext;
2323
import org.springframework.batch.core.step.tasklet.Tasklet;
24+
import org.springframework.batch.item.Chunk;
2425
import org.springframework.batch.repeat.RepeatStatus;
2526
import org.springframework.lang.Nullable;
2627

2728
/**
2829
* A {@link Tasklet} implementing variations on read-process-write item handling.
2930
*
3031
* @author Dave Syer
32+
* @author Mahmoud Ben Hassine
3133
* @param <I> input item type
3234
*/
3335
public class ChunkOrientedTasklet<I> implements Tasklet {

spring-batch-core/src/main/java/org/springframework/batch/core/step/item/ChunkProcessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2007 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.
@@ -17,6 +17,7 @@
1717
package org.springframework.batch.core.step.item;
1818

1919
import org.springframework.batch.core.StepContribution;
20+
import org.springframework.batch.item.Chunk;
2021

2122
/**
2223
* Interface defined for processing {@link Chunk}s.

spring-batch-core/src/main/java/org/springframework/batch/core/step/item/ChunkProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2007 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.
@@ -17,6 +17,7 @@
1717
package org.springframework.batch.core.step.item;
1818

1919
import org.springframework.batch.core.StepContribution;
20+
import org.springframework.batch.item.Chunk;
2021

2122
/**
2223
* Interface for providing {@link Chunk}s to be processed, used by the

spring-batch-core/src/main/java/org/springframework/batch/core/step/item/FaultTolerantChunkProcessor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434
import org.springframework.batch.core.step.skip.SkipLimitExceededException;
3535
import org.springframework.batch.core.step.skip.SkipListenerFailedException;
3636
import org.springframework.batch.core.step.skip.SkipPolicy;
37+
import org.springframework.batch.item.Chunk;
3738
import org.springframework.batch.item.ItemProcessor;
3839
import org.springframework.batch.item.ItemWriter;
40+
import org.springframework.batch.item.SkipWrapper;
3941
import org.springframework.classify.BinaryExceptionClassifier;
4042
import org.springframework.classify.Classifier;
4143
import org.springframework.retry.ExhaustedRetryException;

spring-batch-core/src/main/java/org/springframework/batch/core/step/item/FaultTolerantChunkProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.batch.core.step.skip.SkipListenerFailedException;
2424
import org.springframework.batch.core.step.skip.SkipPolicy;
2525
import org.springframework.batch.core.step.skip.SkipPolicyFailedException;
26+
import org.springframework.batch.item.Chunk;
2627
import org.springframework.batch.item.ItemReader;
2728
import org.springframework.batch.repeat.RepeatOperations;
2829
import org.springframework.classify.BinaryExceptionClassifier;

spring-batch-core/src/main/java/org/springframework/batch/core/step/item/SimpleChunkProcessor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.batch.core.StepListener;
2727
import org.springframework.batch.core.listener.MulticasterBatchListener;
2828
import org.springframework.batch.core.observability.BatchMetrics;
29+
import org.springframework.batch.item.Chunk;
2930
import org.springframework.batch.item.ItemProcessor;
3031
import org.springframework.batch.item.ItemWriter;
3132
import org.springframework.beans.factory.InitializingBean;

spring-batch-core/src/main/java/org/springframework/batch/core/step/item/SimpleChunkProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.batch.core.StepListener;
2929
import org.springframework.batch.core.listener.MulticasterBatchListener;
3030
import org.springframework.batch.core.observability.BatchMetrics;
31+
import org.springframework.batch.item.Chunk;
3132
import org.springframework.batch.item.ItemReader;
3233
import org.springframework.batch.repeat.RepeatCallback;
3334
import org.springframework.batch.repeat.RepeatContext;

spring-batch-core/src/test/java/org/springframework/batch/core/job/builder/FlowJobBuilderTests.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import org.springframework.batch.core.repository.JobRepository;
4545
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
4646
import org.springframework.batch.core.step.StepSupport;
47+
import org.springframework.batch.item.Chunk;
48+
import org.springframework.batch.item.ItemWriter;
4749
import org.springframework.batch.item.support.ListItemReader;
4850
import org.springframework.beans.factory.annotation.Value;
4951
import org.springframework.context.ApplicationContext;
@@ -292,7 +294,11 @@ static class JobConfiguration {
292294
public Step step(StepBuilderFactory stepBuilderFactory,
293295
@Value("#{jobParameters['chunkSize']}") Integer chunkSize) {
294296
return stepBuilderFactory.get("step").<Integer, Integer>chunk(chunkSize)
295-
.reader(new ListItemReader<>(Arrays.asList(1, 2, 3, 4))).writer(items -> {
297+
.reader(new ListItemReader<>(Arrays.asList(1, 2, 3, 4))).writer(new ItemWriter<Integer>() {
298+
@Override
299+
public void write(Chunk<Integer> chunk) throws Exception {
300+
301+
}
296302
}).build();
297303
}
298304

spring-batch-core/src/test/java/org/springframework/batch/core/observability/BatchMetricsTests.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
3939
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
4040
import org.springframework.batch.core.launch.JobLauncher;
41+
import org.springframework.batch.item.Chunk;
42+
import org.springframework.batch.item.ItemWriter;
4143
import org.springframework.batch.item.support.ListItemReader;
4244
import org.springframework.batch.repeat.RepeatStatus;
4345
import org.springframework.context.ApplicationContext;
@@ -237,17 +239,27 @@ public Step step1() {
237239

238240
@Bean
239241
public Step step2() {
242+
ItemWriter<? super Integer> itemWriter = new ItemWriter<Integer>() {
243+
@Override
244+
public void write(Chunk<Integer> chunk) throws Exception {
245+
chunk.getItems().forEach(System.out::println);
246+
}
247+
};
240248
return stepBuilderFactory.get("step2").<Integer, Integer>chunk(2)
241-
.reader(new ListItemReader<>(Arrays.asList(1, 2, 3, 4, 5)))
242-
.writer(items -> items.forEach(System.out::println)).build();
249+
.reader(new ListItemReader<>(Arrays.asList(1, 2, 3, 4, 5))).writer(itemWriter).build();
243250
}
244251

245252
@Bean
246253
public Step step3() {
254+
ItemWriter<? super Integer> itemWriter = new ItemWriter<Integer>() {
255+
@Override
256+
public void write(Chunk<Integer> chunk) throws Exception {
257+
chunk.getItems().forEach(System.out::println);
258+
}
259+
};
247260
return stepBuilderFactory.get("step3").<Integer, Integer>chunk(2)
248-
.reader(new ListItemReader<>(Arrays.asList(6, 7, 8, 9, 10)))
249-
.writer(items -> items.forEach(System.out::println)).faultTolerant().skip(Exception.class)
250-
.skipLimit(3).build();
261+
.reader(new ListItemReader<>(Arrays.asList(6, 7, 8, 9, 10))).writer(itemWriter).faultTolerant()
262+
.skip(Exception.class).skipLimit(3).build();
251263
}
252264

253265
@Bean

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@
2626
import org.junit.jupiter.params.provider.Arguments;
2727
import org.junit.jupiter.params.provider.MethodSource;
2828

29+
import org.springframework.batch.item.Chunk;
30+
2931
import static org.junit.jupiter.api.Assertions.assertEquals;
3032
import static org.junit.jupiter.api.Assertions.assertFalse;
3133
import static org.junit.jupiter.api.Assertions.assertTrue;
3234

3335
/**
3436
* @author Dave Syer
37+
* @author Mahmoud Ben Hassine
3538
*
3639
*/
3740
class AlmostStatefulRetryChunkTests {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.batch.core.StepContribution;
2828
import org.springframework.batch.core.StepExecution;
2929
import org.springframework.batch.core.scope.context.ChunkContext;
30+
import org.springframework.batch.item.Chunk;
3031

3132
/**
3233
* @author Dave Syer

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.batch.core.listener.ItemListenerSupport;
3737
import org.springframework.batch.core.step.skip.AlwaysSkipItemSkipPolicy;
3838
import org.springframework.batch.core.step.skip.LimitCheckingItemSkipPolicy;
39+
import org.springframework.batch.item.Chunk;
3940
import org.springframework.batch.item.ItemProcessor;
4041
import org.springframework.batch.item.ItemWriter;
4142
import org.springframework.batch.item.support.PassThroughItemProcessor;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.batch.core.StepContribution;
2929
import org.springframework.batch.core.StepExecution;
3030
import org.springframework.batch.core.step.skip.LimitCheckingItemSkipPolicy;
31+
import org.springframework.batch.item.Chunk;
3132
import org.springframework.batch.item.ItemReader;
3233
import org.springframework.batch.item.ParseException;
3334
import org.springframework.batch.item.UnexpectedInputException;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.batch.core.JobParameters;
2929
import org.springframework.batch.core.StepContribution;
3030
import org.springframework.batch.core.StepExecution;
31+
import org.springframework.batch.item.Chunk;
3132
import org.springframework.batch.item.ItemProcessor;
3233
import org.springframework.batch.item.ItemWriter;
3334
import org.springframework.lang.Nullable;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.batch.core.JobParameters;
2727
import org.springframework.batch.core.StepContribution;
2828
import org.springframework.batch.core.StepExecution;
29+
import org.springframework.batch.item.Chunk;
2930
import org.springframework.batch.item.support.ListItemReader;
3031
import org.springframework.batch.repeat.support.RepeatTemplate;
3132

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import org.junit.jupiter.api.Test;
2323

24+
import org.springframework.batch.item.SkipWrapper;
25+
2426
/**
2527
* @author Dave Syer
2628
*
@@ -41,7 +43,7 @@ void testItemWrapperT() {
4143

4244
/**
4345
* Test method for
44-
* {@link org.springframework.batch.core.step.item.SkipWrapper#SkipWrapper(java.lang.Object, java.lang.Throwable)}.
46+
* {@link SkipWrapper#SkipWrapper(java.lang.Object, java.lang.Throwable)}.
4547
*/
4648
@Test
4749
void testItemWrapperTException() {
@@ -51,8 +53,7 @@ void testItemWrapperTException() {
5153
}
5254

5355
/**
54-
* Test method for
55-
* {@link org.springframework.batch.core.step.item.SkipWrapper#toString()}.
56+
* Test method for {@link SkipWrapper#toString()}.
5657
*/
5758
@Test
5859
void testToString() {

spring-batch-core/src/test/java/org/springframework/batch/core/step/tasklet/ChunkOrientedStepIntegrationTests.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
import org.springframework.batch.core.StepExecution;
2727
import org.springframework.batch.core.job.JobSupport;
2828
import org.springframework.batch.core.repository.JobRepository;
29+
import org.springframework.batch.item.Chunk;
2930
import org.springframework.batch.item.ExecutionContext;
3031
import org.springframework.batch.item.ItemReader;
32+
import org.springframework.batch.item.ItemWriter;
3133
import org.springframework.batch.item.support.ListItemReader;
3234
import org.springframework.batch.repeat.policy.SimpleCompletionPolicy;
3335
import org.springframework.batch.repeat.support.RepeatTemplate;
@@ -87,13 +89,18 @@ void onSetUp() {
8789
@Disabled
8890
void testStatusForCommitFailedException() throws Exception {
8991

90-
step.setTasklet(new TestingChunkOrientedTasklet<>(getReader(new String[] { "a", "b", "c" }),
91-
data -> TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
92+
step.setTasklet(
93+
new TestingChunkOrientedTasklet<>(getReader(new String[] { "a", "b", "c" }), new ItemWriter<String>() {
9294
@Override
93-
public void beforeCommit(boolean readOnly) {
94-
throw new RuntimeException("Simulate commit failure");
95+
public void write(Chunk<String> chunk) throws Exception {
96+
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
97+
@Override
98+
public void beforeCommit(boolean readOnly) {
99+
throw new RuntimeException("Simulate commit failure");
100+
}
101+
});
95102
}
96-
}), chunkOperations));
103+
}, chunkOperations));
97104

98105
JobExecution jobExecution = jobRepository.createJobExecution(job.getName(),
99106
new JobParameters(Collections.singletonMap("run.id", new JobParameter(getClass().getName() + ".1"))));

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.batch.core.step.skip.AlwaysSkipItemSkipPolicy;
3636
import org.springframework.batch.core.step.skip.SkipLimitExceededException;
3737
import org.springframework.batch.core.step.skip.SkipPolicy;
38+
import org.springframework.batch.item.Chunk;
3839
import org.springframework.batch.item.ItemProcessor;
3940
import org.springframework.batch.item.ItemReader;
4041
import org.springframework.batch.item.ItemWriter;
@@ -70,9 +71,12 @@ class FaultTolerantStepIntegrationTests {
7071
@BeforeEach
7172
void setUp() {
7273
ItemReader<Integer> itemReader = new ListItemReader<>(createItems());
73-
ItemWriter<Integer> itemWriter = chunk -> {
74-
if (chunk.contains(1)) {
75-
throw new IllegalArgumentException();
74+
ItemWriter<Integer> itemWriter = new ItemWriter<Integer>() {
75+
@Override
76+
public void write(Chunk<Integer> chunk) throws Exception {
77+
if (chunk.getItems().contains(1)) {
78+
throw new IllegalArgumentException();
79+
}
7680
}
7781
};
7882
skipPolicy = new SkipIllegalArgumentExceptionSkipPolicy();

spring-batch-core/src/main/java/org/springframework/batch/core/step/item/Chunk.java renamed to spring-batch-infrastructure/src/main/java/org/springframework/batch/item/Chunk.java

Lines changed: 7 additions & 5 deletions
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-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.
@@ -14,8 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.batch.core.step.item;
17+
package org.springframework.batch.item;
1818

19+
import java.io.Serializable;
1920
import java.util.ArrayList;
2021
import java.util.Collection;
2122
import java.util.Collections;
@@ -26,13 +27,14 @@
2627
* Encapsulation of a list of items to be processed and possibly a list of failed items to
2728
* be skipped. To mark an item as skipped clients should iterate over the chunk using the
2829
* {@link #iterator()} method, and if there is a failure call
29-
* {@link org.springframework.batch.core.step.item.Chunk.ChunkIterator#remove()} on the
30-
* iterator. The skipped items are then available through the chunk.
30+
* {@link Chunk.ChunkIterator#remove()} on the iterator. The skipped items are then
31+
* available through the chunk.
3132
*
3233
* @author Dave Syer
34+
* @author Mahmoud Ben Hassine
3335
* @since 2.0
3436
*/
35-
public class Chunk<W> implements Iterable<W> {
37+
public class Chunk<W> implements Iterable<W>, Serializable {
3638

3739
private List<W> items = new ArrayList<>();
3840

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ItemWriter.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@
3636
* @author Dave Syer
3737
* @author Lucas Ward
3838
* @author Taeik Lim
39+
* @author Mahmoud Ben Hassine
3940
*/
40-
@FunctionalInterface
41+
// TODO in v5.2 add @FunctionalInterface once write(List<? extends T> items) is removed
4142
public interface ItemWriter<T> {
4243

4344
/**
@@ -46,7 +47,23 @@ public interface ItemWriter<T> {
4647
* @param items items to be written
4748
* @throws Exception if there are errors. The framework will catch the exception and
4849
* convert or rethrow it as appropriate.
50+
* @deprecated since 5.0 in favor of {@link ItemWriter#write(Chunk)} instead. Will be
51+
* removed in v5.2
4952
*/
50-
void write(List<? extends T> items) throws Exception;
53+
@Deprecated(since = "5.0")
54+
default void write(List<? extends T> items) throws Exception {
55+
write(new Chunk<>(items));
56+
}
57+
58+
/**
59+
* Write items to a target. Will not be called with any null items in normal
60+
* operation.
61+
* @param items to write
62+
* @throws Exception if there are errors. The framework will catch the exception and
63+
* convert or rethrow it as appropriate.
64+
*/
65+
default void write(Chunk<T> items) throws Exception {
66+
67+
}
5168

5269
}

spring-batch-core/src/main/java/org/springframework/batch/core/step/item/SkipWrapper.java renamed to spring-batch-infrastructure/src/main/java/org/springframework/batch/item/SkipWrapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2018 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.
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.batch.core.step.item;
17+
package org.springframework.batch.item;
1818

1919
import org.springframework.lang.Nullable;
2020

0 commit comments

Comments
 (0)