Java Code Examples for com.lmax.disruptor.RingBuffer

The following examples show how to use com.lmax.disruptor.RingBuffer. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: sofa-jraft   Source File: TaskDispatcher.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean dispatch(final Runnable message) {
    final RingBuffer<MessageEvent<Runnable>> ringBuffer = disruptor.getRingBuffer();
    try {
        final long sequence = ringBuffer.tryNext();
        try {
            final MessageEvent<Runnable> event = ringBuffer.get(sequence);
            event.setMessage(message);
        } finally {
            ringBuffer.publish(sequence);
        }
        return true;
    } catch (final InsufficientCapacityException e) {
        // This exception is used by the Disruptor as a global goto. It is a singleton
        // and has no stack trace.  Don't worry about performance.
        return false;
    }
}
 
Example 2
Source Project: redtorch   Source File: FastEventServiceImpl.java    License: MIT License 6 votes vote down vote up
@Override
public void emitAccount(AccountField account) {
	// 发送事件

	RingBuffer<FastEvent> ringBuffer = getRingBuffer();
	long sequence = ringBuffer.next(); // Grab the next sequence
	try {
		FastEvent fastEvent = ringBuffer.get(sequence); // Get the entry in the Disruptor for the sequence
		fastEvent.setObj(account);
		fastEvent.setEvent(account.getAccountId());
		fastEvent.setFastEventType(FastEventType.ACCOUNT);
	} finally {
		ringBuffer.publish(sequence);
	}

}
 
Example 3
Source Project: redtorch   Source File: FastEventServiceImpl.java    License: MIT License 6 votes vote down vote up
@Override
public void emitContract(ContractField contract) {

	// 发送事件

	RingBuffer<FastEvent> ringBuffer = getRingBuffer();
	long sequence = ringBuffer.next(); // Grab the next sequence
	try {
		FastEvent fastEvent = ringBuffer.get(sequence); // Get the entry in the Disruptor for the sequence
		fastEvent.setObj(contract);
		fastEvent.setEvent(contract.getContractId());
		fastEvent.setFastEventType(FastEventType.CONTRACT);
	} finally {
		ringBuffer.publish(sequence);
	}

}
 
Example 4
Source Project: redtorch   Source File: FastEventServiceImpl.java    License: MIT License 6 votes vote down vote up
@Override
public void emitTrade(TradeField trade) {

	// 发送特定合约成交事件
	RingBuffer<FastEvent> ringBuffer = getRingBuffer();
	long sequence = ringBuffer.next(); // Grab the next sequence
	try {
		FastEvent fastEvent = ringBuffer.get(sequence); // Get the entry in the Disruptor for the sequence
		fastEvent.setObj(trade);
		fastEvent.setEvent(trade.getOriginOrderId());
		fastEvent.setFastEventType(FastEventType.TRADE);

	} finally {
		ringBuffer.publish(sequence);
	}

}
 
Example 5
Source Project: redtorch   Source File: FastEventServiceImpl.java    License: MIT License 6 votes vote down vote up
@Override
public void emitOrder(OrderField order) {

	// 发送带定单ID的事件
	RingBuffer<FastEvent> ringBuffer = getRingBuffer();
	long sequence = ringBuffer.next(); // Grab the next sequence

	try {
		FastEvent fastEvent = ringBuffer.get(sequence); // Get the entry in the Disruptor for the sequence
		fastEvent.setObj(order);
		fastEvent.setEvent(order.getOriginOrderId());
		fastEvent.setFastEventType(FastEventType.ORDER);

	} finally {
		ringBuffer.publish(sequence);
	}

}
 
Example 6
Source Project: redtorch   Source File: FastEventServiceImpl.java    License: MIT License 6 votes vote down vote up
@Override
public void emitNotice(NoticeField notice) {
	RingBuffer<FastEvent> ringBuffer = getRingBuffer();
	long sequence = ringBuffer.next(); // Grab the next sequence

	try {
		FastEvent fastEvent = ringBuffer.get(sequence); // Get the entry in the Disruptor for the sequence
		fastEvent.setObj(notice);
		fastEvent.setEvent(FastEventType.NOTICE.getDeclaringClass().getName());
		fastEvent.setFastEventType(FastEventType.NOTICE);

	} finally {
		ringBuffer.publish(sequence);
	}

}
 
