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
This commit is contained in:
Mahmoud Ben Hassine
2022-08-17 21:06:09 +02:00
parent bf2e6ab0e5
commit e67c0069f1
175 changed files with 1077 additions and 763 deletions

View File

@@ -18,12 +18,13 @@ package org.springframework.batch.core;
import java.util.List;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
/**
* <p>
* Listener interface for the writing of items. Implementations of this interface are
* notified before, after, and in case of any exception thrown while writing a list of
* notified before, after, and in case of any exception thrown while writing a chunk of
* items.
* </p>
*
@@ -42,19 +43,18 @@ import org.springframework.batch.item.ItemWriter;
public interface ItemWriteListener<S> extends StepListener {
/**
* Called before {@link ItemWriter#write(java.util.List)}
* Called before {@link ItemWriter#write(Chunk)}
* @param items to be written
*/
default void beforeWrite(List<? extends S> items) {
default void beforeWrite(Chunk<? extends S> items) {
}
/**
* Called after {@link ItemWriter#write(java.util.List)}. This is called before any
* transaction is committed, and before
* {@link ChunkListener#afterChunk(ChunkContext)}.
* Called after {@link ItemWriter#write(Chunk)}. This is called before any transaction
* is committed, and before {@link ChunkListener#afterChunk(ChunkContext)}.
* @param items written items
*/
default void afterWrite(List<? extends S> items) {
default void afterWrite(Chunk<? extends S> items) {
}
/**
@@ -64,7 +64,7 @@ public interface ItemWriteListener<S> extends StepListener {
* @param exception thrown from {@link ItemWriter}
* @param items attempted to be written.
*/
default void onWriteError(Exception exception, List<? extends S> items) {
default void onWriteError(Exception exception, Chunk<? extends S> items) {
}
}

View File

@@ -27,12 +27,14 @@ import org.springframework.batch.item.ItemWriter;
/**
* Marks a method to be called after an item is passed to an {@link ItemWriter}. Note that
* this annotation takes a {@link List} because Spring Batch generally processes a group
* of items (for the sake of efficiency).<br>
* this annotation takes a {@link org.springframework.batch.item.Chunk} because Spring
* Batch generally processes a group of items (for the sake of efficiency).<br>
* <br>
* Expected signature: void afterWrite({@link List}&lt;? extends S&gt; items)
* Expected signature: void afterWrite({@link org.springframework.batch.item.Chunk}&lt;?
* extends S&gt; items)
*
* @author Lucas Ward
* @author Mahmoud Ben Hassine
* @since 2.0
* @see ItemWriteListener
*/

View File

@@ -26,11 +26,13 @@ import org.springframework.batch.core.ItemWriteListener;
import org.springframework.batch.item.ItemWriter;
/**
* Marks a method to be called before an item is passed to an {@link ItemWriter}. <br>
* Marks a method to be called before a chunk is passed to an {@link ItemWriter}. <br>
* <br>
* Expected signature: void beforeWrite({@link List}&lt;? extends S&gt; items)
* Expected signature: void beforeWrite({@link org.springframework.batch.item.Chunk}&lt;?
* extends S&gt; items)
*
* @author Lucas Ward
* @author Mahmoud Ben Hassine
* @since 2.0
* @see ItemWriteListener
*/

View File

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

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2013 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,11 +19,13 @@ import java.util.Iterator;
import java.util.List;
import org.springframework.batch.core.ItemWriteListener;
import org.springframework.batch.item.Chunk;
import org.springframework.core.Ordered;
/**
* @author Lucas Ward
* @author Dave Syer
* @author Mahmoud Ben Hassine
*
*/
public class CompositeItemWriteListener<S> implements ItemWriteListener<S> {
@@ -50,10 +52,10 @@ public class CompositeItemWriteListener<S> implements ItemWriteListener<S> {
/**
* Call the registered listeners in reverse order, respecting and prioritising those
* that implement {@link Ordered}.
* @see ItemWriteListener#afterWrite(java.util.List)
* @see ItemWriteListener#afterWrite(Chunk)
*/
@Override
public void afterWrite(List<? extends S> items) {
public void afterWrite(Chunk<? extends S> items) {
for (Iterator<ItemWriteListener<? super S>> iterator = listeners.reverse(); iterator.hasNext();) {
ItemWriteListener<? super S> listener = iterator.next();
listener.afterWrite(items);
@@ -63,10 +65,10 @@ public class CompositeItemWriteListener<S> implements ItemWriteListener<S> {
/**
* Call the registered listeners in order, respecting and prioritising those that
* implement {@link Ordered}.
* @see ItemWriteListener#beforeWrite(List)
* @see ItemWriteListener#beforeWrite(Chunk)
*/
@Override
public void beforeWrite(List<? extends S> items) {
public void beforeWrite(Chunk<? extends S> items) {
for (Iterator<ItemWriteListener<? super S>> iterator = listeners.iterator(); iterator.hasNext();) {
ItemWriteListener<? super S> listener = iterator.next();
listener.beforeWrite(items);
@@ -76,10 +78,10 @@ public class CompositeItemWriteListener<S> implements ItemWriteListener<S> {
/**
* Call the registered listeners in reverse order, respecting and prioritising those
* that implement {@link Ordered}.
* @see ItemWriteListener#onWriteError(Exception, List)
* @see ItemWriteListener#onWriteError(Exception, Chunk)
*/
@Override
public void onWriteError(Exception ex, List<? extends S> items) {
public void onWriteError(Exception ex, Chunk<? extends S> items) {
for (Iterator<ItemWriteListener<? super S>> iterator = listeners.reverse(); iterator.hasNext();) {
ItemWriteListener<? super S> listener = iterator.next();
listener.onWriteError(ex, items);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2021 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@ import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.core.StepListener;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemStream;
import org.springframework.lang.Nullable;
@@ -241,10 +242,10 @@ public class MulticasterBatchListener<T, S> implements StepExecutionListener, Ch
}
/**
* @see ItemWriteListener#afterWrite(List)
* @see ItemWriteListener#afterWrite(Chunk)
*/
@Override
public void afterWrite(List<? extends S> items) {
public void afterWrite(Chunk<? extends S> items) {
try {
itemWriteListener.afterWrite(items);
}
@@ -254,10 +255,10 @@ public class MulticasterBatchListener<T, S> implements StepExecutionListener, Ch
}
/**
* @see ItemWriteListener#beforeWrite(List)
* @see ItemWriteListener#beforeWrite(Chunk)
*/
@Override
public void beforeWrite(List<? extends S> items) {
public void beforeWrite(Chunk<? extends S> items) {
try {
itemWriteListener.beforeWrite(items);
}
@@ -267,10 +268,10 @@ public class MulticasterBatchListener<T, S> implements StepExecutionListener, Ch
}
/**
* @see ItemWriteListener#onWriteError(Exception, List)
* @see ItemWriteListener#onWriteError(Exception, Chunk)
*/
@Override
public void onWriteError(Exception ex, List<? extends S> items) {
public void onWriteError(Exception ex, Chunk<? extends S> items) {
try {
itemWriteListener.onWriteError(ex, items);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -46,6 +46,7 @@ import org.springframework.batch.core.annotation.OnSkipInRead;
import org.springframework.batch.core.annotation.OnSkipInWrite;
import org.springframework.batch.core.annotation.OnWriteError;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.item.Chunk;
/**
* Enumeration for {@link StepListener} meta data, which ties together the names of
@@ -72,10 +73,10 @@ public enum StepListenerMetaData implements ListenerMetaData {
Object.class),
ON_PROCESS_ERROR("onProcessError", "on-process-error-method", OnProcessError.class, ItemProcessListener.class,
Object.class, Exception.class),
BEFORE_WRITE("beforeWrite", "before-write-method", BeforeWrite.class, ItemWriteListener.class, List.class),
AFTER_WRITE("afterWrite", "after-write-method", AfterWrite.class, ItemWriteListener.class, List.class),
BEFORE_WRITE("beforeWrite", "before-write-method", BeforeWrite.class, ItemWriteListener.class, Chunk.class),
AFTER_WRITE("afterWrite", "after-write-method", AfterWrite.class, ItemWriteListener.class, Chunk.class),
ON_WRITE_ERROR("onWriteError", "on-write-error-method", OnWriteError.class, ItemWriteListener.class,
Exception.class, List.class),
Exception.class, Chunk.class),
ON_SKIP_IN_READ("onSkipInRead", "on-skip-in-read-method", OnSkipInRead.class, SkipListener.class, Throwable.class),
ON_SKIP_IN_PROCESS("onSkipInProcess", "on-skip-in-process-method", OnSkipInProcess.class, SkipListener.class,
Object.class, Throwable.class),

View File

@@ -1,249 +0,0 @@
/*
* Copyright 2006-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.step.item;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
* Encapsulation of a list of items to be processed and possibly a list of failed items to
* be skipped. To mark an item as skipped clients should iterate over the chunk using the
* {@link #iterator()} method, and if there is a failure call
* {@link org.springframework.batch.core.step.item.Chunk.ChunkIterator#remove()} on the
* iterator. The skipped items are then available through the chunk.
*
* @author Dave Syer
* @since 2.0
*/
public class Chunk<W> implements Iterable<W> {
private List<W> items = new ArrayList<>();
private List<SkipWrapper<W>> skips = new ArrayList<>();
private List<Exception> errors = new ArrayList<>();
private Object userData;
private boolean end;
private boolean busy;
public Chunk() {
this(null, null);
}
public Chunk(Collection<? extends W> items) {
this(items, null);
}
public Chunk(Collection<? extends W> items, List<SkipWrapper<W>> skips) {
super();
if (items != null) {
this.items = new ArrayList<>(items);
}
if (skips != null) {
this.skips = new ArrayList<>(skips);
}
}
/**
* Add the item to the chunk.
* @param item the item to add
*/
public void add(W item) {
items.add(item);
}
/**
* Clear the items down to signal that we are done.
*/
public void clear() {
items.clear();
skips.clear();
userData = null;
}
/**
* @return a copy of the items to be processed as an unmodifiable list
*/
public List<W> getItems() {
return Collections.unmodifiableList(new ArrayList<>(items));
}
/**
* @return a copy of the skips as an unmodifiable list
*/
public List<SkipWrapper<W>> getSkips() {
return Collections.unmodifiableList(skips);
}
/**
* @return a copy of the anonymous errors as an unmodifiable list
*/
public List<Exception> getErrors() {
return Collections.unmodifiableList(errors);
}
/**
* Register an anonymous skip. To skip an individual item, use
* {@link ChunkIterator#remove()}.
* @param e the exception that caused the skip
*/
public void skip(Exception e) {
errors.add(e);
}
/**
* @return true if there are no items in the chunk
*/
public boolean isEmpty() {
return items.isEmpty();
}
/**
* Get an unmodifiable iterator for the underlying items.
* @see java.lang.Iterable#iterator()
*/
@Override
public ChunkIterator iterator() {
return new ChunkIterator(items);
}
/**
* @return the number of items (excluding skips)
*/
public int size() {
return items.size();
}
/**
* Flag to indicate if the source data is exhausted.
* @return true if there is no more data to process
*/
public boolean isEnd() {
return end;
}
/**
* Set the flag to say that this chunk represents an end of stream (there is no more
* data to process).
*/
public void setEnd() {
this.end = true;
}
/**
* Query the chunk to see if anyone has registered an interest in keeping a reference
* to it.
* @return the busy flag
*/
public boolean isBusy() {
return busy;
}
/**
* Register an interest in the chunk to prevent it from being cleaned up before the
* flag is reset to false.
* @param busy the flag to set
*/
public void setBusy(boolean busy) {
this.busy = busy;
}
/**
* Clear only the skips list.
*/
public void clearSkips() {
skips.clear();
}
public Object getUserData() {
return userData;
}
public void setUserData(Object userData) {
this.userData = userData;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return String.format("[items=%s, skips=%s]", items, skips);
}
/**
* Special iterator for a chunk providing the {@link #remove(Throwable)} method for
* dynamically removing an item and adding it to the skips.
*
* @author Dave Syer
*
*/
public class ChunkIterator implements Iterator<W> {
final private Iterator<W> iterator;
private W next;
public ChunkIterator(List<W> items) {
iterator = items.iterator();
}
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public W next() {
next = iterator.next();
return next;
}
public void remove(Throwable e) {
remove();
skips.add(new SkipWrapper<>(next, e));
}
@Override
public void remove() {
if (next == null) {
if (iterator.hasNext()) {
next = iterator.next();
}
else {
return;
}
}
iterator.remove();
}
@Override
public String toString() {
return String.format("[items=%s, skips=%s]", items, skips);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2019 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.lang.Nullable;
@@ -28,6 +29,7 @@ import org.springframework.lang.Nullable;
* A {@link Tasklet} implementing variations on read-process-write item handling.
*
* @author Dave Syer
* @author Mahmoud Ben Hassine
* @param <I> input item type
*/
public class ChunkOrientedTasklet<I> implements Tasklet {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,9 +17,10 @@
package org.springframework.batch.core.step.item;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.item.Chunk;
/**
* Interface defined for processing {@link Chunk}s.
* Interface defined for processing {@link org.springframework.batch.item.Chunk}s.
*
* @since 2.0
*/

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,10 +17,11 @@
package org.springframework.batch.core.step.item;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.item.Chunk;
/**
* Interface for providing {@link Chunk}s to be processed, used by the
* {@link ChunkOrientedTasklet}
* Interface for providing {@link org.springframework.batch.item.Chunk}s to be processed,
* used by the {@link ChunkOrientedTasklet}
*
* @since 2.0
* @see ChunkOrientedTasklet

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2008 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.listener.ItemListenerSupport;
import org.springframework.batch.item.Chunk;
/**
* Default implementation of the {@link ItemListenerSupport} class that writes all
@@ -28,6 +29,7 @@ import org.springframework.batch.core.listener.ItemListenerSupport;
* object.
*
* @author Lucas Ward
* @author Mahmoud Ben Hassine
*
*/
public class DefaultItemFailureHandler extends ItemListenerSupport<Object, Object> {
@@ -45,7 +47,7 @@ public class DefaultItemFailureHandler extends ItemListenerSupport<Object, Objec
}
@Override
public void onWriteError(Exception ex, List<? extends Object> item) {
public void onWriteError(Exception ex, Chunk<? extends Object> item) {
try {
logger.error("Error encountered while writing item: [ " + item + "]", ex);
}

View File

@@ -34,8 +34,10 @@ import org.springframework.batch.core.step.skip.NonSkippableProcessException;
import org.springframework.batch.core.step.skip.SkipLimitExceededException;
import org.springframework.batch.core.step.skip.SkipListenerFailedException;
import org.springframework.batch.core.step.skip.SkipPolicy;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.SkipWrapper;
import org.springframework.classify.BinaryExceptionClassifier;
import org.springframework.classify.Classifier;
import org.springframework.retry.ExhaustedRetryException;
@@ -337,7 +339,7 @@ public class FaultTolerantChunkProcessor<I, O> extends SimpleChunkProcessor<I, O
Timer.Sample sample = BatchMetrics.createTimerSample();
String status = BatchMetrics.STATUS_SUCCESS;
try {
doWrite(outputs.getItems());
doWrite(outputs);
}
catch (Exception e) {
status = BatchMetrics.STATUS_FAILURE;
@@ -590,7 +592,7 @@ public class FaultTolerantChunkProcessor<I, O> extends SimpleChunkProcessor<I, O
}
}
List<O> items = Collections.singletonList(outputIterator.next());
Chunk<O> items = Chunk.of(outputIterator.next());
inputIterator.next();
try {
writeItems(items);

View File

@@ -23,6 +23,7 @@ import org.springframework.batch.core.step.skip.SkipException;
import org.springframework.batch.core.step.skip.SkipListenerFailedException;
import org.springframework.batch.core.step.skip.SkipPolicy;
import org.springframework.batch.core.step.skip.SkipPolicyFailedException;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.repeat.RepeatOperations;
import org.springframework.classify.BinaryExceptionClassifier;

View File

@@ -26,6 +26,7 @@ import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepListener;
import org.springframework.batch.core.listener.MulticasterBatchListener;
import org.springframework.batch.core.observability.BatchMetrics;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.InitializingBean;
@@ -145,7 +146,7 @@ public class SimpleChunkProcessor<I, O> implements ChunkProcessor<I>, Initializi
* @param items list of items to be written.
* @throws Exception thrown if error occurs.
*/
protected final void doWrite(List<O> items) throws Exception {
protected final void doWrite(Chunk<O> items) throws Exception {
if (itemWriter == null) {
return;
@@ -167,7 +168,7 @@ public class SimpleChunkProcessor<I, O> implements ChunkProcessor<I>, Initializi
* Call the listener's after write method.
* @param items list of items that were just written.
*/
protected final void doAfterWrite(List<O> items) {
protected final void doAfterWrite(Chunk<O> items) {
listener.afterWrite(items);
}
@@ -176,7 +177,7 @@ public class SimpleChunkProcessor<I, O> implements ChunkProcessor<I>, Initializi
* @param e exception that occurred.
* @param items list of items that failed to be written.
*/
protected final void doOnWriteError(Exception e, List<O> items) {
protected final void doOnWriteError(Exception e, Chunk<O> items) {
listener.onWriteError(e, items);
}
@@ -184,7 +185,7 @@ public class SimpleChunkProcessor<I, O> implements ChunkProcessor<I>, Initializi
* @param items list of items to be written.
* @throws Exception thrown if error occurs.
*/
protected void writeItems(List<O> items) throws Exception {
protected void writeItems(Chunk<O> items) throws Exception {
if (itemWriter != null) {
itemWriter.write(items);
}
@@ -267,10 +268,10 @@ public class SimpleChunkProcessor<I, O> implements ChunkProcessor<I>, Initializi
}
/**
* Simple implementation delegates to the {@link #doWrite(List)} method and increments
* the write count in the contribution. Subclasses can handle more complicated
* scenarios, e.g.with fault tolerance. If output items are skipped they should be
* removed from the inputs as well.
* Simple implementation delegates to the {@link #doWrite(Chunk)} method and
* increments the write count in the contribution. Subclasses can handle more
* complicated scenarios, e.g.with fault tolerance. If output items are skipped they
* should be removed from the inputs as well.
* @param contribution the current step contribution
* @param inputs the inputs that gave rise to the outputs
* @param outputs the outputs to write
@@ -280,7 +281,7 @@ public class SimpleChunkProcessor<I, O> implements ChunkProcessor<I>, Initializi
Timer.Sample sample = BatchMetrics.createTimerSample();
String status = BatchMetrics.STATUS_SUCCESS;
try {
doWrite(outputs.getItems());
doWrite(outputs);
}
catch (Exception e) {
/*

View File

@@ -28,6 +28,7 @@ import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepListener;
import org.springframework.batch.core.listener.MulticasterBatchListener;
import org.springframework.batch.core.observability.BatchMetrics;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.repeat.RepeatCallback;
import org.springframework.batch.repeat.RepeatContext;

View File

@@ -1,75 +0,0 @@
/*
* Copyright 2006-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.step.item;
import org.springframework.lang.Nullable;
/**
* Wrapper for an item and its exception if it failed processing.
*
* @author Dave Syer
* @author Mahmoud Ben Hassine
*
*/
public class SkipWrapper<T> {
final private Throwable exception;
final private T item;
/**
* @param item the item being wrapped.
*/
public SkipWrapper(T item) {
this(item, null);
}
/**
* @param e instance of {@link Throwable} that being wrapped.
*/
public SkipWrapper(Throwable e) {
this(null, e);
}
public SkipWrapper(T item, @Nullable Throwable e) {
this.item = item;
this.exception = e;
}
/**
* Public getter for the exception.
* @return the exception
*/
@Nullable
public Throwable getException() {
return exception;
}
/**
* Public getter for the item.
* @return the item
*/
public T getItem() {
return item;
}
@Override
public String toString() {
return String.format("[exception=%s, item=%s]", exception, item);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2009-2012 the original author or authors.
* Copyright 2009-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,16 +17,18 @@ package org.springframework.batch.core.configuration.xml;
import java.util.List;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
/**
* @author Dan Garrette
* @author Mahmoud Ben Hassine
* @since 2.0
*/
public class DummyItemWriter implements ItemWriter<Object> {
@Override
public void write(List<? extends Object> items) throws Exception {
public void write(Chunk<? extends Object> items) throws Exception {
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2010 the original author or authors.
* Copyright 2010-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,11 +18,12 @@ package org.springframework.batch.core.configuration.xml;
import java.util.List;
import org.springframework.batch.core.annotation.AfterWrite;
import org.springframework.batch.item.Chunk;
public class TestPojoListener extends AbstractTestComponent {
@AfterWrite
public void after(List<Object> items) {
public void after(Chunk<Object> items) {
executed = true;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2012 the original author or authors.
* Copyright 2008-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,12 +17,13 @@ package org.springframework.batch.core.configuration.xml;
import java.util.List;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
public class TestWriter extends AbstractTestComponent implements ItemWriter<String> {
@Override
public void write(List<? extends String> items) throws Exception {
public void write(Chunk<? extends String> items) throws Exception {
executed = true;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2013 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,8 @@ import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.support.transaction.TransactionAwareProxyFactory;
import org.springframework.beans.factory.InitializingBean;
@@ -49,7 +51,7 @@ public class EmptyItemWriter<T> implements ItemWriter<T>, InitializingBean {
}
@Override
public void write(List<? extends T> items) {
public void write(Chunk<? extends T> items) {
for (T data : items) {
if (!failed && list.size() == failurePoint) {
failed = true;

View File

@@ -24,10 +24,12 @@ import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.ItemWriteListener;
import org.springframework.batch.item.Chunk;
/**
* @author Lucas Ward
* @author Will Schipp
* @author Mahmoud Ben Hassine
*
*/
class CompositeItemWriteListenerTests {
@@ -46,21 +48,21 @@ class CompositeItemWriteListenerTests {
@Test
void testBeforeWrite() {
List<Object> item = Collections.singletonList(new Object());
Chunk<Object> item = Chunk.of(new Object());
listener.beforeWrite(item);
compositeListener.beforeWrite(item);
}
@Test
void testAfterWrite() {
List<Object> item = Collections.singletonList(new Object());
Chunk<Object> item = Chunk.of(new Object());
listener.afterWrite(item);
compositeListener.afterWrite(item);
}
@Test
void testOnWriteError() {
List<Object> item = Collections.singletonList(new Object());
Chunk<Object> item = Chunk.of(new Object());
Exception ex = new Exception();
listener.onWriteError(ex, item);
compositeListener.onWriteError(ex, item);
@@ -73,7 +75,7 @@ class CompositeItemWriteListenerTests {
add(listener);
}
});
List<Object> item = Collections.singletonList(new Object());
Chunk<Object> item = Chunk.of(new Object());
listener.beforeWrite(item);
compositeListener.beforeWrite(item);
}

View File

@@ -37,6 +37,7 @@ import org.springframework.batch.core.configuration.annotation.JobBuilderFactory
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
@@ -175,7 +176,7 @@ class ItemListenerErrorTests {
private boolean goingToFail = false;
@Override
public void write(List<? extends String> items) throws Exception {
public void write(Chunk<? extends String> items) throws Exception {
if (goingToFail) {
throw new RuntimeException("failure in the writer");
}
@@ -294,21 +295,21 @@ class ItemListenerErrorTests {
}
@Override
public void beforeWrite(List<? extends String> items) {
public void beforeWrite(Chunk<? extends String> items) {
if (methodToThrowExceptionFrom.equals("beforeWrite")) {
throw new RuntimeException("beforeWrite caused this Exception");
}
}
@Override
public void afterWrite(List<? extends String> items) {
public void afterWrite(Chunk<? extends String> items) {
if (methodToThrowExceptionFrom.equals("afterWrite")) {
throw new RuntimeException("afterWrite caused this Exception");
}
}
@Override
public void onWriteError(Exception ex, List<? extends String> item) {
public void onWriteError(Exception ex, Chunk<? extends String> item) {
if (methodToThrowExceptionFrom.equals("onWriteError")) {
throw new RuntimeException("onWriteError caused this Exception");
}

View File

@@ -38,6 +38,7 @@ import org.springframework.batch.core.annotation.BeforeProcess;
import org.springframework.batch.core.annotation.BeforeRead;
import org.springframework.batch.core.annotation.BeforeWrite;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.item.Chunk;
import org.springframework.lang.Nullable;
/**
@@ -703,7 +704,7 @@ class MulticasterBatchListenerTests {
* (java.util.List)
*/
@Override
public void afterWrite(List<? extends String> items) {
public void afterWrite(Chunk<? extends String> items) {
count++;
if (error) {
throw new RuntimeException("listener error");
@@ -718,7 +719,7 @@ class MulticasterBatchListenerTests {
* (java.util.List)
*/
@Override
public void beforeWrite(List<? extends String> items) {
public void beforeWrite(Chunk<? extends String> items) {
count++;
if (error) {
throw new RuntimeException("listener error");
@@ -733,7 +734,7 @@ class MulticasterBatchListenerTests {
* (java.lang.Exception, java.util.List)
*/
@Override
public void onWriteError(Exception exception, List<? extends String> items) {
public void onWriteError(Exception exception, Chunk<? extends String> items) {
count++;
if (error) {
throw new RuntimeException("listener error");

View File

@@ -52,6 +52,7 @@ import org.springframework.batch.core.annotation.OnReadError;
import org.springframework.batch.core.annotation.OnWriteError;
import org.springframework.batch.core.configuration.xml.AbstractTestComponent;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.item.Chunk;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.Ordered;
import org.springframework.lang.Nullable;
@@ -66,6 +67,7 @@ import static org.springframework.batch.core.listener.StepListenerMetaData.AFTER
/**
* @author Lucas Ward
* @author Mahmoud Ben Hassine
*
*/
class StepListenerFactoryBeanTests {
@@ -87,7 +89,7 @@ class StepListenerFactoryBeanTests {
// factoryBean.setMetaDataMap(metaDataMap);
String readItem = "item";
Integer writeItem = 2;
List<Integer> writeItems = Arrays.asList(writeItem);
Chunk<Integer> writeItems = Chunk.of(writeItem);
StepListener listener = (StepListener) factoryBean.getObject();
((StepExecutionListener) listener).beforeStep(stepExecution);
((StepExecutionListener) listener).afterStep(stepExecution);
@@ -283,7 +285,7 @@ class StepListenerFactoryBeanTests {
factoryBean.setDelegate(delegate);
@SuppressWarnings("unchecked")
ItemWriteListener<String> listener = (ItemWriteListener<String>) factoryBean.getObject();
listener.afterWrite(Arrays.asList("foo", "bar"));
listener.afterWrite(Chunk.of("foo", "bar"));
assertTrue(delegate.isExecuted());
}
@@ -291,16 +293,16 @@ class StepListenerFactoryBeanTests {
void testRightSignatureAnnotation() {
AbstractTestComponent delegate = new AbstractTestComponent() {
@AfterWrite
public void aMethod(List<String> items) {
public void aMethod(Chunk<String> chunk) {
executed = true;
assertEquals("foo", items.get(0));
assertEquals("bar", items.get(1));
assertEquals("foo", chunk.getItems().get(0));
assertEquals("bar", chunk.getItems().get(1));
}
};
factoryBean.setDelegate(delegate);
@SuppressWarnings("unchecked")
ItemWriteListener<String> listener = (ItemWriteListener<String>) factoryBean.getObject();
listener.afterWrite(Arrays.asList("foo", "bar"));
listener.afterWrite(Chunk.of("foo", "bar"));
assertTrue(delegate.isExecuted());
}
@@ -330,7 +332,7 @@ class StepListenerFactoryBeanTests {
factoryBean.setMetaDataMap(metaDataMap);
@SuppressWarnings("unchecked")
ItemWriteListener<String> listener = (ItemWriteListener<String>) factoryBean.getObject();
listener.afterWrite(Arrays.asList("foo", "bar"));
listener.afterWrite(Chunk.of("foo", "bar"));
assertTrue(delegate.isExecuted());
}
@@ -338,19 +340,18 @@ class StepListenerFactoryBeanTests {
void testRightSignatureNamedMethod() {
AbstractTestComponent delegate = new AbstractTestComponent() {
@SuppressWarnings("unused")
public void aMethod(List<String> items) {
public void aMethod(Chunk<String> chunk) {
executed = true;
assertEquals("foo", items.get(0));
assertEquals("bar", items.get(1));
assertEquals("foo", chunk.getItems().get(0));
assertEquals("bar", chunk.getItems().get(1));
}
};
factoryBean.setDelegate(delegate);
Map<String, String> metaDataMap = new HashMap<>();
metaDataMap.put(AFTER_WRITE.getPropertyName(), "aMethod");
factoryBean.setMetaDataMap(metaDataMap);
@SuppressWarnings("unchecked")
ItemWriteListener<String> listener = (ItemWriteListener<String>) factoryBean.getObject();
listener.afterWrite(Arrays.asList("foo", "bar"));
listener.afterWrite(Chunk.of("foo", "bar"));
assertTrue(delegate.isExecuted());
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2012 the original author or authors.
* Copyright 2008-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,8 @@ import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
/**
@@ -40,12 +42,12 @@ public class ExampleItemWriter implements ItemWriter<String> {
}
/**
* @see ItemWriter#write(List)
* @see ItemWriter#write(Chunk)
*/
@Override
public void write(List<? extends String> data) throws Exception {
public void write(Chunk<? extends String> data) throws Exception {
log.info(data);
items.addAll(data);
items.addAll(data.getItems());
}
}

View File

@@ -36,6 +36,7 @@ import org.springframework.batch.core.launch.JobOperator;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.ApplicationContext;
@@ -109,7 +110,7 @@ class OptimisticLockingFailureTests {
public static class Writer implements ItemWriter<String> {
@Override
public void write(List<? extends String> items) throws Exception {
public void write(Chunk<? extends String> items) throws Exception {
for (String item : items) {
System.out.println(item);
}

View File

@@ -38,6 +38,7 @@ import org.springframework.batch.core.configuration.annotation.JobBuilderFactory
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.NonTransientResourceException;
@@ -61,6 +62,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
*
* @author Tobias Flohre
* @author Michael Minella
* @author Mahmoud Ben Hassine
*/
class RegisterMultiListenerTests {
@@ -174,8 +176,8 @@ class RegisterMultiListenerTests {
return new ItemWriter<String>() {
@Override
public void write(List<? extends String> items) throws Exception {
if (items.contains("item2")) {
public void write(Chunk<? extends String> chunk) throws Exception {
if (chunk.getItems().contains("item2")) {
throw new MySkippableException();
}
}
@@ -267,16 +269,16 @@ class RegisterMultiListenerTests {
}
@Override
public void beforeWrite(List<? extends String> items) {
public void beforeWrite(Chunk<? extends String> items) {
callChecker.beforeWriteCalled++;
}
@Override
public void afterWrite(List<? extends String> items) {
public void afterWrite(Chunk<? extends String> items) {
}
@Override
public void onWriteError(Exception exception, List<? extends String> items) {
public void onWriteError(Exception exception, Chunk<? extends String> items) {
}
@Override

View File

@@ -26,12 +26,15 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.batch.item.Chunk;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author Dave Syer
* @author Mahmoud Ben Hassine
*
*/
class AlmostStatefulRetryChunkTests {

View File

@@ -27,9 +27,11 @@ import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.item.Chunk;
/**
* @author Dave Syer
* @author Mahmoud Ben Hassine
*
*/
class ChunkOrientedTaskletTests {

View File

@@ -36,6 +36,7 @@ import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.listener.ItemListenerSupport;
import org.springframework.batch.core.step.skip.AlwaysSkipItemSkipPolicy;
import org.springframework.batch.core.step.skip.LimitCheckingItemSkipPolicy;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.support.PassThroughItemProcessor;
@@ -66,11 +67,11 @@ class FaultTolerantChunkProcessorTests {
batchRetryTemplate = new BatchRetryTemplate();
processor = new FaultTolerantChunkProcessor<>(new PassThroughItemProcessor<>(), new ItemWriter<String>() {
@Override
public void write(List<? extends String> items) throws Exception {
if (items.contains("fail")) {
public void write(Chunk<? extends String> chunk) throws Exception {
if (chunk.getItems().contains("fail")) {
throw new RuntimeException("Planned failure!");
}
list.addAll(items);
list.addAll(chunk.getItems());
}
}, batchRetryTemplate);
batchRetryTemplate.setRetryPolicy(new NeverRetryPolicy());
@@ -193,8 +194,8 @@ class FaultTolerantChunkProcessorTests {
processor.setWriteSkipPolicy(new AlwaysSkipItemSkipPolicy());
processor.setItemWriter(new ItemWriter<String>() {
@Override
public void write(List<? extends String> items) throws Exception {
if (items.contains("fail")) {
public void write(Chunk<? extends String> chunk) throws Exception {
if (chunk.getItems().contains("fail")) {
fail("Expected Error!");
}
}
@@ -210,8 +211,8 @@ class FaultTolerantChunkProcessorTests {
processor.setWriteSkipPolicy(new AlwaysSkipItemSkipPolicy());
processor.setItemWriter(new ItemWriter<String>() {
@Override
public void write(List<? extends String> items) throws Exception {
if (items.contains("fail")) {
public void write(Chunk<? extends String> chunk) throws Exception {
if (chunk.getItems().contains("fail")) {
throw new RuntimeException("Expected Exception!");
}
}
@@ -232,8 +233,8 @@ class FaultTolerantChunkProcessorTests {
processor.setWriteSkipPolicy(new AlwaysSkipItemSkipPolicy());
processor.setItemWriter(new ItemWriter<String>() {
@Override
public void write(List<? extends String> items) throws Exception {
if (items.contains("fail")) {
public void write(Chunk<? extends String> chunk) throws Exception {
if (chunk.getItems().contains("fail")) {
throw new RuntimeException("Expected Exception!");
}
}
@@ -276,8 +277,8 @@ class FaultTolerantChunkProcessorTests {
Chunk<String> chunk = new Chunk<>(Arrays.asList("foo", "fail", "bar"));
processor.setListeners(Arrays.asList(new ItemListenerSupport<String, String>() {
@Override
public void afterWrite(List<? extends String> item) {
after.addAll(item);
public void afterWrite(Chunk<? extends String> chunk) {
after.addAll(chunk.getItems());
}
}));
processor.setWriteSkipPolicy(new AlwaysSkipItemSkipPolicy());
@@ -301,18 +302,18 @@ class FaultTolerantChunkProcessorTests {
Chunk<String> chunk = new Chunk<>(Arrays.asList("foo", "bar"));
processor = new FaultTolerantChunkProcessor<>(new PassThroughItemProcessor<>(), new ItemWriter<String>() {
@Override
public void write(List<? extends String> items) throws Exception {
public void write(Chunk<? extends String> chunk) throws Exception {
// Fail if there is more than one item
if (items.size() > 1) {
if (chunk.size() > 1) {
throw new RuntimeException("Planned failure!");
}
list.addAll(items);
list.addAll(chunk.getItems());
}
}, batchRetryTemplate);
processor.setListeners(Arrays.asList(new ItemListenerSupport<String, String>() {
@Override
public void afterWrite(List<? extends String> item) {
after.addAll(item);
public void afterWrite(Chunk<? extends String> chunk) {
after.addAll(chunk.getItems());
}
}));
processor.setWriteSkipPolicy(new AlwaysSkipItemSkipPolicy());
@@ -330,8 +331,8 @@ class FaultTolerantChunkProcessorTests {
Chunk<String> chunk = new Chunk<>(Arrays.asList("foo", "fail"));
processor.setListeners(Arrays.asList(new ItemListenerSupport<String, String>() {
@Override
public void onWriteError(Exception e, List<? extends String> item) {
writeError.addAll(item);
public void onWriteError(Exception e, Chunk<? extends String> chunk) {
writeError.addAll(chunk.getItems());
}
}));
processor.setWriteSkipPolicy(new AlwaysSkipItemSkipPolicy());
@@ -348,15 +349,15 @@ class FaultTolerantChunkProcessorTests {
Chunk<String> chunk = new Chunk<>(Arrays.asList("foo", "bar"));
processor = new FaultTolerantChunkProcessor<>(new PassThroughItemProcessor<>(), new ItemWriter<String>() {
@Override
public void write(List<? extends String> items) throws Exception {
public void write(Chunk<? extends String> items) throws Exception {
// Always fail in writer
throw new RuntimeException("Planned failure!");
}
}, batchRetryTemplate);
processor.setListeners(Arrays.asList(new ItemListenerSupport<String, String>() {
@Override
public void onWriteError(Exception e, List<? extends String> item) {
writeError.addAll(item);
public void onWriteError(Exception e, Chunk<? extends String> chunk) {
writeError.addAll(chunk.getItems());
}
}));
processor.setWriteSkipPolicy(new AlwaysSkipItemSkipPolicy());
@@ -376,8 +377,8 @@ class FaultTolerantChunkProcessorTests {
processor.setWriteSkipPolicy(new AlwaysSkipItemSkipPolicy());
processor.setItemWriter(new ItemWriter<String>() {
@Override
public void write(List<? extends String> items) throws Exception {
if (items.contains("fail")) {
public void write(Chunk<? extends String> chunk) throws Exception {
if (chunk.getItems().contains("fail")) {
throw new IllegalArgumentException("Expected Exception!");
}
}
@@ -408,8 +409,8 @@ class FaultTolerantChunkProcessorTests {
processor.setWriteSkipPolicy(new AlwaysSkipItemSkipPolicy());
processor.setItemWriter(new ItemWriter<String>() {
@Override
public void write(List<? extends String> items) throws Exception {
if (items.contains("fail")) {
public void write(Chunk<? extends String> chunk) throws Exception {
if (chunk.getItems().contains("fail")) {
throw new IllegalArgumentException("Expected Exception!");
}
}
@@ -445,11 +446,11 @@ class FaultTolerantChunkProcessorTests {
Collections.<Class<? extends Throwable>, Boolean>singletonMap(IllegalArgumentException.class, true)));
processor.setItemWriter(new ItemWriter<String>() {
@Override
public void write(List<? extends String> items) throws Exception {
if (items.contains("fail")) {
public void write(Chunk<? extends String> chunk) throws Exception {
if (chunk.getItems().contains("fail")) {
throw new IllegalArgumentException("Expected Exception!");
}
if (items.contains("2")) {
if (chunk.getItems().contains("2")) {
throw new RuntimeException("Expected Non-Skippable Exception!");
}
}

View File

@@ -28,6 +28,7 @@ import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.step.skip.LimitCheckingItemSkipPolicy;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;

View File

@@ -35,6 +35,7 @@ import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.step.JobRepositorySupport;
import org.springframework.batch.core.step.factory.FaultTolerantStepFactoryBean;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
@@ -141,7 +142,7 @@ class FaultTolerantStepFactoryBeanNonBufferingTests {
}
@Override
public void write(List<? extends String> items) throws Exception {
public void write(Chunk<? extends String> items) throws Exception {
logger.debug("Writing: " + items);
for (String item : items) {
if (failures.contains(item)) {

View File

@@ -41,6 +41,7 @@ import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
import org.springframework.batch.core.step.AbstractStep;
import org.springframework.batch.core.step.factory.FaultTolerantStepFactoryBean;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
@@ -91,8 +92,8 @@ class FaultTolerantStepFactoryBeanRetryTests {
private ItemWriter<String> writer = new ItemWriter<String>() {
@Override
public void write(List<? extends String> data) throws Exception {
processed.addAll(data);
public void write(Chunk<? extends String> data) throws Exception {
processed.addAll(data.getItems());
}
};
@@ -152,7 +153,7 @@ class FaultTolerantStepFactoryBeanRetryTests {
factory.setTransactionManager(new ResourcelessTransactionManager());
ItemWriter<Integer> failingWriter = new ItemWriter<Integer>() {
@Override
public void write(List<? extends Integer> data) throws Exception {
public void write(Chunk<? extends Integer> data) throws Exception {
int count = 0;
for (Integer item : data) {
if (count++ == 2) {
@@ -202,7 +203,7 @@ class FaultTolerantStepFactoryBeanRetryTests {
final List<String> ITEM_LIST = Arrays.asList("a", "b", "c");
ItemWriter<String> failingWriter = new ItemWriter<String>() {
@Override
public void write(List<? extends String> data) throws Exception {
public void write(Chunk<? extends String> data) throws Exception {
int count = 0;
for (String item : data) {
if (count++ == 2) {
@@ -248,7 +249,7 @@ class FaultTolerantStepFactoryBeanRetryTests {
void testNoItemsReprocessedWhenErrorInWriterAndProcessorNotTransactional() throws Exception {
ItemWriter<String> failingWriter = new ItemWriter<String>() {
@Override
public void write(List<? extends String> data) throws Exception {
public void write(Chunk<? extends String> data) throws Exception {
int count = 0;
for (String item : data) {
if (count++ == 2) {
@@ -358,11 +359,11 @@ class FaultTolerantStepFactoryBeanRetryTests {
factory.setStreams(new ItemStream[] { reader });
factory.setItemWriter(new ItemWriter<String>() {
@Override
public void write(List<? extends String> items) throws Exception {
if (fail && items.contains("e")) {
public void write(Chunk<? extends String> chunk) throws Exception {
if (fail && chunk.getItems().contains("e")) {
throw new RuntimeException("Planned failure");
}
processed.addAll(items);
processed.addAll(chunk.getItems());
}
});
factory.setRetryLimit(0);
@@ -445,11 +446,11 @@ class FaultTolerantStepFactoryBeanRetryTests {
ItemWriter<String> itemWriter = new ItemWriter<String>() {
@Override
public void write(List<? extends String> item) throws Exception {
logger.debug("Write Called! Item: [" + item + "]");
processed.addAll(item);
written.addAll(item);
if (item.contains("b") || item.contains("d")) {
public void write(Chunk<? extends String> chunk) throws Exception {
logger.debug("Write Called! Item: [" + chunk.getItems() + "]");
processed.addAll(chunk.getItems());
written.addAll(chunk.getItems());
if (chunk.getItems().contains("b") || chunk.getItems().contains("d")) {
throw new RuntimeException("Write error - planned but recoverable.");
}
}
@@ -503,11 +504,11 @@ class FaultTolerantStepFactoryBeanRetryTests {
ItemWriter<String> itemWriter = new ItemWriter<String>() {
@Override
public void write(List<? extends String> item) throws Exception {
logger.debug("Write Called! Item: [" + item + "]");
processed.addAll(item);
written.addAll(item);
if (item.contains("b") || item.contains("d")) {
public void write(Chunk<? extends String> chunk) throws Exception {
logger.debug("Write Called! Item: [" + chunk + "]");
processed.addAll(chunk.getItems());
written.addAll(chunk.getItems());
if (chunk.getItems().contains("b") || chunk.getItems().contains("d")) {
throw new RuntimeException("Write error - planned but recoverable.");
}
}
@@ -556,10 +557,10 @@ class FaultTolerantStepFactoryBeanRetryTests {
};
ItemWriter<String> itemWriter = new ItemWriter<String>() {
@Override
public void write(List<? extends String> item) throws Exception {
processed.addAll(item);
written.addAll(item);
logger.debug("Write Called! Item: [" + item + "]");
public void write(Chunk<? extends String> chunk) throws Exception {
processed.addAll(chunk.getItems());
written.addAll(chunk.getItems());
logger.debug("Write Called! Item: [" + chunk.getItems() + "]");
throw new RuntimeException("Write error - planned but retryable.");
}
};
@@ -608,10 +609,10 @@ class FaultTolerantStepFactoryBeanRetryTests {
};
ItemWriter<String> itemWriter = new ItemWriter<String>() {
@Override
public void write(List<? extends String> item) throws Exception {
processed.addAll(item);
written.addAll(item);
logger.debug("Write Called! Item: [" + item + "]");
public void write(Chunk<? extends String> chunk) throws Exception {
processed.addAll(chunk.getItems());
written.addAll(chunk.getItems());
logger.debug("Write Called! Item: [" + chunk.getItems() + "]");
throw new RuntimeException("Write error - planned but not skippable.");
}
};
@@ -655,10 +656,10 @@ class FaultTolerantStepFactoryBeanRetryTests {
};
ItemWriter<String> itemWriter = new ItemWriter<String>() {
@Override
public void write(List<? extends String> item) throws Exception {
processed.addAll(item);
written.addAll(item);
logger.debug("Write Called! Item: [" + item + "]");
public void write(Chunk<? extends String> chunk) throws Exception {
processed.addAll(chunk.getItems());
written.addAll(chunk.getItems());
logger.debug("Write Called! Item: [" + chunk.getItems() + "]");
throw new RuntimeException("Write error - planned but retryable.");
}
};
@@ -707,9 +708,9 @@ class FaultTolerantStepFactoryBeanRetryTests {
};
ItemWriter<String> itemWriter = new ItemWriter<String>() {
@Override
public void write(List<? extends String> item) throws Exception {
processed.addAll(item);
logger.debug("Write Called! Item: [" + item + "]");
public void write(Chunk<? extends String> chunk) throws Exception {
processed.addAll(chunk.getItems());
logger.debug("Write Called! Item: [" + chunk.getItems() + "]");
throw new RuntimeException("Write error - planned but retryable.");
}
};

View File

@@ -49,6 +49,7 @@ import org.springframework.batch.core.step.factory.FaultTolerantStepFactoryBean;
import org.springframework.batch.core.step.skip.LimitCheckingItemSkipPolicy;
import org.springframework.batch.core.step.skip.SkipLimitExceededException;
import org.springframework.batch.core.step.skip.SkipPolicy;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemStream;
@@ -451,7 +452,7 @@ public class FaultTolerantStepFactoryBeanTests {
factory.setSkippableExceptionClasses(map);
factory.setItemWriter(new ItemWriter<String>() {
@Override
public void write(List<? extends String> items) {
public void write(Chunk<? extends String> items) {
throw new FatalRuntimeException("Ouch!");
}
});
@@ -766,8 +767,8 @@ public class FaultTolerantStepFactoryBeanTests {
ItemProcessListener<String, String>, SkipListener<String, String>, ChunkListener {
@Override
public void write(List<? extends String> items) throws Exception {
if (items.contains("4")) {
public void write(Chunk<? extends String> chunk) throws Exception {
if (chunk.getItems().contains("4")) {
throw new SkippableException("skippable");
}
}
@@ -786,16 +787,16 @@ public class FaultTolerantStepFactoryBeanTests {
}
@Override
public void afterWrite(List<? extends String> items) {
public void afterWrite(Chunk<? extends String> items) {
listenerCalls.add(2);
}
@Override
public void beforeWrite(List<? extends String> items) {
public void beforeWrite(Chunk<? extends String> items) {
}
@Override
public void onWriteError(Exception exception, List<? extends String> items) {
public void onWriteError(Exception exception, Chunk<? extends String> items) {
}
@Override

View File

@@ -19,6 +19,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@@ -33,6 +34,7 @@ import java.util.List;
* </p>
*
* @author Chris Schaefer
* @author Mahmoud Ben Hassine
* @since 3.1
*/
@SpringJUnitConfig
@@ -52,12 +54,12 @@ class ScriptItemProcessorTests {
public static class TestItemWriter implements ItemWriter<String> {
@Override
public void write(List<? extends String> items) throws Exception {
Assert.notNull(items, "Items cannot be null");
Assert.isTrue(!items.isEmpty(), "Items cannot be empty");
Assert.isTrue(items.size() == 1, "Items should only contain one entry");
public void write(Chunk<? extends String> chunk) throws Exception {
Assert.notNull(chunk.getItems(), "Items cannot be null");
Assert.isTrue(!chunk.getItems().isEmpty(), "Items cannot be empty");
Assert.isTrue(chunk.getItems().size() == 1, "Items should only contain one entry");
String item = items.get(0);
String item = chunk.getItems().get(0);
Assert.isTrue("BLAH".equals(item), "Transformed item to write should have been: BLAH but got: " + item);
}

View File

@@ -28,6 +28,7 @@ import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemWriter;
import org.springframework.lang.Nullable;
@@ -46,11 +47,11 @@ class SimpleChunkProcessorTests {
}
}, new ItemWriter<String>() {
@Override
public void write(List<? extends String> items) throws Exception {
if (items.contains("fail")) {
public void write(Chunk<? extends String> chunk) throws Exception {
if (chunk.getItems().contains("fail")) {
throw new RuntimeException("Planned failure!");
}
list.addAll(items);
list.addAll(chunk.getItems());
}
});

View File

@@ -26,6 +26,7 @@ import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.batch.repeat.support.RepeatTemplate;

View File

@@ -46,6 +46,7 @@ import org.springframework.batch.core.repository.support.JobRepositoryFactoryBea
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.AbstractStep;
import org.springframework.batch.core.step.factory.SimpleStepFactoryBean;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
@@ -72,8 +73,8 @@ class SimpleStepFactoryBeanTests {
private final ItemWriter<String> writer = new ItemWriter<String>() {
@Override
public void write(List<? extends String> data) throws Exception {
written.addAll(data);
public void write(Chunk<? extends String> data) throws Exception {
written.addAll(data.getItems());
}
};
@@ -174,7 +175,7 @@ class SimpleStepFactoryBeanTests {
factory.setItemWriter(new ItemWriter<String>() {
@Override
public void write(List<? extends String> data) throws Exception {
public void write(Chunk<? extends String> data) throws Exception {
throw new RuntimeException("Error!");
}
});
@@ -185,7 +186,7 @@ class SimpleStepFactoryBeanTests {
}
@Override
public void onWriteError(Exception ex, List<? extends String> item) {
public void onWriteError(Exception ex, Chunk<? extends String> item) {
listened.add(ex);
}
} });
@@ -212,7 +213,7 @@ class SimpleStepFactoryBeanTests {
factory.setBeanName("exceptionStep");
factory.setItemWriter(new ItemWriter<String>() {
@Override
public void write(List<? extends String> data) throws Exception {
public void write(Chunk<? extends String> data) throws Exception {
throw new RuntimeException("Foo");
}
});
@@ -237,7 +238,7 @@ class SimpleStepFactoryBeanTests {
int count = 0;
@Override
public void write(List<? extends String> data) throws Exception {
public void write(Chunk<? extends String> data) throws Exception {
if (count++ == 0) {
throw new RuntimeException("Foo");
}
@@ -264,8 +265,8 @@ class SimpleStepFactoryBeanTests {
String trail = "";
@Override
public void beforeWrite(List<? extends Object> items) {
if (items.contains("error")) {
public void beforeWrite(Chunk<? extends Object> chunk) {
if (chunk.getItems().contains("error")) {
throw new RuntimeException("rollback the last chunk");
}
@@ -273,7 +274,7 @@ class SimpleStepFactoryBeanTests {
}
@Override
public void afterWrite(List<? extends Object> items) {
public void afterWrite(Chunk<? extends Object> items) {
trail = trail + "3";
}
@@ -379,7 +380,7 @@ class SimpleStepFactoryBeanTests {
ItemWriteListener<String>, ItemProcessListener<String, String>, ChunkListener {
@Override
public void write(List<? extends String> items) throws Exception {
public void write(Chunk<? extends String> items) throws Exception {
}
@Nullable
@@ -402,16 +403,16 @@ class SimpleStepFactoryBeanTests {
}
@Override
public void afterWrite(List<? extends String> items) {
public void afterWrite(Chunk<? extends String> items) {
listenerCalls.add("write");
}
@Override
public void beforeWrite(List<? extends String> items) {
public void beforeWrite(Chunk<? extends String> items) {
}
@Override
public void onWriteError(Exception exception, List<? extends String> items) {
public void onWriteError(Exception exception, Chunk<? extends String> items) {
}
@Override
@@ -470,20 +471,20 @@ class SimpleStepFactoryBeanTests {
class TestItemListenerWriter implements ItemWriter<String>, ItemWriteListener<String> {
@Override
public void write(List<? extends String> items) throws Exception {
public void write(Chunk<? extends String> items) throws Exception {
}
@Override
public void afterWrite(List<? extends String> items) {
public void afterWrite(Chunk<? extends String> items) {
listenerCalls.add("write");
}
@Override
public void beforeWrite(List<? extends String> items) {
public void beforeWrite(Chunk<? extends String> items) {
}
@Override
public void onWriteError(Exception exception, List<? extends String> items) {
public void onWriteError(Exception exception, Chunk<? extends String> items) {
}
}

View File

@@ -21,8 +21,11 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import org.springframework.batch.item.SkipWrapper;
/**
* @author Dave Syer
* @author Mahmoud Ben Hassine
*
*/
class SkipWrapperTests {
@@ -41,7 +44,7 @@ class SkipWrapperTests {
/**
* Test method for
* {@link org.springframework.batch.core.step.item.SkipWrapper#SkipWrapper(java.lang.Object, java.lang.Throwable)}.
* {@link SkipWrapper#SkipWrapper(java.lang.Object, java.lang.Throwable)}.
*/
@Test
void testItemWrapperTException() {
@@ -51,8 +54,7 @@ class SkipWrapperTests {
}
/**
* Test method for
* {@link org.springframework.batch.core.step.item.SkipWrapper#toString()}.
* Test method for {@link SkipWrapper#toString()}.
*/
@Test
void testToString() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2013 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,11 +18,13 @@ package org.springframework.batch.core.step.item;
import java.util.ArrayList;
import java.util.List;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.support.transaction.TransactionAwareProxyFactory;
/**
* @author Dan Garrette
* @author Mahmoud Ben Hassine
* @since 2.0.1
*/
public class SkipWriterStub<T> extends AbstractExceptionThrowingItemHandlerStub<T> implements ItemWriter<T> {
@@ -49,7 +51,7 @@ public class SkipWriterStub<T> extends AbstractExceptionThrowingItemHandlerStub<
}
@Override
public void write(List<? extends T> items) throws Exception {
public void write(Chunk<? extends T> items) throws Exception {
logger.debug("Writing: " + items);
for (T item : items) {
written.add(item);

View File

@@ -21,6 +21,7 @@ import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
@@ -33,6 +34,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author mminella
* @author Mahmoud Ben Hassine
*/
@SpringJUnitConfig
public class ReprocessExceptionTests {
@@ -77,7 +79,7 @@ public class ReprocessExceptionTests {
public static class PersonItemWriter implements ItemWriter<Person> {
@Override
public void write(List<? extends Person> persons) throws Exception {
public void write(Chunk<? extends Person> persons) throws Exception {
for (Person person : persons) {
System.out.println(person.getFirstName() + " " + person.getLastName());
if (person.getFirstName().equals("JANE")) {

View File

@@ -34,6 +34,7 @@ import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.job.JobSupport;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.support.ListItemReader;
@@ -53,6 +54,7 @@ import static org.junit.jupiter.api.Assertions.assertNotSame;
/**
* @author Dave Syer
* @author Mahmoud Ben Hassine
*
*/
@SpringJUnitConfig(locations = "/org/springframework/batch/core/repository/dao/sql-dao-test.xml")
@@ -125,8 +127,8 @@ class AsyncChunkOrientedStepIntegrationTests {
getReader(new String[] { "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c" }),
new ItemWriter<String>() {
@Override
public void write(List<? extends String> data) throws Exception {
written.addAll(data);
public void write(Chunk<? extends String> data) throws Exception {
written.addAll(data.getItems());
}
}, chunkOperations));

View File

@@ -33,6 +33,7 @@ import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.JobRepositorySupport;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemStreamSupport;
@@ -59,11 +60,11 @@ class AsyncTaskletStepTests {
ItemWriter<String> itemWriter = new ItemWriter<String>() {
@Override
public void write(List<? extends String> data) throws Exception {
public void write(Chunk<? extends String> data) throws Exception {
// Thread.sleep(100L);
logger.info("Items: " + data);
processed.addAll(data);
if (data.contains("fail")) {
processed.addAll(data.getItems());
if (data.getItems().contains("fail")) {
throw new RuntimeException("Planned");
}
}

View File

@@ -37,6 +37,7 @@ import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteExcep
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.repeat.policy.SimpleCompletionPolicy;
@@ -88,7 +89,7 @@ class StepExecutorInterruptionTests {
step.setTransactionManager(this.transactionManager);
itemWriter = new ItemWriter<Object>() {
@Override
public void write(List<? extends Object> item) throws Exception {
public void write(Chunk<? extends Object> item) throws Exception {
}
};
stepExecution = new StepExecution(step.getName(), jobExecution);

View File

@@ -45,6 +45,7 @@ import org.springframework.batch.core.repository.support.JobRepositoryFactoryBea
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.JobRepositorySupport;
import org.springframework.batch.core.step.StepInterruptionPolicy;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemStream;
@@ -75,8 +76,8 @@ class TaskletStepTests {
ItemWriter<String> itemWriter = new ItemWriter<String>() {
@Override
public void write(List<? extends String> data) throws Exception {
processed.addAll(data);
public void write(Chunk<? extends String> data) throws Exception {
processed.addAll(data.getItems());
}
};

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ package org.springframework.batch.core.test.football.internal;
import java.util.List;
import org.springframework.batch.core.test.football.domain.Game;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
@@ -38,7 +39,7 @@ public class JdbcGameDao extends JdbcDaoSupport implements ItemWriter<Game> {
}
@Override
public void write(List<? extends Game> games) {
public void write(Chunk<? extends Game> games) {
for (Game game : games) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ package org.springframework.batch.core.test.football.internal;
import java.util.List;
import org.springframework.batch.core.test.football.domain.PlayerSummary;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
@@ -35,7 +36,7 @@ public class JdbcPlayerSummaryDao implements ItemWriter<PlayerSummary> {
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
@Override
public void write(List<? extends PlayerSummary> summaries) {
public void write(Chunk<? extends PlayerSummary> summaries) {
for (PlayerSummary summary : summaries) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import java.util.List;
import org.springframework.batch.core.test.football.domain.Player;
import org.springframework.batch.core.test.football.domain.PlayerDao;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
public class PlayerItemWriter implements ItemWriter<Player> {
@@ -27,7 +28,7 @@ public class PlayerItemWriter implements ItemWriter<Player> {
private PlayerDao playerDao;
@Override
public void write(List<? extends Player> players) throws Exception {
public void write(Chunk<? extends Player> players) throws Exception {
for (Player player : players) {
playerDao.savePlayer(player);
}

View File

@@ -39,6 +39,7 @@ import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.factory.FaultTolerantStepFactoryBean;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
@@ -111,7 +112,7 @@ class FaultTolerantStepFactoryBeanIntegrationTests {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
writer.write(Arrays.asList("foo", "bar"));
writer.write(Chunk.of("foo", "bar"));
processor.process("spam");
assertEquals(3, JdbcTestUtils.countRowsInTable(jdbcTemplate, "ERROR_LOG"));
@@ -228,7 +229,7 @@ class FaultTolerantStepFactoryBeanIntegrationTests {
}
@Override
public void write(List<? extends String> items) throws Exception {
public void write(Chunk<? extends String> items) throws Exception {
for (String item : items) {
written.add(item);
jdbcTemplate.update("INSERT INTO ERROR_LOG (MESSAGE, STEP_NAME) VALUES (?, ?)", item, "written");

View File

@@ -39,6 +39,7 @@ import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.factory.FaultTolerantStepFactoryBean;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
@@ -108,7 +109,7 @@ class FaultTolerantStepFactoryBeanRollbackIntegrationTests {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
writer.write(Arrays.asList("foo", "bar"));
writer.write(Chunk.of("foo", "bar"));
processor.process("spam");
assertEquals(3, JdbcTestUtils.countRowsInTable(jdbcTemplate, "ERROR_LOG"));
@@ -253,7 +254,7 @@ class FaultTolerantStepFactoryBeanRollbackIntegrationTests {
}
@Override
public void write(List<? extends String> items) throws Exception {
public void write(Chunk<? extends String> items) throws Exception {
for (String item : items) {
written.add(item);
jdbcTemplate.update("INSERT INTO ERROR_LOG (MESSAGE, STEP_NAME) VALUES (?, ?)", item, "written");

View File

@@ -35,6 +35,7 @@ import org.springframework.batch.core.step.builder.FaultTolerantStepBuilder;
import org.springframework.batch.core.step.skip.AlwaysSkipItemSkipPolicy;
import org.springframework.batch.core.step.skip.SkipLimitExceededException;
import org.springframework.batch.core.step.skip.SkipPolicy;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
@@ -71,7 +72,7 @@ class FaultTolerantStepIntegrationTests {
void setUp() {
ItemReader<Integer> itemReader = new ListItemReader<>(createItems());
ItemWriter<Integer> itemWriter = chunk -> {
if (chunk.contains(1)) {
if (chunk.getItems().contains(1)) {
throw new IllegalArgumentException();
}
};
@@ -169,7 +170,7 @@ class FaultTolerantStepIntegrationTests {
private int cpt;
@Override
public void write(List<? extends Integer> items) throws Exception {
public void write(Chunk<? extends Integer> items) throws Exception {
cpt++;
if (cpt == 1) {
throw new Exception("Error during write");
@@ -210,8 +211,8 @@ class FaultTolerantStepIntegrationTests {
ItemWriter<Integer> itemWriter = new ItemWriter<Integer>() {
@Override
public void write(List<? extends Integer> items) throws Exception {
if (items.contains(3)) {
public void write(Chunk<? extends Integer> chunk) throws Exception {
if (chunk.getItems().contains(3)) {
throw new Exception("Error during write");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,8 @@ import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
public class LoggingItemWriter<T> implements ItemWriter<T> {
@@ -26,7 +28,7 @@ public class LoggingItemWriter<T> implements ItemWriter<T> {
protected Log logger = LogFactory.getLog(LoggingItemWriter.class);
@Override
public void write(List<? extends T> items) throws Exception {
public void write(Chunk<? extends T> items) throws Exception {
logger.info(items);
}