本文整理匯總了Java中com.lmax.disruptor.dsl.ProducerType類的典型用法代碼示例。如果您正苦於以下問題:Java ProducerType類的具體用法?Java ProducerType怎麽用?Java ProducerType使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ProducerType類屬於com.lmax.disruptor.dsl包,在下文中一共展示了ProducerType類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: initDisruptor
import com.lmax.disruptor.dsl.ProducerType; //導入依賴的package包/類
private void initDisruptor(int processors, int ringBufferSize) {
LOG.info("eds client init disruptor with processors="
+ processors + " and ringBufferSize=" + ringBufferSize);
executor = Executors.newFixedThreadPool(
processors,
new ThreadFactoryBuilder().setNameFormat("disruptor-executor-%d").build());
final WaitStrategy waitStrategy = createWaitStrategy();
ringBufferSize = sizeFor(ringBufferSize); // power of 2
disruptor = new Disruptor<>(EdsRingBufferEvent.FACTORY, ringBufferSize, executor,
ProducerType.MULTI, waitStrategy);
EdsEventWorkHandler[] handlers = new EdsEventWorkHandler[processors];
for (int i = 0; i < handlers.length; i++) {
handlers[i] = new EdsEventWorkHandler();
}
// handlers number = threads number
disruptor.handleEventsWithWorkerPool(handlers); // "handleEventsWith" just like topics , with multiple consumers
disruptor.start();
}
示例2: SharedMessageStore
import com.lmax.disruptor.dsl.ProducerType; //導入依賴的package包/類
@SuppressWarnings("unchecked")
public SharedMessageStore(MessageDao messageDao, int bufferSize, int maxDbBatchSize) {
pendingMessages = new ConcurrentHashMap<>();
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
.setNameFormat("DisruptorMessageStoreThread-%d").build();
disruptor = new Disruptor<>(DbOperation.getFactory(),
bufferSize, namedThreadFactory, ProducerType.MULTI, new SleepingWaitStrategy());
disruptor.setDefaultExceptionHandler(new LogExceptionHandler());
disruptor.handleEventsWith(new DbEventMatcher(bufferSize))
.then(new DbWriter(messageDao, maxDbBatchSize))
.then((EventHandler<DbOperation>) (event, sequence, endOfBatch) -> event.clear());
disruptor.start();
this.messageDao = messageDao;
}
示例3: createDisruptor
import com.lmax.disruptor.dsl.ProducerType; //導入依賴的package包/類
/**
* create a disruptor
*
* @param name The title of the disruptor
* @param ringBufferSize The size of ringBuffer
*/
public void createDisruptor(String name, int ringBufferSize) {
if (DISRUPTOR_MAP.keySet().contains(name)) {
throw new NulsRuntimeException(ErrorCode.FAILED, "create disruptor faild,the name is repetitive!");
}
Disruptor<DisruptorEvent> disruptor = new Disruptor<DisruptorEvent>(EVENT_FACTORY,
ringBufferSize, new NulsThreadFactory(ModuleService.getInstance().getModuleId(EventBusModuleBootstrap.class),name), ProducerType.SINGLE,
new SleepingWaitStrategy());
disruptor.handleEventsWith(new EventHandler<DisruptorEvent>() {
@Override
public void onEvent(DisruptorEvent disruptorEvent, long l, boolean b) throws Exception {
Log.debug(disruptorEvent.getData() + "");
}
});
DISRUPTOR_MAP.put(name, disruptor);
}
示例4: main
import com.lmax.disruptor.dsl.ProducerType; //導入依賴的package包/類
public static void main(String[] args) throws InterruptedException
{
Disruptor<ObjectBox> disruptor = new Disruptor<ObjectBox>(
ObjectBox.FACTORY, RING_SIZE, Executors.newCachedThreadPool(), ProducerType.MULTI,
new BlockingWaitStrategy());
disruptor.handleEventsWith(new Consumer()).then(new Consumer());
final RingBuffer<ObjectBox> ringBuffer = disruptor.getRingBuffer();
Publisher p = new Publisher();
IMessage message = new IMessage();
ITransportable transportable = new ITransportable();
String streamName = "com.lmax.wibble";
System.out.println("publishing " + RING_SIZE + " messages");
for (int i = 0; i < RING_SIZE; i++)
{
ringBuffer.publishEvent(p, message, transportable, streamName);
Thread.sleep(10);
}
System.out.println("start disruptor");
disruptor.start();
System.out.println("continue publishing");
while (true)
{
ringBuffer.publishEvent(p, message, transportable, streamName);
Thread.sleep(10);
}
}
示例5: RingBufferService
import com.lmax.disruptor.dsl.ProducerType; //導入依賴的package包/類
@SuppressWarnings("unchecked")
private RingBufferService(final Properties config) {
config.put(KafkaProducerService.CONFIG_PREFIX + "value.serializer", ByteBufSerde.ByteBufSerializer.class.getName());
config.put(KafkaProducerService.CONFIG_PREFIX + "key.serializer", Serdes.String().serializer());
producer = KafkaProducerService.getInstance(config);
bufferSize = ConfigurationHelper.getIntSystemThenEnvProperty(RB_CONF_BUFFER_SIZE, RB_DEFAULT_BUFFER_SIZE, config);
targetTopic = ConfigurationHelper.getSystemThenEnvProperty(RB_CONF_TOPIC_NAME, RB_DEFAULT_TOPIC_NAME, config);
eventBuffInitSize = ConfigurationHelper.getIntSystemThenEnvProperty(RB_CONF_BUFF_INITIAL_SIZE, RB_DEFAULT_BUFF_INITIAL_SIZE, config);
shutdownTimeout = ConfigurationHelper.getIntSystemThenEnvProperty(RB_CONF_STOP_TIMEOUT, RB_DEFAULT_STOP_TIMEOUT, config);
waitStrategy = RBWaitStrategy.getConfiguredStrategy(config);
disruptor = new Disruptor<ByteBuf>(this, bufferSize, threadFactory, ProducerType.MULTI, waitStrategy);
disruptor.handleEventsWith(this); // FIXME: need to able to supply several handlers
disruptor.start();
ringBuffer = disruptor.getRingBuffer();
log.info("<<<<< RawRingBufferDispatcher Started.");
}
示例6: initDispatcherFactoryFromConfiguration
import com.lmax.disruptor.dsl.ProducerType; //導入依賴的package包/類
private void initDispatcherFactoryFromConfiguration(String name) {
if (dispatcherFactories.get(name) != null) {
return;
}
for (DispatcherConfiguration dispatcherConfiguration : configuration.getDispatcherConfigurations()) {
if (!dispatcherConfiguration.getName()
.equalsIgnoreCase(name)) {
continue;
}
if (DispatcherType.DISPATCHER_GROUP == dispatcherConfiguration.getType()) {
addCachedDispatchers(dispatcherConfiguration.getName(),
createDispatcherFactory(dispatcherConfiguration.getName(),
dispatcherConfiguration.getSize() == 0 ? PROCESSORS : dispatcherConfiguration.getSize(),
dispatcherConfiguration.getBacklog(),
null,
ProducerType.MULTI,
new AgileWaitingStrategy()));
}
}
}
示例7: RingBufferProcessor
import com.lmax.disruptor.dsl.ProducerType; //導入依賴的package包/類
private RingBufferProcessor(String name,
ExecutorService executor,
int bufferSize,
WaitStrategy waitStrategy,
boolean shared,
boolean autoCancel) {
super(name, executor, autoCancel);
this.ringBuffer = RingBuffer.create(
shared ? ProducerType.MULTI : ProducerType.SINGLE,
new EventFactory<MutableSignal<E>>() {
@Override
public MutableSignal<E> newInstance() {
return new MutableSignal<E>();
}
},
bufferSize,
waitStrategy
);
this.recentSequence = new Sequence(Sequencer.INITIAL_CURSOR_VALUE);
this.barrier = ringBuffer.newBarrier();
//ringBuffer.addGatingSequences(recentSequence);
}
示例8: RingBufferWorkProcessor
import com.lmax.disruptor.dsl.ProducerType; //導入依賴的package包/類
private RingBufferWorkProcessor(String name,
ExecutorService executor,
int bufferSize,
WaitStrategy waitStrategy,
boolean share,
boolean autoCancel) {
super(name, executor, autoCancel);
this.ringBuffer = RingBuffer.create(
share ? ProducerType.MULTI : ProducerType.SINGLE,
new EventFactory<MutableSignal<E>>() {
@Override
public MutableSignal<E> newInstance() {
return new MutableSignal<E>();
}
},
bufferSize,
waitStrategy
);
ringBuffer.addGatingSequences(workSequence);
}
示例9: qndSingleThread
import com.lmax.disruptor.dsl.ProducerType; //導入依賴的package包/類
@SuppressWarnings("unchecked")
static void qndSingleThread(int numItems) {
final AtomicLong COUNTER_RECEIVED = new AtomicLong(0);
final Disruptor<LongEvent> disruptor = new Disruptor<>(LongEvent.FACTORY, 128,
Executors.defaultThreadFactory(), ProducerType.MULTI, new YieldingWaitStrategy());
disruptor.handleEventsWith(
(event, sequence, endOfBatch) -> COUNTER_RECEIVED.incrementAndGet());
disruptor.start();
final long t = System.currentTimeMillis();
for (int i = 0; i < numItems; i++) {
disruptor.publishEvent((event, seq) -> event.set(seq));
}
long d = System.currentTimeMillis() - t;
NumberFormat nf = NumberFormat.getInstance();
System.out.println("========== qndSingleThread:");
System.out.println("Sent: " + nf.format(numItems) + " / Received: "
+ nf.format(COUNTER_RECEIVED.get()) + " / Duration: " + d + " / Speed: "
+ NumberFormat.getInstance().format((numItems * 1000.0 / d)) + " items/sec");
disruptor.shutdown();
}
示例10: DisruptorQueueImpl
import com.lmax.disruptor.dsl.ProducerType; //導入依賴的package包/類
public DisruptorQueueImpl(String queueName, ProducerType producerType,
int bufferSize, WaitStrategy wait) {
this._queueName = PREFIX + queueName;
_buffer = RingBuffer.create(producerType, new ObjectEventFactory(),
bufferSize, wait);
_consumer = new Sequence();
_barrier = _buffer.newBarrier();
_buffer.addGatingSequences(_consumer);
if (producerType == ProducerType.SINGLE) {
consumerStartedFlag = true;
} else {
// make sure we flush the pending messages in cache first
if (bufferSize < 2) {
throw new RuntimeException("QueueSize must >= 2");
}
try {
publishDirect(FLUSH_CACHE, true);
} catch (InsufficientCapacityException e) {
throw new RuntimeException("This code should be unreachable!",
e);
}
}
}
示例11: create
import com.lmax.disruptor.dsl.ProducerType; //導入依賴的package包/類
/**
* Create a new Ring Buffer with the specified producer type (SINGLE or MULTI)
*
* @param producerType producer type to use {@link ProducerType}.
* @param factory used to create events within the ring buffer.
* @param bufferSize number of elements to create within the ring buffer.
* @param waitStrategy used to determine how to wait for new elements to become available.
* @throws IllegalArgumentException if bufferSize is less than 1 or not a power of 2
*/
public static <E> RingBuffer<E> create(ProducerType producerType,
EventFactory<E> factory,
int bufferSize,
WaitStrategy waitStrategy)
{
switch (producerType)
{
case SINGLE:
return createSingleProducer(factory, bufferSize, waitStrategy);
case MULTI:
return createMultiProducer(factory, bufferSize, waitStrategy);
default:
throw new IllegalStateException(producerType.toString());
}
}
示例12: open
import com.lmax.disruptor.dsl.ProducerType; //導入依賴的package包/類
/**
* Start dispatching events
*
* @return a Future that asyncrhonously notifies an observer when this EventReactor is ready
*/
@SuppressWarnings("unchecked")
public CompletableFuture<? extends EventReactor<T>> open() {
if (isRunning.compareAndSet(false, true)) {
CompletableFuture<EventReactor<T>> future = new CompletableFuture<>();
this.disruptor =
new Disruptor<>(EVENT_FACTORY, ringSize, threadFactory, ProducerType.MULTI,
new BusySpinWaitStrategy());
this.disruptor.handleEventsWith(this::handleEvent);
this.ringBuffer = disruptor.start();
// Starts a timer thread
this.timer = new Timer();
future.complete(this);
return future;
} else {
return CompletableFuture.completedFuture(this);
}
}
示例13: DisruptorQueueImpl
import com.lmax.disruptor.dsl.ProducerType; //導入依賴的package包/類
public DisruptorQueueImpl(String queueName, ProducerType producerType, int bufferSize, WaitStrategy wait) {
this._queueName = PREFIX + queueName;
_buffer = RingBuffer.create(producerType, new ObjectEventFactory(), bufferSize, wait);
_consumer = new Sequence();
_barrier = _buffer.newBarrier();
_buffer.addGatingSequences(_consumer);
if (producerType == ProducerType.SINGLE) {
consumerStartedFlag = true;
} else {
// make sure we flush the pending messages in cache first
if (bufferSize < 2) {
throw new RuntimeException("QueueSize must >= 2");
}
try {
publishDirect(FLUSH_CACHE, true);
} catch (InsufficientCapacityException e) {
throw new RuntimeException("This code should be unreachable!", e);
}
}
}
示例14: testLaterStartConsumer
import com.lmax.disruptor.dsl.ProducerType; //導入依賴的package包/類
@Test
public void testLaterStartConsumer() throws InterruptedException {
System.out.println("!!!!!!!!!!!!!!!Begin testLaterStartConsumer!!!!!!!!!!");
final AtomicBoolean messageConsumed = new AtomicBoolean(false);
// Set queue length to 1, so that the RingBuffer can be easily full
// to trigger consumer blocking
DisruptorQueue queue = createQueue("consumerHang", ProducerType.MULTI, 2);
push(queue, 1);
Runnable producer = new Producer(queue);
Runnable consumer = new Consumer(queue, new EventHandler<Object>() {
long count = 0;
@Override
public void onEvent(Object obj, long sequence, boolean endOfBatch) throws Exception {
messageConsumed.set(true);
System.out.println("Consume " + count++);
}
});
run(producer, 0, 0, consumer, 50);
Assert.assertTrue("disruptor message is never consumed due to consumer thread hangs", messageConsumed.get());
System.out.println("!!!!!!!!!!!!!!!!End testLaterStartConsumer!!!!!!!!!!");
}
示例15: testSingleProducer
import com.lmax.disruptor.dsl.ProducerType; //導入依賴的package包/類
@Test
public void testSingleProducer() throws InterruptedException {
System.out.println("!!!!!!!!!!!!!!Begin testSingleProducer!!!!!!!!!!!!!!");
final AtomicBoolean messageConsumed = new AtomicBoolean(false);
// Set queue length to 1, so that the RingBuffer can be easily full
// to trigger consumer blocking
DisruptorQueue queue = createQueue("consumerHang", ProducerType.SINGLE, 1);
push(queue, 1);
Runnable producer = new Producer(queue);
Runnable consumer = new Consumer(queue, new EventHandler<Object>() {
long count = 0;
@Override
public void onEvent(Object obj, long sequence, boolean endOfBatch) throws Exception {
messageConsumed.set(true);
System.out.println("Consume " + count++);
}
});
run(producer, 0, 0, consumer, 50);
Assert.assertTrue("disruptor message is never consumed due to consumer thread hangs", messageConsumed.get());
System.out.println("!!!!!!!!!!!!!!End testSingleProducer!!!!!!!!!!!!!!");
}