Example 7
public void test() throws Exception {
    TestEventFactory factory = new TestEventFactory();

    ThreadFactory threadFactory = Executors.defaultThreadFactory();
    Disruptor<TestEvent> disruptor = new Disruptor<>(factory, 1024, threadFactory);

    disruptor.handleEventsWith(new TestEventHandler());

    disruptor.start();

    RingBuffer<TestEvent> ringBuffer = disruptor.getRingBuffer();

    TestEventProducerWithTranslator producer = new TestEventProducerWithTranslator(ringBuffer);

    ByteBuffer bb = ByteBuffer.allocate(8);
    for (long l = 0; true; l++) {
        bb.putLong(0, l);
        producer.onData(bb);
        Thread.sleep(1000);
    }
}
 
Example 8
Source Project: game-executor   Source File: DisruptorExecutorService.java    License: Apache License 2.0 6 votes vote down vote up
@Override
    public void startup() {
        EventBus eventBus = disruptorDispatchThread.getEventBus();
        executorService = new NonOrderedQueuePoolExecutor(poolName, excutorSize);
        cycleEventHandler = new CycleEventHandler[excutorSize];
        for(int i = 0; i < excutorSize; i++){
            cycleEventHandler[i] = new CycleEventHandler(eventBus);
        }

        RingBuffer ringBuffer = disruptorDispatchThread.getRingBuffer();
        workerPool = new WorkerPool(ringBuffer, ringBuffer.newBarrier(), new FatalExceptionHandler(), cycleEventHandler);
        ringBuffer.addGatingSequences(workerPool.getWorkerSequences());

        workerPool.start(executorService);

//        BatchEventProcessor<CycleEvent>[] batchEventProcessors = new BatchEventProcessor[excutorSize];
//        for(int i = 0; i < excutorSize; i++){
//            batchEventProcessors[i] = new BatchEventProcessor<>(ringBuffer, sequenceBarrier, cycleEventHandler[i]);
//            ringBuffer.addGatingSequences(batchEventProcessors[i].getSequence());
////            executorService.submit(batchEventProcessors[i]);
//        }
    }
 
Example 9
Source Project: elastic-rabbitmq   Source File: LongEventMain.java    License: MIT License 6 votes vote down vote up
public static void main(String[] args) throws Exception {
        Executor executor = Executors.newCachedThreadPool();

        LongEventFactory eventFactory = new LongEventFactory();
        int bufferSize = 1024;

//        Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(eventFactory, bufferSize, executor);
//        disruptor.handleEventsWith(new LongEventHandler());
//        disruptor.start();

        Disruptor<LongEvent> disruptor = new Disruptor<>(LongEvent::new, bufferSize, executor);
        disruptor.handleEventsWith((event, sequence, endOfBatch) -> {System.out.println("Event: " + event);
            System.out.println("CurrentThreadName:" + Thread.currentThread().getName());
        });
        disruptor.start();

        RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
        LongEventProducer producer = new LongEventProducer(ringBuffer);

        ByteBuffer bb = ByteBuffer.allocate(8);
        for (long l = 0; true; l++) {
            bb.putLong(0, l);
            ringBuffer.publishEvent((event, sequence, buffer) -> event.set(buffer.getLong(0)), bb);

           // producer.onData(bb);
            //Thread.sleep(1000);
        }
    }
 
Example 10
Source Project: cicada   Source File: DisruptorTransfer.java    License: MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
public DisruptorTransfer(final SpanEventHandler spanEventHandler, final int buffSize) {
  // Executor executor = Executors.newCachedThreadPool();
  final ThreadFactory threadFactory = Executors.defaultThreadFactory();

  // The factory for the event
  final SpanEventFactory factory = new SpanEventFactory();

  // Specify the size of the ring buffer, must be power of 2.
  final int bufferSize = buffSize;

  // Construct the Disruptor
  disruptor = new Disruptor<SpanEvent>(factory, bufferSize, threadFactory);

  // Connect the handler
  // disruptor.handleEventsWith(new
  // SpanEventHandler("http://localhost:9080/upload"));
  disruptor.handleEventsWith(spanEventHandler);

  // Start the Disruptor, starts all threads running
  disruptor.start();

  final RingBuffer<SpanEvent> ringBuffer = disruptor.getRingBuffer();
  producer = new SpanEventProducer(ringBuffer);
}
 
Example 11
Source Project: camunda-bpm-reactor   Source File: RingBufferProcessor.java    License: Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 12
Source Project: couchbase-jvm-core   Source File: KeyValueLocator.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Locates the proper {@link Node}s for a Memcache bucket.
 *
 * @param request the request.
 * @param nodes the managed nodes.
 * @param config the bucket configuration.
 */
private static void locateForMemcacheBucket(final BinaryRequest request, final List<Node> nodes,
    final MemcachedBucketConfig config, CoreEnvironment env, RingBuffer<ResponseEvent> responseBuffer) {

    if (!keyIsValid(request)) {
        return;
    }

    String hostname = config.nodeForId(request.keyBytes());
    request.partition((short) 0);

    for (Node node : nodes) {
        if (node.hostname().equals(hostname)) {
            node.send(request);
            return;
        }
    }

    if(handleNotEqualNodeSizes(config.nodes().size(), nodes.size())) {
        RetryHelper.retryOrCancel(env, request, responseBuffer);
        return;
    }

    throw new IllegalStateException("Node not found for request" + request);
}
 
Example 13
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);

}
 
Example 14
Source Project: Jupiter   Source File: TaskDispatcher.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean dispatch(Runnable message) {
    RingBuffer<MessageEvent<Runnable>> ringBuffer = disruptor.getRingBuffer();
    try {
        long sequence = ringBuffer.tryNext();
        try {
            MessageEvent<Runnable> event = ringBuffer.get(sequence);
            event.setMessage(message);
        } finally {
            ringBuffer.publish(sequence);
        }
        return true;
    } catch (InsufficientCapacityException e) {
        // 这个异常是Disruptor当做全局goto使用的, 是单例的并且没有堆栈信息, 不必担心抛出异常的性能问题
        return false;
    }
}
 
Example 15
Source Project: Okra   Source File: DisruptorAdapterHandler.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, O msg) throws Exception {
    UUID uuid = CHANNEL_UUID.get(ctx.channel());
    if (null != uuid) {
        Session session = SESSIONS.get(uuid);
        if (null != session) {
            RingBuffer<ConcurrentEvent> ringBuffer = THREAD_LOCAL.get().getRingBuffer();
            long next = ringBuffer.next();
            try {
                ConcurrentEvent commandEvent = ringBuffer.get(next);
                commandEvent.setValues(newExecutor(session, msg));
            } finally {
                ringBuffer.publish(next);
            }
        }
    }
}
 
Example 16
Source Project: Okra   Source File: BenchmarkHandler.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
    RingBuffer<ConcurrentEvent> ringBuffer = THREAD_LOCAL.get().getRingBuffer();
    long next = ringBuffer.next();
    try {
        ConcurrentEvent commandEvent = ringBuffer.get(next);
        commandEvent.setValues(new Executor() {
            @Override
            public void onExecute() {
                ctx.channel().writeAndFlush(msg);
            }

            @Override
            public void release() {
            }
        });
    } finally {
        ringBuffer.publish(next);
    }
}
 
Example 17
Source Project: netty-pubsub   Source File: RingBufferWorkerPoolFactory.java    License: MIT License 5 votes vote down vote up
/**
 * 
 * @param type  ����������  �������� ��������
 * @param bufferSize  ringbuffer������
 * @param waitStrategy   �ȴ�����
 * @param messageConsumers  ������
 */
@SuppressWarnings("unchecked")
public void initAndStart(ProducerType type, int bufferSize, WaitStrategy waitStrategy, MessageConsumer[] messageConsumers) {
	if(!startFlag){
	//1. ����ringBuffer����
	this.ringBuffer = RingBuffer.create(type,
			new EventFactory<MessageWrapper>() {
				public MessageWrapper newInstance() {
					return new MessageWrapper();
				}
			},
			bufferSize,
			waitStrategy);
	//2.�������դ��
	this.sequenceBarrier = this.ringBuffer.newBarrier();
	//3.���ù�����
	this.workerPool = new WorkerPool(this.ringBuffer,
			this.sequenceBarrier, 
			new EventExceptionHandler(), messageConsumers);
	//4 �����������������������
	for(MessageConsumer mc : messageConsumers){
		this.consumers.put(mc.getConsumerId(), mc);
	}
	//5 ������ǵ�sequences
	this.ringBuffer.addGatingSequences(this.workerPool.getWorkerSequences());
	//6 �������ǵĹ�����
	this.workerPool.start(Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()/2));
	//7.����������־����ֹ�ظ�����
	startFlag=true;
	}
}
 
Example 18
Source Project: litchi   Source File: DisruptorService.java    License: Apache License 2.0 5 votes vote down vote up
public boolean isEmpty(int threadId) {
    List<MessageThread> list = threadMaps.get(threadId);
    if (list == null) {
        return true;
    }

    for (MessageThread thread : list) {
        RingBuffer<MessageBuffer> ringBuffer = thread.getRingBuffer();
        if (ringBuffer.getBufferSize() != ringBuffer.remainingCapacity()) {
            return false;
        }
    }
    return true;
}
 
Example 19
Source Project: akka-disruptor   Source File: DisruptorMessageQueue.java    License: MIT License 5 votes vote down vote up
DisruptorMessageQueue(int bufferSize, WaitStrategy waitStrategy) {
    ringBuffer = RingBuffer.createMultiProducer(EVENT_FACTORY, bufferSize, waitStrategy);
    ringBuffer.addGatingSequences(sequence);
    sequenceBarrier = ringBuffer.newBarrier();
    mask = bufferSize - 1;
    buffer = new Envelope[bufferSize];
}
 
Example 20
Source Project: tutorials   Source File: DelayedMultiEventProducer.java    License: MIT License 5 votes vote down vote up
@Override
public void startProducing(final RingBuffer<ValueEvent> ringBuffer, final int count) {
    final Runnable simpleProducer = () -> produce(ringBuffer, count, false);
    final Runnable delayedProducer = () -> produce(ringBuffer, count, true);
    new Thread(simpleProducer).start();
    new Thread(delayedProducer).start();
}
 
Example 21
Source Project: couchbase-jvm-core   Source File: CoreContext.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new {@link CoreContext} with no a core id.
 *
 * @param environment the environment to share.
 * @param responseRingBuffer the response ring buffer to share.
 * @param coreId the core id to use.
 */
public CoreContext(final CoreEnvironment environment,
    final RingBuffer<ResponseEvent> responseRingBuffer,
    final long coreId) {
    this.environment = environment;
    this.responseRingBuffer = responseRingBuffer;
    this.coreId = coreId;
}
 
Example 22
Source Project: md_blockchain   Source File: DisruptorProducer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void publish(BaseEvent baseEvent) {
    RingBuffer<BaseEvent> ringBuffer = disruptor.getRingBuffer();
    long sequence = ringBuffer.next();
    try {
        // Get the entry in the Disruptor
        BaseEvent event = ringBuffer.get(sequence);
        // for the sequence   // Fill with data
        event.setBlockPacket(baseEvent.getBlockPacket());
        event.setChannelContext(baseEvent.getChannelContext());
    } finally {
        ringBuffer.publish(sequence);
    }
}
 
Example 23
Source Project: tutorials   Source File: DisruptorIntegrationTest.java    License: MIT License 5 votes vote down vote up
@Test
public void whenMultipleProducerMultipleConsumer_thenOutputInFifoOrder() {
    final EventConsumer eventConsumer = new MultiEventPrintConsumer();
    final EventProducer eventProducer = new DelayedMultiEventProducer();
    createDisruptor(ProducerType.MULTI, eventConsumer);
    final RingBuffer<ValueEvent> ringBuffer = disruptor.start();

    startProducing(ringBuffer, 32, eventProducer);

    disruptor.halt();
    disruptor.shutdown();
}
 
Example 24
Source Project: redtorch   Source File: FastEventServiceImpl.java    License: MIT License 5 votes vote down vote up
@Override
public void emitPosition(PositionField position) {

	RingBuffer<FastEvent> ringBuffer = getRingBuffer();
	long sequence = ringBuffer.next(); // Grab the next sequence
	try {
		FastEvent fastEvent = ringBuffer.get(sequence); // Get the entry in the Disruptor for the sequence
		fastEvent.setObj(position);
		fastEvent.setEvent(position.getPositionId());
		fastEvent.setFastEventType(FastEventType.POSITION);

	} finally {
		ringBuffer.publish(sequence);
	}
}
 
Example 25
/**
 * publish disruptor event.
 *
 * @param transactionRecover {@linkplain TransactionRecover }
 * @param type               {@linkplain CompensationActionEnum}
 */
public void publishEvent(final TransactionRecover transactionRecover, final int type) {
    if (txConfig.getCompensation()) {
        final RingBuffer<TxTransactionEvent> ringBuffer = disruptor.getRingBuffer();
        ringBuffer.publishEvent(new TxTransactionEventTranslator(type), transactionRecover);
    }
}
 
Example 26
Source Project: devicehive-java-server   Source File: KafkaRpcServer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void start() {
    final ExecutorService execService = Executors.newFixedThreadPool(workerThreads);
    RingBuffer<ServerEvent> ringBuffer = workerPool.start(execService);
    logger.info("LMAX Disruptor started. Buffer size: {}", ringBuffer.getBufferSize());
    requestConsumer.startConsumers(ringBuffer);
}
 
Example 27
Source Project: tutorials   Source File: SingleEventProducer.java    License: MIT License 5 votes vote down vote up
private void produce(final RingBuffer<ValueEvent> ringBuffer, final int count) {
    for (int i = 0; i < count; i++) {
        final long seq = ringBuffer.next();
        final ValueEvent valueEvent = ringBuffer.get(seq);
        valueEvent.setValue(i);
        ringBuffer.publish(seq);
    }
}
 
Example 28
Source Project: tutorials   Source File: DisruptorIntegrationTest.java    License: MIT License 5 votes vote down vote up
@Test
public void whenSingleProducerMultipleConsumer_thenOutputInFifoOrder() {
    final EventConsumer eventConsumer = new MultiEventConsumer();
    final EventProducer eventProducer = new SingleEventProducer();
    createDisruptor(ProducerType.SINGLE, eventConsumer);
    final RingBuffer<ValueEvent> ringBuffer = disruptor.start();

    startProducing(ringBuffer, 32, eventProducer);

    disruptor.halt();
    disruptor.shutdown();
}
 
Example 29
Source Project: opencensus-java   Source File: DisruptorEventQueue.java    License: Apache License 2.0 5 votes vote down vote up
private static DisruptorEventQueue create() {
  // Create new Disruptor for processing. Note that Disruptor creates a single thread per
  // consumer (see https://github.com/LMAX-Exchange/disruptor/issues/121 for details);
  // this ensures that the event handler can take unsynchronized actions whenever possible.
  Disruptor<DisruptorEvent> disruptor =
      new Disruptor<>(
          DisruptorEventFactory.INSTANCE,
          DISRUPTOR_BUFFER_SIZE,
          new DaemonThreadFactory("OpenCensus.Disruptor"),
          ProducerType.MULTI,
          new SleepingWaitStrategy(0, 1000 * 1000));
  disruptor.handleEventsWith(new DisruptorEventHandler[] {DisruptorEventHandler.INSTANCE});
  disruptor.start();
  final RingBuffer<DisruptorEvent> ringBuffer = disruptor.getRingBuffer();

  DisruptorEnqueuer enqueuer =
      new DisruptorEnqueuer() {
        @Override
        public void enqueue(Entry entry) {
          long sequence = ringBuffer.next();
          try {
            DisruptorEvent event = ringBuffer.get(sequence);
            event.setEntry(entry);
          } finally {
            ringBuffer.publish(sequence);
          }
        }
      };
  return new DisruptorEventQueue(disruptor, enqueuer);
}
 
Example 30
public static <E> void onNext(E value, RingBuffer<MutableSignal<E>> ringBuffer) {
  if (value == null) {
    throw SpecificationExceptions.spec_2_13_exception();
  }

  final long seqId = ringBuffer.next();
  final MutableSignal<E> signal = ringBuffer.get(seqId);
  signal.type = MutableSignal.Type.NEXT;
  signal.value = value;

  ringBuffer.publish(seqId);
}