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

The following examples show how to use com.lmax.disruptor.RingBuffer#createSingleProducer() . 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: HashWheelTimer.java    From camunda-bpm-reactor with Apache License 2.0 4 votes vote down vote up
/**
 * Create a new {@code HashWheelTimer} using the given timer {@param resolution} and {@param wheelSize}. All times
 * will
 * rounded up to the closest multiple of this resolution.
 *
 * @param name      name for daemon thread factory to be displayed
 * @param res       resolution of this timer in milliseconds
 * @param wheelSize size of the Ring Buffer supporting the Timer, the larger the wheel, the less the lookup time is
 *                  for sparse timeouts. Sane default is 512.
 * @param strategy  strategy for waiting for the next tick
 * @param exec      Executor instance to submit tasks to
 */
public HashWheelTimer(String name, int res, int wheelSize, WaitStrategy strategy, Executor exec) {
  this.waitStrategy = strategy;

  this.wheel = RingBuffer.createSingleProducer(new EventFactory<Set<TimerPausable>>() {
    @Override
    public Set<TimerPausable> newInstance() {
      return new ConcurrentSkipListSet<TimerPausable>();
    }
  }, wheelSize);

  this.resolution = res;
  this.loop = new NamedDaemonThreadFactory(name).newThread(new Runnable() {
    @Override
    public void run() {
      long deadline = System.currentTimeMillis();

      while (true) {
        Set<TimerPausable> registrations = wheel.get(wheel.getCursor());

        for (TimerPausable r : registrations) {
          if (r.isCancelled()) {
            registrations.remove(r);
          } else if (r.ready()) {
            executor.execute(r);
            registrations.remove(r);

            if (!r.isCancelAfterUse()) {
              reschedule(r);
            }
          } else if (r.isPaused()) {
            reschedule(r);
          } else {
            r.decrement();
          }
        }

        deadline += resolution;

        try {
          waitStrategy.waitUntil(deadline);
        } catch (InterruptedException e) {
          return;
        }

        wheel.publish(wheel.next());
      }
    }
  });

  this.executor = exec;
  this.start();
}
 
Example 2
Source File: MysqlMultiStageCoprocessor.java    From canal with Apache License 2.0 4 votes vote down vote up
@Override
public void start() {
    super.start();
    this.exception = null;
    this.disruptorMsgBuffer = RingBuffer.createSingleProducer(new MessageEventFactory(),
        ringBufferSize,
        new BlockingWaitStrategy());
    int tc = parserThreadCount > 0 ? parserThreadCount : 1;
    this.parserExecutor = Executors.newFixedThreadPool(tc, new NamedThreadFactory("MultiStageCoprocessor-Parser-"
                                                                                  + destination));

    this.stageExecutor = Executors.newFixedThreadPool(2, new NamedThreadFactory("MultiStageCoprocessor-other-"
                                                                                + destination));
    SequenceBarrier sequenceBarrier = disruptorMsgBuffer.newBarrier();
    ExceptionHandler exceptionHandler = new SimpleFatalExceptionHandler();
    // stage 2
    this.logContext = new LogContext();
    simpleParserStage = new BatchEventProcessor<MessageEvent>(disruptorMsgBuffer,
        sequenceBarrier,
        new SimpleParserStage(logContext));
    simpleParserStage.setExceptionHandler(exceptionHandler);
    disruptorMsgBuffer.addGatingSequences(simpleParserStage.getSequence());

    // stage 3
    SequenceBarrier dmlParserSequenceBarrier = disruptorMsgBuffer.newBarrier(simpleParserStage.getSequence());
    WorkHandler<MessageEvent>[] workHandlers = new DmlParserStage[tc];
    for (int i = 0; i < tc; i++) {
        workHandlers[i] = new DmlParserStage();
    }
    workerPool = new WorkerPool<MessageEvent>(disruptorMsgBuffer,
        dmlParserSequenceBarrier,
        exceptionHandler,
        workHandlers);
    Sequence[] sequence = workerPool.getWorkerSequences();
    disruptorMsgBuffer.addGatingSequences(sequence);

    // stage 4
    SequenceBarrier sinkSequenceBarrier = disruptorMsgBuffer.newBarrier(sequence);
    sinkStoreStage = new BatchEventProcessor<MessageEvent>(disruptorMsgBuffer,
        sinkSequenceBarrier,
        new SinkStoreStage());
    sinkStoreStage.setExceptionHandler(exceptionHandler);
    disruptorMsgBuffer.addGatingSequences(sinkStoreStage.getSequence());

    // start work
    stageExecutor.submit(simpleParserStage);
    stageExecutor.submit(sinkStoreStage);
    workerPool.start(parserExecutor);
}
 
Example 3
Source File: DisruptorDispatchThread.java    From game-executor with Apache License 2.0 2 votes vote down vote up
public void initRingBuffer(){

        ringBuffer = RingBuffer.createSingleProducer(new CycleDisruptorEventFactory(), bufferSize, new BlockingWaitStrategy());
    }