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

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

There is a newer version: 4.0.7
Show newest version
package com.redis.spring.batch.step;

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

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.SimpleStepBuilder;
import org.springframework.batch.core.step.builder.StepBuilderHelper;
import org.springframework.batch.core.step.item.ChunkOrientedTasklet;
import org.springframework.batch.core.step.item.SimpleChunkProcessor;
import org.springframework.batch.core.step.tasklet.Tasklet;
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.reader.PollableItemReader;

public class FlushingStepBuilder extends SimpleStepBuilder {

    private Duration interval = FlushingChunkProvider.DEFAULT_FLUSHING_INTERVAL;

    private Duration idleTimeout;

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

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

    @Override
    public FlushingFaultTolerantStepBuilder faultTolerant() {
        return new FlushingFaultTolerantStepBuilder<>(this);
    }

    @Override
    protected Tasklet createTasklet() {
        ItemReader reader = getReader();
        ItemWriter writer = getWriter();
        Assert.state(reader != null, "ItemReader must be provided");
        Assert.state(writer != null, "ItemWriter must be provided");
        FlushingChunkProvider chunkProvider = createChunkProvider();
        SimpleChunkProcessor chunkProcessor = createChunkProcessor();
        ChunkOrientedTasklet tasklet = new ChunkOrientedTasklet<>(chunkProvider, chunkProcessor);
        tasklet.setBuffering(!isReaderTransactionalQueue());
        return tasklet;
    }

    private SimpleChunkProcessor createChunkProcessor() {
        SimpleChunkProcessor chunkProcessor = new SimpleChunkProcessor<>(getProcessor(), getWriter());
        chunkProcessor.setListeners(new ArrayList<>(getItemListeners()));
        return chunkProcessor;
    }

    protected FlushingChunkProvider createChunkProvider() {
        FlushingChunkProvider chunkProvider = new FlushingChunkProvider<>(getReader(), createChunkOperations());
        chunkProvider.setInterval(interval);
        chunkProvider.setIdleTimeout(idleTimeout);
        ArrayList listeners = new ArrayList<>(getItemListeners());
        chunkProvider.setListeners(listeners);
        return chunkProvider;
    }

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

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

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

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

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

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

    @Override
    public FlushingStepBuilder processor(Function function) {
        return (FlushingStepBuilder) super.processor(function);
    }

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

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

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

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

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

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

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

    public Duration getInterval() {
        return interval;
    }

    public Duration getIdleTimeout() {
        return idleTimeout;
    }

}