Skip to content

Add AbstractTaskletStepBuilder copy constructor #4471

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
wants to merge 2 commits into from
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 @@ -49,6 +49,7 @@
* @author Dave Syer
* @author Michael Minella
* @author Mahmoud Ben Hassine
* @author Ilpyo Yang
* @since 2.2
* @param <B> the type of builder represented
*/
Expand All @@ -74,6 +75,23 @@ public AbstractTaskletStepBuilder(StepBuilderHelper<?> parent) {
super(parent);
}

/**
* Create a new builder initialized with any properties in the parent. The parent is
* copied, so it can be re-used.
* @param parent a parent helper containing common step properties
*/
public AbstractTaskletStepBuilder(AbstractTaskletStepBuilder<?> parent) {
Copy link
Contributor

Choose a reason for hiding this comment

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

How about making it protected?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh I will try thanks!

super(parent);
this.chunkListeners = parent.chunkListeners;
this.stepOperations = parent.stepOperations;
this.transactionManager = parent.transactionManager;
this.transactionAttribute = parent.transactionAttribute;
this.streams.addAll(parent.streams);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
this.streams.addAll(parent.streams);
this.streams = parent.streams;

Hmm maybe we can just use parent.streams instead of addAll()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm maybe we can just use parent.streams instead of addAll()?

To creating a copy constructor, it seems like the second option, this.streams = parents.streams, is appropriate. Thank you for your comment @injae-kim!

But just curious.. was there any specific reason for initially declaring streams as a final variable?

this.exceptionHandler = parent.exceptionHandler;
this.throttleLimit = parent.throttleLimit;
this.taskExecutor = parent.taskExecutor;
}
Comment on lines +78 to +93
Copy link
Contributor

Choose a reason for hiding this comment

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

image

        TaskletStep step2 = new StepBuilder("step-name", jobRepository)
                .chunk(10, transactionManager)
                .taskExecutor(taskExecutor)// The task executor is set before faultTolerant()
                .reader(itemReader)
                .processor(itemProcessor)
                .writer(itemWriter)
                .faultTolerant() // after faultTolerant(), taskExecutor is not copied
                // cause there's no copy constructor of AbstractTaskletStepBuilder!
                .build();

Hi @fmbenhassine , me and @Ilpyo-Yang checked that there's no copy constructor of AbstractTaskletStepBuilder, so SimpleStepBuilder.faultTolerant() doesn't copy member variables of AbstractTaskletStepBuilder well now.

All users who use SimpleStepBuilder.faultTolerant() affected by this bug, so I think it's good to release this bug fix on next release.

So can you review this PR? thanks a lot! 🙇


protected abstract Tasklet createTasklet();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ protected SimpleStepBuilder(SimpleStepBuilder<I, O> parent) {
this.itemListeners = parent.itemListeners;
this.readerTransactionalQueue = parent.readerTransactionalQueue;
this.meterRegistry = parent.meterRegistry;
this.transactionManager(parent.getTransactionManager());
}

public FaultTolerantStepBuilder<I, O> faultTolerant() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package org.springframework.batch.test;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think package line should be followed by copyright comments. (see also)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't recognize it.. I will updated it ;)

/*
* Copyright 2020-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.
* 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.
*/

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.AbstractTaskletStepBuilder;
import org.springframework.batch.core.step.builder.SimpleStepBuilder;
import org.springframework.batch.core.step.builder.StepBuilderHelper;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.test.context.SpringBatchTest;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

/**
* Test cases for verifying the {@link AbstractTaskletStepBuilder} and faultTolerant() functionality.
*
* @author Ilpyo Yang
*/
@SpringBatchTest
@SpringJUnitConfig
public class AbstractTaskletStepBuilderTests {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this test code should be placed somewhere in spring-batch-core/src/test/java....

spring-batch-test module is for providing test utils for spring batch users.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

umm that is why this not merged.. I missed few things in contribute regulation. Thank you for kindness review. I will update all in this holiday

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@acktsap thanks for the detailed review, and I've incorporated the suggested changes.

Copy link
Contributor

Choose a reason for hiding this comment

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

I still see this class in the spring-batch-test module. No problem, I will move it to the core module on merge.

private final JobRepository jobRepository = mock(JobRepository.class);
private final int chunkSize = 10;
private final ItemReader itemReader = mock(ItemReader.class);
private final ItemProcessor itemProcessor = mock(ItemProcessor.class);
private final ItemWriter itemWriter = mock(ItemWriter.class);
private final SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
SimpleStepBuilder simpleStepBuilder;

private <T> T accessPrivateField(Object o, String fieldName) throws ReflectiveOperationException {
Field field = o.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
return (T) field.get(o);
}

private <T> T accessSuperClassPrivateField(Object o, String fieldName) throws ReflectiveOperationException {
Field field = o.getClass().getSuperclass().getDeclaredField(fieldName);
field.setAccessible(true);
return (T) field.get(o);
}

@BeforeEach
void set(){
StepBuilderHelper stepBuilderHelper = new StepBuilderHelper("test", jobRepository) {
@Override
protected StepBuilderHelper self() {
return null;
}
};
simpleStepBuilder = new SimpleStepBuilder(stepBuilderHelper);
simpleStepBuilder.chunk(chunkSize);
simpleStepBuilder.reader(itemReader);
simpleStepBuilder.processor(itemProcessor);
simpleStepBuilder.writer(itemWriter);
}

@Test
void copyConstractorTest() throws ReflectiveOperationException {
Constructor<SimpleStepBuilder> constructor = SimpleStepBuilder.class.getDeclaredConstructor(SimpleStepBuilder.class);
constructor.setAccessible(true);
SimpleStepBuilder copySimpleStepBuilder = constructor.newInstance(simpleStepBuilder);

int copyChunkSize = accessPrivateField(copySimpleStepBuilder, "chunkSize");
ItemReader copyItemReader = accessPrivateField(copySimpleStepBuilder, "reader");
ItemProcessor copyItemProcessor = accessPrivateField(copySimpleStepBuilder, "processor");
ItemWriter copyItemWriter = accessPrivateField(copySimpleStepBuilder, "writer");

assertEquals(chunkSize, copyChunkSize);
assertEquals(itemReader, copyItemReader);
assertEquals(itemProcessor, copyItemProcessor);
assertEquals(itemWriter, copyItemWriter);
}

@Test
void faultTolerantMethodTest() throws ReflectiveOperationException {
simpleStepBuilder.taskExecutor(taskExecutor); // The task executor is set before faultTolerant()
simpleStepBuilder.faultTolerant();

int afterChunkSize = accessPrivateField(simpleStepBuilder, "chunkSize");
ItemReader afterItemReader = accessPrivateField(simpleStepBuilder, "reader");
ItemProcessor afterItemProcessor = accessPrivateField(simpleStepBuilder, "processor");
ItemWriter afterItemWriter = accessPrivateField(simpleStepBuilder, "writer");
TaskExecutor afterTaskExecutor = accessSuperClassPrivateField(simpleStepBuilder, "taskExecutor");

assertEquals(chunkSize, afterChunkSize);
assertEquals(itemReader, afterItemReader);
assertEquals(itemProcessor, afterItemProcessor);
assertEquals(itemWriter, afterItemWriter);
assertEquals(taskExecutor, afterTaskExecutor);
}
}