Skip to content

Commit 234e28a

Browse files
hpoettkerfmbenhassine
authored andcommitted
Allow lambdas to be passed as item processors
Resolves #4061
1 parent 33338a4 commit 234e28a

File tree

3 files changed

+5
-29
lines changed

3 files changed

+5
-29
lines changed

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

Lines changed: 1 addition & 23 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.
@@ -20,7 +20,6 @@
2020
import java.util.HashSet;
2121
import java.util.LinkedHashSet;
2222
import java.util.Set;
23-
import java.util.function.Function;
2423

2524
import org.springframework.batch.core.ChunkListener;
2625
import org.springframework.batch.core.ItemProcessListener;
@@ -47,7 +46,6 @@
4746
import org.springframework.batch.item.ItemReader;
4847
import org.springframework.batch.item.ItemStream;
4948
import org.springframework.batch.item.ItemWriter;
50-
import org.springframework.batch.item.function.FunctionItemProcessor;
5149
import org.springframework.batch.repeat.CompletionPolicy;
5250
import org.springframework.batch.repeat.RepeatOperations;
5351
import org.springframework.batch.repeat.policy.SimpleCompletionPolicy;
@@ -78,8 +76,6 @@ public class SimpleStepBuilder<I, O> extends AbstractTaskletStepBuilder<SimpleSt
7876

7977
private ItemProcessor<? super I, ? extends O> processor;
8078

81-
private Function<? super I, ? extends O> itemProcessorFunction;
82-
8379
private int chunkSize = 0;
8480

8581
private RepeatOperations chunkOperations;
@@ -112,7 +108,6 @@ protected SimpleStepBuilder(SimpleStepBuilder<I, O> parent) {
112108
this.reader = parent.reader;
113109
this.writer = parent.writer;
114110
this.processor = parent.processor;
115-
this.itemProcessorFunction = parent.itemProcessorFunction;
116111
this.itemListeners = parent.itemListeners;
117112
this.readerTransactionalQueue = parent.readerTransactionalQueue;
118113
}
@@ -236,19 +231,6 @@ public SimpleStepBuilder<I, O> processor(ItemProcessor<? super I, ? extends O> p
236231
return this;
237232
}
238233

239-
/**
240-
* A {@link Function} to be delegated to as an {@link ItemProcessor}. If this is set,
241-
* it will take precedence over any {@code ItemProcessor} configured via
242-
* {@link #processor(ItemProcessor)}.
243-
*
244-
* @param function the function to delegate item processing to
245-
* @return this for fluent chaining
246-
*/
247-
public SimpleStepBuilder<I, O> processor(Function<? super I, ? extends O> function) {
248-
this.itemProcessorFunction = function;
249-
return this;
250-
}
251-
252234
/**
253235
* Sets a flag to say that the reader is transactional (usually a queue), which is to say that failed items might be
254236
* rolled back and re-presented in a subsequent transaction. Default is false, meaning that the items are read
@@ -359,10 +341,6 @@ protected ItemWriter<? super O> getWriter() {
359341
}
360342

361343
protected ItemProcessor<? super I, ? extends O> getProcessor() {
362-
if(this.itemProcessorFunction != null) {
363-
this.processor = new FunctionItemProcessor<>(this.itemProcessorFunction);
364-
}
365-
366344
return processor;
367345
}
368346

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-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,7 +17,6 @@
1717

1818
import java.util.Arrays;
1919
import java.util.List;
20-
import java.util.function.Function;
2120

2221
import org.junit.Before;
2322
import org.junit.Test;
@@ -245,7 +244,7 @@ private void assertStepFunctions(boolean faultTolerantStep) throws Exception {
245244
.transactionManager(transactionManager)
246245
.<Object, String>chunk(3)
247246
.reader(reader)
248-
.processor((Function<Object, String>) s -> s.toString())
247+
.processor(Object::toString)
249248
.writer(itemWriter)
250249
.listener(new AnnotationBasedStepExecutionListener());
251250

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2021 the original author or authors.
2+
* Copyright 2010-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.
@@ -70,7 +70,6 @@ public class FaultTolerantStepIntegrationTests {
7070
@Before
7171
public void setUp() {
7272
ItemReader<Integer> itemReader = new ListItemReader<>(createItems());
73-
ItemProcessor<Integer, Integer> itemProcessor = item -> item > 20 ? null : item;
7473
ItemWriter<Integer> itemWriter = chunk -> {
7574
if (chunk.contains(1)) {
7675
throw new IllegalArgumentException();
@@ -80,7 +79,7 @@ public void setUp() {
8079
stepBuilder = new StepBuilderFactory(jobRepository, transactionManager).get("step")
8180
.<Integer, Integer>chunk(CHUNK_SIZE)
8281
.reader(itemReader)
83-
.processor(itemProcessor)
82+
.processor(item -> item > 20 ? null : item)
8483
.writer(itemWriter)
8584
.faultTolerant();
8685
}

0 commit comments

Comments
 (0)