Skip to content

updated SimpleStepBuilder to assign ItemProcessor lambda at construction #3897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ protected SimpleStepBuilder(SimpleStepBuilder<I, O> parent) {
this.reader = parent.reader;
this.writer = parent.writer;
this.processor = parent.processor;
this.itemProcessorFunction = parent.itemProcessorFunction;
this.itemListeners = parent.itemListeners;
this.readerTransactionalQueue = parent.readerTransactionalQueue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
Expand Down Expand Up @@ -56,6 +56,7 @@
* @author Dave Syer
* @author Michael Minella
* @author Mahmoud Ben Hassine
* @author Parikshit Dutta
*
*/
@SuppressWarnings("serial")
Expand Down Expand Up @@ -214,6 +215,15 @@ public void testItemListeners() throws Exception {

@Test
public void testFunctions() throws Exception {
assertStepFunctions(false);
}

@Test
public void testFunctionsWithFaultTolerantStep() throws Exception {
assertStepFunctions(true);
}

private void assertStepFunctions(boolean faultTolerantStep) throws Exception {
JobRepository jobRepository = new MapJobRepositoryFactoryBean().getObject();
StepExecution execution = jobRepository.createJobExecution("foo", new JobParameters()).createStepExecution("step");
jobRepository.add(execution);
Expand All @@ -228,20 +238,24 @@ public void testFunctions() throws Exception {
ItemReader<Long> reader = new ListItemReader<>(items);

ListItemWriter<String> itemWriter = new ListItemWriter<>();
@SuppressWarnings("unchecked")

SimpleStepBuilder<Object, String> builder = new StepBuilder("step")
.repository(jobRepository)
.transactionManager(transactionManager)
.<Object, String>chunk(3)
.reader(reader)
.processor((Function<Object, String>) s -> s.toString())
.writer(itemWriter)
.listener(new AnnotationBasedStepExecutionListener());
.repository(jobRepository)
.transactionManager(transactionManager)
.<Object, String>chunk(3)
.reader(reader)
.processor((Function<Object, String>) s -> s.toString())
.writer(itemWriter)
.listener(new AnnotationBasedStepExecutionListener());

if (faultTolerantStep) {
builder = builder.faultTolerant();
}
builder.build().execute(execution);

assertEquals(BatchStatus.COMPLETED, execution.getStatus());

List<? extends String> writtenItems = itemWriter.getWrittenItems();
List<? extends String> writtenItems = ((ListItemWriter) itemWriter).getWrittenItems();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change (which is not related to the main goal of the fix) has introduced a warning about an unchecked assignment. The warning could be fixed by using an explicit type like ((ListItemWriter<String>) itemWriter), but this makes the cast redundant. I will revert that change on merge.

assertEquals("1", writtenItems.get(0));
assertEquals("2", writtenItems.get(1));
assertEquals("3", writtenItems.get(2));
Expand Down