Use lambdas and method references where appropriate
This commit is contained in:
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
package org.springframework.batch.integration.async;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.FutureTask;
|
||||
|
||||
@@ -90,18 +89,16 @@ public class AsyncItemProcessor<I, O> implements ItemProcessor<I, Future<O>>, In
|
||||
@Nullable
|
||||
public Future<O> process(final I item) throws Exception {
|
||||
final StepExecution stepExecution = getStepExecution();
|
||||
FutureTask<O> task = new FutureTask<>(new Callable<>() {
|
||||
public O call() throws Exception {
|
||||
FutureTask<O> task = new FutureTask<>(() -> {
|
||||
if (stepExecution != null) {
|
||||
StepSynchronizationManager.register(stepExecution);
|
||||
}
|
||||
try {
|
||||
return delegate.process(item);
|
||||
}
|
||||
finally {
|
||||
if (stepExecution != null) {
|
||||
StepSynchronizationManager.register(stepExecution);
|
||||
}
|
||||
try {
|
||||
return delegate.process(item);
|
||||
}
|
||||
finally {
|
||||
if (stepExecution != null) {
|
||||
StepSynchronizationManager.close();
|
||||
}
|
||||
StepSynchronizationManager.close();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -253,30 +253,25 @@ public class MessageChannelPartitionHandler extends AbstractPartitionHandler imp
|
||||
throws Exception {
|
||||
final Set<StepExecution> result = new HashSet<>(split.size());
|
||||
|
||||
Callable<Set<StepExecution>> callback = new Callable<>() {
|
||||
@Override
|
||||
public Set<StepExecution> call() throws Exception {
|
||||
Set<Long> currentStepExecutionIds = split.stream()
|
||||
.map(StepExecution::getId)
|
||||
.collect(Collectors.toSet());
|
||||
JobExecution jobExecution = jobExplorer.getJobExecution(managerStepExecution.getJobExecutionId());
|
||||
jobExecution.getStepExecutions()
|
||||
.stream()
|
||||
.filter(stepExecution -> currentStepExecutionIds.contains(stepExecution.getId()))
|
||||
.filter(stepExecution -> !result.contains(stepExecution))
|
||||
.filter(stepExecution -> !stepExecution.getStatus().isRunning())
|
||||
.forEach(result::add);
|
||||
Callable<Set<StepExecution>> callback = () -> {
|
||||
Set<Long> currentStepExecutionIds = split.stream().map(StepExecution::getId).collect(Collectors.toSet());
|
||||
JobExecution jobExecution = jobExplorer.getJobExecution(managerStepExecution.getJobExecutionId());
|
||||
jobExecution.getStepExecutions()
|
||||
.stream()
|
||||
.filter(stepExecution -> currentStepExecutionIds.contains(stepExecution.getId()))
|
||||
.filter(stepExecution -> !result.contains(stepExecution))
|
||||
.filter(stepExecution -> !stepExecution.getStatus().isRunning())
|
||||
.forEach(result::add);
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(String.format("Currently waiting on %s partitions to finish", split.size()));
|
||||
}
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(String.format("Currently waiting on %s partitions to finish", split.size()));
|
||||
}
|
||||
|
||||
if (result.size() == split.size()) {
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
if (result.size() == split.size()) {
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -21,7 +21,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -68,11 +67,7 @@ class AsyncItemProcessorTests {
|
||||
};
|
||||
processor.setDelegate(delegate);
|
||||
Future<String> result = StepScopeTestUtils.doInStepScope(MetaDataInstanceFactory.createStepExecution(),
|
||||
new Callable<>() {
|
||||
public Future<String> call() throws Exception {
|
||||
return processor.process("foo");
|
||||
}
|
||||
});
|
||||
() -> processor.process("foo"));
|
||||
assertEquals("foofoo", result.get());
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@ package org.springframework.batch.integration.async;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.FutureTask;
|
||||
@@ -64,19 +63,9 @@ class AsyncItemWriterTests {
|
||||
writer.setDelegate(new ListItemWriter(writtenItems));
|
||||
Chunk<FutureTask<String>> processedItems = new Chunk<>();
|
||||
|
||||
processedItems.add(new FutureTask<>(new Callable<>() {
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
return "foo";
|
||||
}
|
||||
}));
|
||||
processedItems.add(new FutureTask<>(() -> "foo"));
|
||||
|
||||
processedItems.add(new FutureTask<>(new Callable<>() {
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
return "bar";
|
||||
}
|
||||
}));
|
||||
processedItems.add(new FutureTask<>(() -> "bar"));
|
||||
|
||||
for (FutureTask<String> processedItem : processedItems) {
|
||||
taskExecutor.execute(processedItem);
|
||||
@@ -94,19 +83,9 @@ class AsyncItemWriterTests {
|
||||
writer.setDelegate(new ListItemWriter(writtenItems));
|
||||
Chunk<FutureTask<String>> processedItems = new Chunk<>();
|
||||
|
||||
processedItems.add(new FutureTask<>(new Callable<>() {
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
return "foo";
|
||||
}
|
||||
}));
|
||||
processedItems.add(new FutureTask<>(() -> "foo"));
|
||||
|
||||
processedItems.add(new FutureTask<>(new Callable<>() {
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
return null;
|
||||
}
|
||||
}));
|
||||
processedItems.add(new FutureTask<>(() -> null));
|
||||
|
||||
for (FutureTask<String> processedItem : processedItems) {
|
||||
taskExecutor.execute(processedItem);
|
||||
@@ -123,18 +102,10 @@ class AsyncItemWriterTests {
|
||||
writer.setDelegate(new ListItemWriter(writtenItems));
|
||||
Chunk<FutureTask<String>> processedItems = new Chunk<>();
|
||||
|
||||
processedItems.add(new FutureTask<>(new Callable<>() {
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
return "foo";
|
||||
}
|
||||
}));
|
||||
processedItems.add(new FutureTask<>(() -> "foo"));
|
||||
|
||||
processedItems.add(new FutureTask<>(new Callable<>() {
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
throw new RuntimeException("This was expected");
|
||||
}
|
||||
processedItems.add(new FutureTask<>(() -> {
|
||||
throw new RuntimeException("This was expected");
|
||||
}));
|
||||
|
||||
for (FutureTask<String> processedItem : processedItems) {
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.batch.integration.chunk;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.batch.core.StepContribution;
|
||||
import org.springframework.batch.core.step.item.ChunkProcessor;
|
||||
import org.springframework.batch.item.Chunk;
|
||||
import org.springframework.batch.test.MetaDataInstanceFactory;
|
||||
|
||||
@@ -34,11 +33,7 @@ class ChunkProcessorChunkHandlerTests {
|
||||
@Test
|
||||
void testVanillaHandleChunk() throws Exception {
|
||||
// given
|
||||
handler.setChunkProcessor(new ChunkProcessor<>() {
|
||||
public void process(StepContribution contribution, Chunk<Object> chunk) throws Exception {
|
||||
count += chunk.size();
|
||||
}
|
||||
});
|
||||
handler.setChunkProcessor((contribution, chunk) -> count += chunk.size());
|
||||
StepContribution stepContribution = MetaDataInstanceFactory.createStepExecution().createStepContribution();
|
||||
Chunk items = Chunk.of("foo", "bar");
|
||||
ChunkRequest chunkRequest = new ChunkRequest<>(0, items, 12L, stepContribution);
|
||||
|
||||
Reference in New Issue
Block a user