All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
io.polaris.core.datacarrier.DataCarrier Maven / Gradle / Ivy
package io.polaris.core.datacarrier;
import io.polaris.core.datacarrier.buffer.BufferChannel;
import io.polaris.core.datacarrier.buffer.BufferStrategy;
import io.polaris.core.datacarrier.consumer.ConsumeDriver;
import io.polaris.core.datacarrier.consumer.IBulkConsumerDriver;
import io.polaris.core.datacarrier.consumer.IConsumer;
import io.polaris.core.datacarrier.consumer.IConsumerDriver;
import io.polaris.core.datacarrier.partition.IDataPartitioner;
import io.polaris.core.datacarrier.partition.SimpleRollingPartitioner;
/**
* @author Qt
* @since 1.8
*/
public class DataCarrier {
private BufferChannel channel;
private IConsumerDriver driver;
private String name;
public DataCarrier(int bufferCount, int bufferSize) {
this("DEFAULT", bufferCount, bufferSize);
}
public DataCarrier(String name, int bufferCount, int bufferSize) {
this(name, bufferCount, bufferSize, BufferStrategy.BLOCKING);
}
public DataCarrier(int bufferCount, int bufferSize, BufferStrategy strategy) {
this("DEFAULT", bufferCount, bufferSize, strategy);
}
public DataCarrier(String name, int bufferCount, int bufferSize, BufferStrategy strategy) {
this(name, bufferCount, bufferSize, strategy, new SimpleRollingPartitioner());
}
public DataCarrier(String name, int bufferCount, int bufferSize, BufferStrategy strategy,IDataPartitioner partitioner) {
this.name = name;
channel = new BufferChannel<>(bufferCount, bufferSize, partitioner, strategy);
}
public boolean produce(T data) {
if (driver != null) {
if (!driver.isRunning(channel)) {
return false;
}
}
return this.channel.produce(data);
}
public DataCarrier consume(IConsumer consumer, int num) {
return this.consume(consumer, num, 20);
}
public DataCarrier consume(IConsumer consumer, int num, long thinkTime) {
if (driver != null) {
driver.close(channel);
}
driver = new ConsumeDriver(this.name, this.channel, consumer, num, thinkTime);
driver.begin(channel);
return this;
}
public DataCarrier consume(IBulkConsumerDriver consumerDriver, IConsumer consumer) {
driver = consumerDriver;
consumerDriver.add(channel, consumer);
driver.begin(channel);
return this;
}
public DataCarrier setPartitioner(IDataPartitioner dataPartitioner) {
this.channel.setPartitioner(dataPartitioner);
return this;
}
public DataCarrier setMaxRetryCount(final int maxRetryCount) {
this.channel.setMaxRetryCount(maxRetryCount);
return this;
}
public BufferChannel getChannel() {
return channel;
}
public void shutdown() {
if (driver != null) {
driver.close(channel);
}
}
}