[JAVA] spring-batch is automatically registered when listener is implemented by reader etc.

background

An ItemReader, ItemWriter or ItemProcessor that itself implements one of the StepListener interfaces is registered automatically with the Step if using the namespace element or one of the the *StepFactoryBean factories.

Excerpt from https://docs.spring.io/spring-batch/4.1.x/reference/html/step.html#interceptingStepExecution

If you implement a listener such as `` `StepExecutionListener``` in the implementation class such as ItemReader </ code>, that listener will also be automatically registered (at least for running spring-batch with spring-boot). , And that. I will try it for the time being.

Or rather, I was a little surprised because I didn't know it, so I left a note.

Source code

pom.xml

pom.xml


	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.2.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-batch</artifactId>
		</dependency>

ItemReader + StepExecutionListener

Create an appropriate chunk process.

SampleChunk


import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.stereotype.Component;

@Component
public class SampleChunk implements ItemReader<String>, ItemWriter<String>, StepExecutionListener {

	@Override
	public void write(List<? extends String> items) throws Exception {
		items.forEach(s -> System.out.println(s));
	}

	int i=0;
	
	@Override
	public String read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
		if (i<5) {
			i++;
			return i + "";
		}
		return null;
	}

	@Override
	public void beforeStep(StepExecution stepExecution) {
		System.out.println("beforeStep");
	}

	@Override
	public ExitStatus afterStep(StepExecution stepExecution) {
		System.out.println("afterStep");
		return stepExecution.getExitStatus();
	}
}

step definition

Create an appropriate step definition.

	@Autowired
	SampleChunk r2;

	@Bean("chunkstep1")
	public Step step1() {
		StepExecutionListener s = new SampleChunk();
		return stepBuilderFactory
				.get("step1")
				.<String, String>chunk(1)
				.reader(r2)
				.writer(r2)
				.build();//.listener(s)
	}

Run

beforeStep
1
2
3
4
5
afterStep

Certainly listeners are also registered.

Register with listener

Try registering a listener with listener as shown below.

	@Bean("chunkstep1")
	public Step step1() {
		StepExecutionListener s = new SampleChunk();
		return stepBuilderFactory
				.get("step1")
				.<String, String>chunk(1)
				.reader(r2)
				.writer(r2)
				.listener(s)
				.build();
	}
beforeStep
beforeStep
1
2
3
4
5
afterStep
afterStep

This is double.

Recommended Posts

spring-batch is automatically registered when listener is implemented by reader etc.
LIMIT 11 when data is acquired by irb