Skip to content

Commit e67c006

Browse files
committed
Use the Chunk API consistently
This commit replaces the usage of List with Chunk where appropriate. Summary of changes: - The Chunk class was moved from the `org.springframework.batch.core.step.item` package to the `org.springframework.batch.item` package - The signature of the method `ItemWriter#write(List)` was changed to `ItemWriter#write(Chunk)` - All implementations of `ItemWriter` were updated to use the Chunk API instead of List - All methods in the `ItemWriteListener` interface were updated to use the Chunk API instead of List - All implementations of `ItemWriteListener` were updated to use the Chunk API instead of List - The constructor of `ChunkRequest` was changed to accept a Chunk instead of a Collection of items - The return type of `ChunkRequest#getItems()` was changed from List to Chunk Resolves #3954
1 parent bf2e6ab commit e67c006

File tree

175 files changed

+1077
-763
lines changed

Some content is hidden

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

175 files changed

+1077
-763
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
import java.util.List;
1919

2020
import org.springframework.batch.core.scope.context.ChunkContext;
21+
import org.springframework.batch.item.Chunk;
2122
import org.springframework.batch.item.ItemWriter;
2223

2324
/**
2425
* <p>
2526
* Listener interface for the writing of items. Implementations of this interface are
26-
* notified before, after, and in case of any exception thrown while writing a list of
27+
* notified before, after, and in case of any exception thrown while writing a chunk of
2728
* items.
2829
* </p>
2930
*
@@ -42,19 +43,18 @@
4243
public interface ItemWriteListener<S> extends StepListener {
4344

4445
/**
45-
* Called before {@link ItemWriter#write(java.util.List)}
46+
* Called before {@link ItemWriter#write(Chunk)}
4647
* @param items to be written
4748
*/
48-
default void beforeWrite(List<? extends S> items) {
49+
default void beforeWrite(Chunk<? extends S> items) {
4950
}
5051

5152
/**
52-
* Called after {@link ItemWriter#write(java.util.List)}. This is called before any
53-
* transaction is committed, and before
54-
* {@link ChunkListener#afterChunk(ChunkContext)}.
53+
* Called after {@link ItemWriter#write(Chunk)}. This is called before any transaction
54+
* is committed, and before {@link ChunkListener#afterChunk(ChunkContext)}.
5555
* @param items written items
5656
*/
57-
default void afterWrite(List<? extends S> items) {
57+
default void afterWrite(Chunk<? extends S> items) {
5858
}
5959

6060
/**
@@ -64,7 +64,7 @@ default void afterWrite(List<? extends S> items) {
6464
* @param exception thrown from {@link ItemWriter}
6565
* @param items attempted to be written.
6666
*/
67-
default void onWriteError(Exception exception, List<? extends S> items) {
67+
default void onWriteError(Exception exception, Chunk<? extends S> items) {
6868
}
6969

7070
}

spring-batch-core/src/main/java/org/springframework/batch/core/annotation/AfterWrite.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@
2727

2828
/**
2929
* Marks a method to be called after an item is passed to an {@link ItemWriter}. Note that
30-
* this annotation takes a {@link List} because Spring Batch generally processes a group
31-
* of items (for the sake of efficiency).<br>
30+
* this annotation takes a {@link org.springframework.batch.item.Chunk} because Spring
31+
* Batch generally processes a group of items (for the sake of efficiency).<br>
3232
* <br>
33-
* Expected signature: void afterWrite({@link List}&lt;? extends S&gt; items)
33+
* Expected signature: void afterWrite({@link org.springframework.batch.item.Chunk}&lt;?
34+
* extends S&gt; items)
3435
*
3536
* @author Lucas Ward
37+
* @author Mahmoud Ben Hassine
3638
* @since 2.0
3739
* @see ItemWriteListener
3840
*/

spring-batch-core/src/main/java/org/springframework/batch/core/annotation/BeforeWrite.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626
import org.springframework.batch.item.ItemWriter;
2727

2828
/**
29-
* Marks a method to be called before an item is passed to an {@link ItemWriter}. <br>
29+
* Marks a method to be called before a chunk is passed to an {@link ItemWriter}. <br>
3030
* <br>
31-
* Expected signature: void beforeWrite({@link List}&lt;? extends S&gt; items)
31+
* Expected signature: void beforeWrite({@link org.springframework.batch.item.Chunk}&lt;?
32+
* extends S&gt; items)
3233
*
3334
* @author Lucas Ward
35+
* @author Mahmoud Ben Hassine
3436
* @since 2.0
3537
* @see ItemWriteListener
3638
*/

spring-batch-core/src/main/java/org/springframework/batch/core/annotation/OnWriteError.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@
2727

2828
/**
2929
* Marks a method to be called if an exception is thrown by an {@link ItemWriter}. Note
30-
* that this annotation takes a {@link List} because Spring Batch generally processes a
31-
* group of items (for the sake of efficiency).<br>
30+
* that this annotation takes a {@link org.springframework.batch.item.Chunk} because
31+
* Spring Batch generally processes a group of items (for the sake of efficiency).<br>
3232
* <br>
33-
* Expected signature: void onWriteError({@link Exception} exception, {@link List}&lt;?
34-
* extends S&gt; items)
33+
* Expected signature: void onWriteError({@link Exception} exception,
34+
* {@link org.springframework.batch.item.Chunk}&lt;? extends S&gt; items)
3535
*
3636
* @author Lucas Ward
37+
* @author Mahmoud Ben Hassine
3738
* @since 2.0
3839
* @see ItemWriteListener
3940
*/

spring-batch-core/src/main/java/org/springframework/batch/core/listener/CompositeItemWriteListener.java

Lines changed: 9 additions & 7 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.
@@ -19,11 +19,13 @@
1919
import java.util.List;
2020

2121
import org.springframework.batch.core.ItemWriteListener;
22+
import org.springframework.batch.item.Chunk;
2223
import org.springframework.core.Ordered;
2324

2425
/**
2526
* @author Lucas Ward
2627
* @author Dave Syer
28+
* @author Mahmoud Ben Hassine
2729
*
2830
*/
2931
public class CompositeItemWriteListener<S> implements ItemWriteListener<S> {
@@ -50,10 +52,10 @@ public void register(ItemWriteListener<? super S> itemWriteListener) {
5052
/**
5153
* Call the registered listeners in reverse order, respecting and prioritising those
5254
* that implement {@link Ordered}.
53-
* @see ItemWriteListener#afterWrite(java.util.List)
55+
* @see ItemWriteListener#afterWrite(Chunk)
5456
*/
5557
@Override
56-
public void afterWrite(List<? extends S> items) {
58+
public void afterWrite(Chunk<? extends S> items) {
5759
for (Iterator<ItemWriteListener<? super S>> iterator = listeners.reverse(); iterator.hasNext();) {
5860
ItemWriteListener<? super S> listener = iterator.next();
5961
listener.afterWrite(items);
@@ -63,10 +65,10 @@ public void afterWrite(List<? extends S> items) {
6365
/**
6466
* Call the registered listeners in order, respecting and prioritising those that
6567
* implement {@link Ordered}.
66-
* @see ItemWriteListener#beforeWrite(List)
68+
* @see ItemWriteListener#beforeWrite(Chunk)
6769
*/
6870
@Override
69-
public void beforeWrite(List<? extends S> items) {
71+
public void beforeWrite(Chunk<? extends S> items) {
7072
for (Iterator<ItemWriteListener<? super S>> iterator = listeners.iterator(); iterator.hasNext();) {
7173
ItemWriteListener<? super S> listener = iterator.next();
7274
listener.beforeWrite(items);
@@ -76,10 +78,10 @@ public void beforeWrite(List<? extends S> items) {
7678
/**
7779
* Call the registered listeners in reverse order, respecting and prioritising those
7880
* that implement {@link Ordered}.
79-
* @see ItemWriteListener#onWriteError(Exception, List)
81+
* @see ItemWriteListener#onWriteError(Exception, Chunk)
8082
*/
8183
@Override
82-
public void onWriteError(Exception ex, List<? extends S> items) {
84+
public void onWriteError(Exception ex, Chunk<? extends S> items) {
8385
for (Iterator<ItemWriteListener<? super S>> iterator = listeners.reverse(); iterator.hasNext();) {
8486
ItemWriteListener<? super S> listener = iterator.next();
8587
listener.onWriteError(ex, items);

spring-batch-core/src/main/java/org/springframework/batch/core/listener/MulticasterBatchListener.java

Lines changed: 8 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.
@@ -28,6 +28,7 @@
2828
import org.springframework.batch.core.StepExecutionListener;
2929
import org.springframework.batch.core.StepListener;
3030
import org.springframework.batch.core.scope.context.ChunkContext;
31+
import org.springframework.batch.item.Chunk;
3132
import org.springframework.batch.item.ItemStream;
3233
import org.springframework.lang.Nullable;
3334

@@ -241,10 +242,10 @@ public void onReadError(Exception ex) {
241242
}
242243

243244
/**
244-
* @see ItemWriteListener#afterWrite(List)
245+
* @see ItemWriteListener#afterWrite(Chunk)
245246
*/
246247
@Override
247-
public void afterWrite(List<? extends S> items) {
248+
public void afterWrite(Chunk<? extends S> items) {
248249
try {
249250
itemWriteListener.afterWrite(items);
250251
}
@@ -254,10 +255,10 @@ public void afterWrite(List<? extends S> items) {
254255
}
255256

256257
/**
257-
* @see ItemWriteListener#beforeWrite(List)
258+
* @see ItemWriteListener#beforeWrite(Chunk)
258259
*/
259260
@Override
260-
public void beforeWrite(List<? extends S> items) {
261+
public void beforeWrite(Chunk<? extends S> items) {
261262
try {
262263
itemWriteListener.beforeWrite(items);
263264
}
@@ -267,10 +268,10 @@ public void beforeWrite(List<? extends S> items) {
267268
}
268269

269270
/**
270-
* @see ItemWriteListener#onWriteError(Exception, List)
271+
* @see ItemWriteListener#onWriteError(Exception, Chunk)
271272
*/
272273
@Override
273-
public void onWriteError(Exception ex, List<? extends S> items) {
274+
public void onWriteError(Exception ex, Chunk<? extends S> items) {
274275
try {
275276
itemWriteListener.onWriteError(ex, items);
276277
}

spring-batch-core/src/main/java/org/springframework/batch/core/listener/StepListenerMetaData.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-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.
@@ -46,6 +46,7 @@
4646
import org.springframework.batch.core.annotation.OnSkipInWrite;
4747
import org.springframework.batch.core.annotation.OnWriteError;
4848
import org.springframework.batch.core.scope.context.ChunkContext;
49+
import org.springframework.batch.item.Chunk;
4950

5051
/**
5152
* Enumeration for {@link StepListener} meta data, which ties together the names of
@@ -72,10 +73,10 @@ public enum StepListenerMetaData implements ListenerMetaData {
7273
Object.class),
7374
ON_PROCESS_ERROR("onProcessError", "on-process-error-method", OnProcessError.class, ItemProcessListener.class,
7475
Object.class, Exception.class),
75-
BEFORE_WRITE("beforeWrite", "before-write-method", BeforeWrite.class, ItemWriteListener.class, List.class),
76-
AFTER_WRITE("afterWrite", "after-write-method", AfterWrite.class, ItemWriteListener.class, List.class),
76+
BEFORE_WRITE("beforeWrite", "before-write-method", BeforeWrite.class, ItemWriteListener.class, Chunk.class),
77+
AFTER_WRITE("afterWrite", "after-write-method", AfterWrite.class, ItemWriteListener.class, Chunk.class),
7778
ON_WRITE_ERROR("onWriteError", "on-write-error-method", OnWriteError.class, ItemWriteListener.class,
78-
Exception.class, List.class),
79+
Exception.class, Chunk.class),
7980
ON_SKIP_IN_READ("onSkipInRead", "on-skip-in-read-method", OnSkipInRead.class, SkipListener.class, Throwable.class),
8081
ON_SKIP_IN_PROCESS("onSkipInProcess", "on-skip-in-process-method", OnSkipInProcess.class, SkipListener.class,
8182
Object.class, Throwable.class),

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: 3 additions & 2 deletions
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,9 +17,10 @@
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
/**
22-
* Interface defined for processing {@link Chunk}s.
23+
* Interface defined for processing {@link org.springframework.batch.item.Chunk}s.
2324
*
2425
* @since 2.0
2526
*/

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

Lines changed: 4 additions & 3 deletions
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,10 +17,11 @@
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
/**
22-
* Interface for providing {@link Chunk}s to be processed, used by the
23-
* {@link ChunkOrientedTasklet}
23+
* Interface for providing {@link org.springframework.batch.item.Chunk}s to be processed,
24+
* used by the {@link ChunkOrientedTasklet}
2425
*
2526
* @since 2.0
2627
* @see ChunkOrientedTasklet

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2008 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.
@@ -20,6 +20,7 @@
2020
import org.apache.commons.logging.Log;
2121
import org.apache.commons.logging.LogFactory;
2222
import org.springframework.batch.core.listener.ItemListenerSupport;
23+
import org.springframework.batch.item.Chunk;
2324

2425
/**
2526
* Default implementation of the {@link ItemListenerSupport} class that writes all
@@ -28,6 +29,7 @@
2829
* object.
2930
*
3031
* @author Lucas Ward
32+
* @author Mahmoud Ben Hassine
3133
*
3234
*/
3335
public class DefaultItemFailureHandler extends ItemListenerSupport<Object, Object> {
@@ -45,7 +47,7 @@ public void onReadError(Exception ex) {
4547
}
4648

4749
@Override
48-
public void onWriteError(Exception ex, List<? extends Object> item) {
50+
public void onWriteError(Exception ex, Chunk<? extends Object> item) {
4951
try {
5052
logger.error("Error encountered while writing item: [ " + item + "]", ex);
5153
}

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

Lines changed: 4 additions & 2 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;
@@ -337,7 +339,7 @@ public Object doWithRetry(RetryContext context) throws Exception {
337339
Timer.Sample sample = BatchMetrics.createTimerSample();
338340
String status = BatchMetrics.STATUS_SUCCESS;
339341
try {
340-
doWrite(outputs.getItems());
342+
doWrite(outputs);
341343
}
342344
catch (Exception e) {
343345
status = BatchMetrics.STATUS_FAILURE;
@@ -590,7 +592,7 @@ private void scan(final StepContribution contribution, final Chunk<I> inputs, fi
590592
}
591593
}
592594

593-
List<O> items = Collections.singletonList(outputIterator.next());
595+
Chunk<O> items = Chunk.of(outputIterator.next());
594596
inputIterator.next();
595597
try {
596598
writeItems(items);

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;

0 commit comments

Comments
 (0)