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

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

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

import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemStreamWriter;
import org.springframework.batch.item.ItemWriter;
import org.springframework.util.Assert;

public class ThrottledItemWriter implements ItemStreamWriter {

	private final ItemWriter delegate;
	private final long sleep;

	public ThrottledItemWriter(ItemWriter delegate, long sleep) {
		Assert.notNull(delegate, "Delegate must not be null");
		Assert.isTrue(sleep > 0, "Sleep duration must be strictly positive");
		this.delegate = delegate;
		this.sleep = sleep;
	}

	@Override
	public void open(ExecutionContext executionContext) {
		if (delegate instanceof ItemStream) {
			((ItemStream) delegate).open(executionContext);
		}
	}

	@Override
	public void update(ExecutionContext executionContext) {
		if (delegate instanceof ItemStream) {
			((ItemStream) delegate).update(executionContext);
		}
	}

	@Override
	public void close() {
		if (delegate instanceof ItemStream) {
			((ItemStream) delegate).close();
		}
	}

	@Override
	public void write(Chunk items) throws Exception {
		delegate.write(items);
		Thread.sleep(sleep);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy