Esempio n. 1
0
 private void consumeBatchToCursor(long cursor, EventHandler<Object> handler) {
   for (long curr = _consumer.get() + 1; curr <= cursor; curr++) {
     try {
       MutableObject mo = _buffer.get(curr);
       Object o = mo.o;
       mo.setObject(null);
       if (o == FLUSH_CACHE) {
         Object c = null;
         while (true) {
           c = _cache.poll();
           if (c == null) break;
           else handler.onEvent(c, curr, true);
         }
       } else if (o == INTERRUPT) {
         throw new InterruptedException("Disruptor processing interrupted");
       } else {
         handler.onEvent(o, curr, curr == cursor);
       }
     } catch (Exception e) {
       throw new RuntimeException(e);
     }
   }
   // TODO: only set this if the consumer cursor has changed?
   _consumer.set(cursor);
 }
Esempio n. 2
0
  /**
   * It is ok to have another thread rerun this method after a halt().
   *
   * @throws IllegalStateException if this object instance is already running in a thread
   */
  @Override
  public void run() {
    if (!running.compareAndSet(false, true)) {
      throw new IllegalStateException("Thread is already running");
    }
    sequenceBarrier.clearAlert();

    notifyStart();

    T event = null;
    long nextSequence = sequence.get() + 1L;
    while (true) {
      try {
        final long availableSequence = sequenceBarrier.waitFor(nextSequence);

        while (nextSequence <= availableSequence) {
          event = dataProvider.get(nextSequence);
          eventHandler.onEvent(event, nextSequence, nextSequence == availableSequence);
          nextSequence++;
        }

        sequence.set(availableSequence);
      } catch (final TimeoutException e) {
        notifyTimeout(sequence.get());
      } catch (final AlertException ex) {
        if (!running.get()) {
          break;
        }
      } catch (final Throwable ex) {
        exceptionHandler.handleEventException(ex, nextSequence, event);
        sequence.set(nextSequence);
        nextSequence++;
      }
    }

    notifyShutdown();

    running.set(false);
  }