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

com.redis.spring.batch.writer.AbstractOperationItemWriter Maven / Gradle / Ivy

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

import java.time.Duration;

import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemStreamWriter;

import com.redis.spring.batch.common.AbstractOperationExecutor;
import com.redis.spring.batch.common.Operation;
import com.redis.spring.batch.writer.operation.MultiExec;
import com.redis.spring.batch.writer.operation.ReplicaWait;

import io.lettuce.core.AbstractRedisClient;
import io.lettuce.core.RedisException;
import io.lettuce.core.codec.RedisCodec;

public abstract class AbstractOperationItemWriter extends AbstractOperationExecutor
		implements ItemStreamWriter {

	public static final Duration DEFAULT_WAIT_TIMEOUT = Duration.ofSeconds(1);

	private int waitReplicas;
	private Duration waitTimeout = DEFAULT_WAIT_TIMEOUT;
	private boolean multiExec;

	protected AbstractOperationItemWriter(AbstractRedisClient client, RedisCodec codec) {
		super(client, codec);
	}

	public void setWaitReplicas(int replicas) {
		this.waitReplicas = replicas;
	}

	public void setWaitTimeout(Duration timeout) {
		this.waitTimeout = timeout;
	}

	public void setMultiExec(boolean enabled) {
		this.multiExec = enabled;
	}

	@Override
	public void open(ExecutionContext executionContext) {
		open();
	}

	@Override
	public void write(Chunk items) throws RedisException {
		execute(items);
	}

	@Override
	protected Operation operation() {
		Operation operation = writeOperation();
		operation = replicaWaitOperation(operation);
		return multiExec(operation);
	}

	protected abstract Operation writeOperation();

	private Operation multiExec(Operation operation) {
		if (multiExec) {
			return new MultiExec<>(operation);
		}
		return operation;
	}

	private Operation replicaWaitOperation(Operation operation) {
		if (waitReplicas > 0) {
			return new ReplicaWait<>(operation, waitReplicas, waitTimeout);
		}
		return operation;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy