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

com.redis.spring.batch.item.BlockingQueueItemWriter Maven / Gradle / Ivy

There is a newer version: 4.4.9
Show newest version
package com.redis.spring.batch.item;

import java.util.List;
import java.util.concurrent.BlockingQueue;

import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.ItemStreamWriter;
import org.springframework.util.CollectionUtils;

public class BlockingQueueItemWriter implements ItemStreamWriter {

	private final BlockingQueue queue;
	private final ItemProcessor, List> processor;

	public BlockingQueueItemWriter(ItemProcessor, List> processor, BlockingQueue queue) {
		this.queue = queue;
		this.processor = processor;
	}

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

	@Override
	public void close() throws ItemStreamException {
		if (processor instanceof ItemStream) {
			((ItemStream) processor).close();
		}
	}

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

	@Override
	public void write(Chunk chunk) throws Exception {
		List processedChunk = processor.process(chunk.getItems());
		if (!CollectionUtils.isEmpty(processedChunk)) {
			for (T element : processedChunk) {
				queue.put(element);
			}
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy