com.lmax.disruptor.RingBuffer Java Examples
The following examples show how to use
com.lmax.disruptor.RingBuffer.
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 File: LongEventMain.java From elastic-rabbitmq with MIT License | 6 votes |
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 #2
Source File: TaskDispatcher.java From Jupiter with Apache License 2.0 | 6 votes |
@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 #3
Source File: DisruptorAdapterHandler.java From Okra with Apache License 2.0 | 6 votes |
@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 #4
Source File: BenchmarkHandler.java From Okra with Apache License 2.0 | 6 votes |
@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 #5
Source File: FastEventServiceImpl.java From redtorch with MIT License | 6 votes |
@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 #6
Source File: FastEventServiceImpl.java From redtorch with MIT License | 6 votes |
@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 #7
Source File: FastEventServiceImpl.java From redtorch with MIT License | 6 votes |
@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 #8
Source File: FastEventServiceImpl.java From redtorch with MIT License | 6 votes |
@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 #9
Source File: RingBufferWorkProcessor.java From camunda-bpm-reactor with Apache License 2.0 | 6 votes |
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 #10
Source File: KeyValueLocator.java From couchbase-jvm-core with Apache License 2.0 | 6 votes |
/** * 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 #11
Source File: RingBufferProcessor.java From camunda-bpm-reactor with Apache License 2.0 | 6 votes |
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 File: DisruptorTransfer.java From cicada with MIT License | 6 votes |
@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 #13
Source File: FastEventServiceImpl.java From redtorch with MIT License | 6 votes |
@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 #14
Source File: DisruptorExample.java From pragmatic-java-engineer with GNU General Public License v3.0 | 6 votes |
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 #15
Source File: DisruptorExecutorService.java From game-executor with Apache License 2.0 | 6 votes |
@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 #16
Source File: TaskDispatcher.java From sofa-jraft with Apache License 2.0 | 6 votes |
@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 #17
Source File: CoreContext.java From couchbase-jvm-core with Apache License 2.0 | 5 votes |
/** * 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 #18
Source File: DisruptorService.java From litchi with Apache License 2.0 | 5 votes |
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 File: DisruptorIntegrationTest.java From tutorials with MIT License | 5 votes |
@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 #20
Source File: FastEventServiceImpl.java From redtorch with MIT License | 5 votes |
@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 #21
Source File: DisruptorProducer.java From md_blockchain with Apache License 2.0 | 5 votes |
@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 #22
Source File: SingleEventProducer.java From tutorials with MIT License | 5 votes |
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 #23
Source File: DisruptorIntegrationTest.java From tutorials with MIT License | 5 votes |
@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 #24
Source File: TxTransactionEventPublisher.java From Raincat with GNU Lesser General Public License v3.0 | 5 votes |
/** * 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 #25
Source File: DelayedMultiEventProducer.java From tutorials with MIT License | 5 votes |
@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 #26
Source File: RingBufferWorkerPoolFactory.java From netty-pubsub with MIT License | 5 votes |
/** * * @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 #27
Source File: RingBufferSubscriberUtils.java From camunda-bpm-reactor with Apache License 2.0 | 5 votes |
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); }
Example #28
Source File: DisruptorEventQueue.java From opencensus-java with Apache License 2.0 | 5 votes |
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 #29
Source File: KafkaRpcServer.java From devicehive-java-server with Apache License 2.0 | 5 votes |
@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 #30
Source File: InputReader.java From perf-workshop with Apache License 2.0 | 5 votes |
public InputReader(final RingBuffer<Packet> messageSink, final NanoTimer nanoTimer, final CommandLineArgs commandLineArgs) { this.messageSink = messageSink; this.nanoTimer = nanoTimer; this.commandLineArgs = commandLineArgs; this.numberOfIterations = commandLineArgs.getNumberOfIterations(); }