All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.redis.spring.batch.step.FlushingFaultTolerantStepBuilder Maven / Gradle / Ivy

The newest version!
package com.redis.spring.batch.step;

import java.time.Duration;
import java.util.ArrayList;

import org.springframework.batch.core.ItemProcessListener;
import org.springframework.batch.core.ItemReadListener;
import org.springframework.batch.core.ItemWriteListener;
import org.springframework.batch.core.StepListener;
import org.springframework.batch.core.step.builder.FaultTolerantStepBuilder;
import org.springframework.batch.core.step.builder.SimpleStepBuilder;
import org.springframework.batch.core.step.builder.StepBuilderHelper;
import org.springframework.batch.core.step.item.ChunkProvider;
import org.springframework.batch.core.step.item.FaultTolerantChunkProvider;
import org.springframework.batch.core.step.skip.SkipPolicy;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.repeat.CompletionPolicy;
import org.springframework.batch.repeat.RepeatOperations;
import org.springframework.util.Assert;

import com.redis.spring.batch.item.PollableItemReader;

public class FlushingFaultTolerantStepBuilder extends FaultTolerantStepBuilder {

	private Duration flushInterval = FlushingChunkProvider.DEFAULT_FLUSH_INTERVAL;
	private Duration idleTimeout = FlushingChunkProvider.DEFAULT_IDLE_TIMEOUT;

	public FlushingFaultTolerantStepBuilder(StepBuilderHelper parent) {
		super(parent);
	}

	public FlushingFaultTolerantStepBuilder(SimpleStepBuilder parent) {
		super(parent);
	}

	public FlushingFaultTolerantStepBuilder(FlushingStepBuilder parent) {
		super(parent);
		this.flushInterval = parent.getFlushInterval();
		this.idleTimeout = parent.getIdleTimeout();
	}

	@Override
	protected ChunkProvider createChunkProvider() {
		SkipPolicy readSkipPolicy = createSkipPolicy();
		readSkipPolicy = getFatalExceptionAwareProxy(readSkipPolicy);
		int maxSkipsOnRead = Math.max(getChunkSize(), FaultTolerantChunkProvider.DEFAULT_MAX_SKIPS_ON_READ);
		FlushingFaultTolerantChunkProvider chunkProvider = new FlushingFaultTolerantChunkProvider<>(getReader(),
				createChunkOperations());
		chunkProvider.setMaxSkipsOnRead(maxSkipsOnRead);
		chunkProvider.setSkipPolicy(readSkipPolicy);
		chunkProvider.setRollbackClassifier(getRollbackClassifier());
		chunkProvider.setFlushInterval(flushInterval);
		chunkProvider.setIdleTimeout(idleTimeout);
		ArrayList listeners = new ArrayList<>(getItemListeners());
		listeners.addAll(getSkipListeners());
		chunkProvider.setListeners(listeners);
		return chunkProvider;
	}

	@Override
	public FlushingFaultTolerantStepBuilder chunk(int chunkSize) {
		return (FlushingFaultTolerantStepBuilder) super.chunk(chunkSize);
	}

	@Override
	public FlushingFaultTolerantStepBuilder chunk(CompletionPolicy completionPolicy) {
		return (FlushingFaultTolerantStepBuilder) super.chunk(completionPolicy);
	}

	public FlushingFaultTolerantStepBuilder flushInterval(Duration interval) {
		this.flushInterval = interval;
		return this;
	}

	public FlushingFaultTolerantStepBuilder idleTimeout(Duration timeout) {
		this.idleTimeout = timeout;
		return this;
	}

	@Override
	public FlushingFaultTolerantStepBuilder faultTolerant() {
		return (FlushingFaultTolerantStepBuilder) super.faultTolerant();
	}

	@Override
	public FlushingFaultTolerantStepBuilder reader(ItemReader reader) {
		Assert.state(reader instanceof PollableItemReader, "Reader must be an instance of PollableItemReader");
		return (FlushingFaultTolerantStepBuilder) super.reader(reader);
	}

	@Override
	public FlushingFaultTolerantStepBuilder writer(ItemWriter writer) {
		return (FlushingFaultTolerantStepBuilder) super.writer(writer);
	}

	@Override
	public FlushingFaultTolerantStepBuilder processor(ItemProcessor processor) {
		return (FlushingFaultTolerantStepBuilder) super.processor(processor);
	}

	@Override
	public FlushingFaultTolerantStepBuilder readerIsTransactionalQueue() {
		return (FlushingFaultTolerantStepBuilder) super.readerIsTransactionalQueue();
	}

	@Override
	public FlushingFaultTolerantStepBuilder listener(Object listener) {
		return (FlushingFaultTolerantStepBuilder) super.listener(listener);
	}

	@Override
	public FlushingFaultTolerantStepBuilder listener(ItemReadListener listener) {
		return (FlushingFaultTolerantStepBuilder) super.listener(listener);
	}

	@Override
	public FlushingFaultTolerantStepBuilder listener(ItemWriteListener listener) {
		return (FlushingFaultTolerantStepBuilder) super.listener(listener);
	}

	@Override
	public FlushingFaultTolerantStepBuilder listener(ItemProcessListener listener) {
		return (FlushingFaultTolerantStepBuilder) super.listener(listener);
	}

	@Override
	public FlushingFaultTolerantStepBuilder chunkOperations(RepeatOperations repeatTemplate) {
		return (FlushingFaultTolerantStepBuilder) super.chunkOperations(repeatTemplate);
	}

}