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

com.redis.riot.core.Step Maven / Gradle / Ivy

The newest version!
package com.redis.riot.core;

import java.time.Duration;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.LongSupplier;
import java.util.function.Supplier;

import org.springframework.batch.core.ItemReadListener;
import org.springframework.batch.core.ItemWriteListener;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;

import com.redis.spring.batch.step.FlushingChunkProvider;

public class Step {

	private static final long NO_MAX_ITEM_COUNT = -1;
	private static final String EMPTY_STRING = "";

	protected final String name;
	private final ItemReader reader;
	private final ItemWriter writer;
	private String taskName;
	private Supplier statusMessageSupplier = () -> EMPTY_STRING;
	private LongSupplier maxItemCountSupplier = () -> NO_MAX_ITEM_COUNT;
	private ItemProcessor processor;
	private Set executionListeners = new LinkedHashSet<>();
	private Set> readListeners = new LinkedHashSet<>();
	private Set> writeListeners = new LinkedHashSet<>();
	private boolean live;
	private Duration flushInterval = FlushingChunkProvider.DEFAULT_FLUSH_INTERVAL;
	private Duration idleTimeout = FlushingChunkProvider.DEFAULT_IDLE_TIMEOUT;
	private Collection> skip = new HashSet<>();
	private Collection> noSkip = new HashSet<>();
	private Collection> retry = new HashSet<>();
	private Collection> noRetry = new HashSet<>();

	public Step(String name, ItemReader reader, ItemWriter writer) {
		this.name = name;
		this.reader = reader;
		this.writer = writer;
	}

	public String getName() {
		return name;
	}

	public String getTaskName() {
		return taskName;
	}

	public Step taskName(String name) {
		this.taskName = name;
		return this;
	}

	public ItemReader getReader() {
		return reader;
	}

	public ItemProcessor getProcessor() {
		return processor;
	}

	public ItemWriter getWriter() {
		return writer;
	}

	public long maxItemCount() {
		return maxItemCountSupplier.getAsLong();
	}

	public String statusMessage() {
		return statusMessageSupplier.get();
	}

	public Supplier getStatusMessageSupplier() {
		return statusMessageSupplier;
	}

	public Step statusMessageSupplier(Supplier supplier) {
		this.statusMessageSupplier = supplier;
		return this;
	}

	public LongSupplier getMaxItemCountSupplier() {
		return maxItemCountSupplier;
	}

	public Step maxItemCountSupplier(LongSupplier supplier) {
		this.maxItemCountSupplier = supplier;
		return this;
	}

	public Step processor(ItemProcessor processor) {
		this.processor = processor;
		return this;
	}

	public Step maxItemCount(int count) {
		return maxItemCountSupplier(() -> count);
	}

	public Set> getReadListeners() {
		return readListeners;
	}

	public Step readListener(ItemReadListener listener) {
		this.readListeners.add(listener);
		return this;
	}

	public Set> getWriteListeners() {
		return writeListeners;
	}

	public Step writeListener(ItemWriteListener listener) {
		writeListeners.add(listener);
		return this;
	}

	public Set getExecutionListeners() {
		return executionListeners;
	}

	public Step executionListener(StepExecutionListener listener) {
		executionListeners.add(listener);
		return this;
	}

	public Duration getFlushInterval() {
		return flushInterval;
	}

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

	public Duration getIdleTimeout() {
		return idleTimeout;
	}

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

	public boolean isLive() {
		return live;
	}

	public Step live(boolean live) {
		this.live = live;
		return this;
	}

	public Step skip(Class exception) {
		skip.add(exception);
		return this;
	}

	public Step retry(Class exception) {
		retry.add(exception);
		return this;
	}

	public Step noSkip(Class exception) {
		noSkip.add(exception);
		return this;
	}

	public Step noRetry(Class exception) {
		noRetry.add(exception);
		return this;
	}

	public Collection> getNoRetry() {
		return noRetry;
	}

	public Collection> getNoSkip() {
		return noSkip;
	}

	public Collection> getRetry() {
		return retry;
	}

	public Collection> getSkip() {
		return skip;
	}

	@Override
	public String toString() {
		return "Step [name=" + name + ", reader=" + reader + ", writer=" + writer + ", taskName=" + taskName
				+ ", statusMessageSupplier=" + statusMessageSupplier + ", maxItemCountSupplier=" + maxItemCountSupplier
				+ ", processor=" + processor + ", executionListeners=" + executionListeners + ", readListeners="
				+ readListeners + ", writeListeners=" + writeListeners + ", live=" + live + ", flushInterval="
				+ flushInterval + ", idleTimeout=" + idleTimeout + ", skip=" + skip + ", noSkip=" + noSkip + ", retry="
				+ retry + ", noRetry=" + noRetry + "]";
	}
	
	
}