Java Code Examples for com.lmax.disruptor.RingBuffer#next()

The following examples show how to use com.lmax.disruptor.RingBuffer#next() . 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: FastEventServiceImpl.java    From redtorch with 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 2
Source File: FastEventServiceImpl.java    From redtorch with 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 3
Source File: FastEventServiceImpl.java    From redtorch with MIT License 6 votes vote down vote up
@Override
public void emitTick(TickField tick) {

	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(tick);
		fastEvent.setEvent(tick.getUnifiedSymbol()+"@"+tick.getGatewayId());
		fastEvent.setFastEventType(FastEventType.TICK);

	} finally {
		ringBuffer.publish(sequence);
	}

}
 
Example 4
Source File: FastEventServiceImpl.java    From redtorch with 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 File: FastEventServiceImpl.java    From redtorch with 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 File: FastEventServiceImpl.java    From redtorch with 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
Source File: BenchmarkHandler.java    From Okra with 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 8
Source File: DisruptorAdapterHandler.java    From Okra with 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 9
Source File: LogicProcessor.java    From Okra with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    while (true) {
        Executor executor;
        while ((executor = msgQueue.poll()) != null) {
            RingBuffer<ConcurrentEvent> rb = disruptor.getRingBuffer();
            long next = rb.next();
            try {
                ConcurrentEvent event = rb.get(next);
                event.setValues(executor);
            } finally {
                rb.publish(next);
                waitSize.decrementAndGet();
            }
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 
Example 10
Source File: RingBufferSubscriberUtils.java    From camunda-bpm-reactor with Apache License 2.0 5 votes vote down vote up
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 11
Source File: SingleEventProducer.java    From tutorials with 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 12
Source File: DelayedMultiEventProducer.java    From tutorials with MIT License 5 votes vote down vote up
private void produce(final RingBuffer<ValueEvent> ringBuffer, final int count, final boolean addDelay) {
    for (int i = 0; i < count; i++) {
        final long seq = ringBuffer.next();
        final ValueEvent valueEvent = ringBuffer.get(seq);
        valueEvent.setValue(i);
        ringBuffer.publish(seq);
        if (addDelay) {
            addDelay();
        }
    }
}
 
Example 13
Source File: SlowLogRecorder.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Add slow log rpcCall details to ringbuffer
 *
 * @param rpcLogDetails all details of rpc call that would be useful for ring buffer
 *   consumers
 */
public void addSlowLogPayload(RpcLogDetails rpcLogDetails) {
  if (!isOnlineLogProviderEnabled) {
    return;
  }
  RingBuffer<RingBufferEnvelope> ringBuffer = this.disruptor.getRingBuffer();
  long seqId = ringBuffer.next();
  try {
    ringBuffer.get(seqId).load(rpcLogDetails);
  } finally {
    ringBuffer.publish(seqId);
  }
}
 
Example 14
Source File: DisruptorAdapterBy41xHandler.java    From Okra with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, O msg) throws Exception {
    Session session = SESSIONS.get(ctx.channel().id());
    if (null == session) {
        return;
    }
    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 15
Source File: RingBufferSubscriberUtils.java    From camunda-bpm-reactor with Apache License 2.0 5 votes vote down vote up
public static <E> void onComplete(RingBuffer<MutableSignal<E>> ringBuffer) {
  final long seqId = ringBuffer.next();
  final MutableSignal<E> signal = ringBuffer.get(seqId);

  signal.type = MutableSignal.Type.COMPLETE;
  signal.value = null;
  signal.error = null;

  ringBuffer.publish(seqId);
}
 
Example 16
Source File: RingBufferSubscriberUtils.java    From camunda-bpm-reactor with Apache License 2.0 5 votes vote down vote up
public static <E> void onError(Throwable error, RingBuffer<MutableSignal<E>> ringBuffer) {
  if (error == null) {
    throw SpecificationExceptions.spec_2_13_exception();
  }

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

  signal.type = MutableSignal.Type.ERROR;
  signal.value = null;
  signal.error = error;

  ringBuffer.publish(seqId);
}
 
Example 17
Source File: DisruptorProducer.java    From md_blockchain with 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 18
Source File: DisruptorEventQueue.java    From opencensus-java with 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 19
Source File: FastEventServiceImpl.java    From redtorch with 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 20
Source File: FastEventServiceImpl.java    From redtorch with MIT License 5 votes vote down vote up
@Override
public void emitEvent(FastEventType fastEventType, String event, Object obj) {
	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.setFastEventType(fastEventType);
		fastEvent.setEvent(event);
		fastEvent.setObj(obj);

	} finally {
		ringBuffer.publish(sequence);
	}
}