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

The following examples show how to use com.lmax.disruptor.RingBuffer#get() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
Source File: TaskDispatcher.java    From Jupiter with 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 9
Source File: TaskDispatcher.java    From sofa-jraft with 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 10
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 11
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 12
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 13
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 14
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 15
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 16
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 17
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 18
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);
	}
}
 
Example 19
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 20
Source File: RingBufferSubscriberUtils.java    From camunda-bpm-reactor with Apache License 2.0 4 votes vote down vote up
public static <T> boolean waitRequestOrTerminalEvent(
  Sequence pendingRequest,
  RingBuffer<MutableSignal<T>> ringBuffer,
  SequenceBarrier barrier,
  Subscriber<? super T> subscriber,
  AtomicBoolean isRunning
) {
  final long waitedSequence = ringBuffer.getCursor() + 1L;
  try {
    MutableSignal<T> event = null;
    while (pendingRequest.get() < 0l) {
      //pause until first request
      if (event == null) {
        barrier.waitFor(waitedSequence);
        event = ringBuffer.get(waitedSequence);

        if (event.type == MutableSignal.Type.COMPLETE) {
          try {
            subscriber.onComplete();
            return false;
          } catch (Throwable t) {
            Exceptions.throwIfFatal(t);
            subscriber.onError(t);
            return false;
          }
        } else if (event.type == MutableSignal.Type.ERROR) {
          subscriber.onError(event.error);
          return false;
        }
      } else {
        barrier.checkAlert();
      }
      LockSupport.parkNanos(1l);
    }
  } catch (TimeoutException te) {
    //ignore
  } catch (AlertException ae) {
    if (!isRunning.get()) {
      return false;
    }
  } catch (InterruptedException ie) {
    Thread.currentThread().interrupt();
  }

  return true;
}