/** {@inheritDoc} */
  @SuppressWarnings("unchecked")
  @Override
  public <T> T createRingBufferProxy(
      final Class<T> proxyInterface,
      final Disruptor<ProxyMethodInvocation> disruptor,
      final OverflowStrategy overflowStrategy,
      final T implementation) {
    validator.validateAll(disruptor, proxyInterface);

    disruptor.handleEventsWith(createSingleImplementationHandlerChain(implementation));

    final ArgumentHolderGenerator argumentHolderGenerator = new ArgumentHolderGenerator(classPool);
    argumentHolderGenerator.createArgumentHolderClass(proxyInterface);

    prefillArgumentHolderObjects(disruptor.getRingBuffer(), argumentHolderGenerator);

    final Map<Method, Invoker> methodToInvokerMap =
        createMethodToInvokerMap(proxyInterface, argumentHolderGenerator);

    return generateProxy(
        proxyInterface,
        disruptor.getRingBuffer(),
        methodToInvokerMap,
        overflowStrategy,
        argumentHolderGenerator);
  }
  @SuppressWarnings("unchecked")
  @Override
  public void init() throws ServletException {
    try {
      disruptor =
          new Disruptor<byte[]>(
              new EventFactory<byte[]>() {
                @Override
                public byte[] newInstance() {
                  return new byte[256];
                }
              },
              262144,
              executor);

      disruptor.handleEventsWith(new LogicHandler());
      disruptor.start();
      generator = new Generator(executor);

      MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
      mBeanServer.registerMBean(
          new StandardMBean(generator, GeneratorMBean.class),
          new ObjectName("com.lmax:type=generator"));
    } catch (Exception e) {
      throw new ServletException(e);
    }
  }
 @SafeVarargs
 public ShriekingMushroom(int bufferSize, Executor exec, EventHandler<Event>... events) {
   disrupt = new Disruptor<Event>(EventBuilder.FACTORY, bufferSize, exec);
   disrupt.handleEventsWith(events);
   RingBuffer<Event> buffer = disrupt.start();
   builder = new EventBuilder(buffer);
 }
 @SuppressWarnings("unchecked")
 public static void createDisruptors(DisruptorType type, DisruptorConfig disruptorConfig) {
   WaitStrategy inboundWaitStrategy = getWaitStrategy(disruptorConfig.getDisruptorWaitStrategy());
   for (int i = 0; i < disruptorConfig.getNoDisruptors(); i++) {
     ExecutorService executorService =
         Executors.newFixedThreadPool(disruptorConfig.getNoOfEventHandlersPerDisruptor());
     Disruptor disruptor =
         new Disruptor<>(
             CarbonDisruptorEvent.EVENT_FACTORY,
             disruptorConfig.getBufferSize(),
             executorService,
             ProducerType.MULTI,
             inboundWaitStrategy);
     ExceptionHandler exh = new GenericExceptionHandler();
     EventHandler[] eventHandlers =
         new EventHandler[disruptorConfig.getNoOfEventHandlersPerDisruptor()];
     for (int j = 0; j < disruptorConfig.getNoOfEventHandlersPerDisruptor(); j++) {
       EventHandler eventHandler = new CarbonDisruptorEventHandler();
       eventHandlers[j] = eventHandler;
     }
     disruptor.handleEventsWith(eventHandlers);
     for (EventHandler eventHandler : eventHandlers) {
       disruptor.handleExceptionsFor(eventHandler).with(exh);
     }
     disruptorConfig.addDisruptor(disruptor.start());
   }
   disruptorConfigHashMap.put(type, disruptorConfig);
 }
  public void engage() {
    System.out.println("Starting the Disruptor");
    // starts the event processors
    final RingBuffer<DemoEvent> ringBuf = disruptor.start();

    // now we start the publishers
    List<Future<?>> futures = new ArrayList<Future<?>>();
    ExecutorService execService = Executors.newFixedThreadPool(NUM_PUBLISHERS);

    try {
      for (DisruptorDemoPublisher pub : pubs) {
        futures.add(execService.submit(pub));
      }

      // wait for each publisher to finish
      for (Future<?> f : futures) {
        f.get();
      }

      // this should wait until all events are processed
      disruptor.shutdown();

    } catch (Exception e) {
      System.out.println(e.toString());
      e.printStackTrace();

    } finally {
      execService.shutdown();
    }
  }
 private void initProcess() {
   SequenceBarrier sequenceBarrier = disruptor.getRingBuffer().newBarrier();
   EventHandler<SmsEvent> handler =
       FleetGuiceInjector.getInjector().getInstance(SmsEventHandler.class);
   BatchEventProcessor<SmsEvent> batchEventProcessor =
       new BatchEventProcessor<>(disruptor.getRingBuffer(), sequenceBarrier, handler);
   disruptor.handleEventsWith(batchEventProcessor);
 }
  public void start() throws IllegalStateException {
    if (disruptor == null) {
      throw new IllegalStateException("disruptor == null, call init");
    }

    disruptor.start();
  }
 @Override
 public void run() {
   TradeTransactionEventTranslator translator = new TradeTransactionEventTranslator();
   for (int i = 0; i < LOOP; i++) {
     disruptor.publishEvent(translator);
   }
   latch.countDown();
 }
  @SuppressWarnings("unchecked")
  public MultiplePublishersUsingDisruptorNoTranslator() {
    disruptor =
        new Disruptor<DemoEvent>(
            DemoEvent.FACTORY,
            Executors.newCachedThreadPool(), // TODO: is this the right one to use here???
            new MultiThreadedClaimStrategy(RING_BUFFER_SZ),
            new YieldingWaitStrategy());
    disruptor.handleEventsWith(new DemoEventHandler(1)); // just one consumer for this demo

    for (int i = 1; i <= NUM_PUBLISHERS; i++) {
      pubs.add(
          new DisruptorDemoPublisher(
                  i, NUM_MSGS_TO_PUBLISH_PER_PUBLISHER, disruptor.getRingBuffer(), cycBarrier)
              .shouldPause(true));
    }
  }
  static {
    initInfoForExecutorThread();
    LOGGER.debug("AsyncLogger.ThreadNameStrategy={}", THREAD_NAME_STRATEGY);
    final int ringBufferSize = calculateRingBufferSize();

    final WaitStrategy waitStrategy = createWaitStrategy();
    disruptor =
        new Disruptor<>(
            RingBufferLogEvent.FACTORY, ringBufferSize, EXECUTOR, ProducerType.MULTI, waitStrategy);
    disruptor.handleExceptionsWith(getExceptionHandler());
    disruptor.handleEventsWith(new RingBufferLogEventHandler());

    LOGGER.debug(
        "Starting AsyncLogger disruptor with ringbuffer size {}...",
        disruptor.getRingBuffer().getBufferSize());
    disruptor.start();
  }
 @SuppressWarnings("unchecked")
 private RingBuffer<RecordsHolder> getRingBuffer(Object member) {
   Disruptor<RecordsHolder> disruptor = this.disruptorMap.get(member);
   if (disruptor == null) {
     synchronized (this) {
       disruptor = this.disruptorMap.get(member);
       if (disruptor == null) {
         disruptor =
             new Disruptor<>(
                 new RecordsEventFactory(), REMOTE_INDEX_COMMUNICATOR_BUFFER_SIZE, this.executor);
         disruptor.handleEventsWith(new RecordsEventHandler(member));
         this.disruptorMap.put(member, disruptor);
         disruptor.start();
       }
     }
   }
   return disruptor.getRingBuffer();
 }
 private void enqueueLogMessageInfo(Info info) {
   // LOG4J2-639: catch NPE if disruptor field was set to null after our check above
   try {
     // Note: do NOT use the temp variable above!
     // That could result in adding a log event to the disruptor after it was shut down,
     // which could cause the publishEvent method to hang and never return.
     disruptor.publishEvent(info.translator);
   } catch (final NullPointerException npe) {
     LOGGER.fatal("Ignoring log event after log4j was shut down.");
   }
 }
  @Override
  public void stop() {

    // close all, tcp, udp, multicast

    synchronized (this) {
      thread.stop();

      // events shouldn't hit the disruptor any more
      disrupt.shutdown();
    }
  }
  @SuppressWarnings("unchecked")
  public void registerConsumer(ExchangeEventConsumer consumer)
      throws IllegalStateException, NullPointerException {
    if (disruptor == null) {
      throw new IllegalStateException("disruptor == null, call init");
    }

    if (consumer == null) {
      throw new NullPointerException("consumer == null");
    }

    disruptor.handleEventsWith(consumer);
  }
  public void registerProducer(ExchangeEventProducer<T> producer)
      throws IllegalStateException, NullPointerException {
    if (disruptor == null) {
      throw new IllegalStateException("disruptor == null, call init");
    }

    if (producer == null) {
      throw new NullPointerException("consumer == null");
    }

    producer.setRingBuffer(disruptor.getRingBuffer());

    producers.add(producer);
  }
  public static void stop() {
    final Disruptor<RingBufferLogEvent> temp = disruptor;

    // Must guarantee that publishing to the RingBuffer has stopped
    // before we call disruptor.shutdown()
    disruptor = null; // client code fails with NPE if log after stop = OK
    if (temp == null) {
      return; // stop() has already been called
    }

    // Calling Disruptor.shutdown() will wait until all enqueued events are fully processed,
    // but this waiting happens in a busy-spin. To avoid (postpone) wasting CPU,
    // we sleep in short chunks, up to 10 seconds, waiting for the ringbuffer to drain.
    for (int i = 0; hasBacklog(temp) && i < MAX_DRAIN_ATTEMPTS_BEFORE_SHUTDOWN; i++) {
      try {
        Thread.sleep(SLEEP_MILLIS_BETWEEN_DRAIN_ATTEMPTS); // give up the CPU for a while
      } catch (final InterruptedException e) { // ignored
      }
    }
    temp.shutdown(); // busy-spins until all events currently in the disruptor have been processed
    EXECUTOR.shutdown(); // finally, kill the processor thread
    Info.THREADLOCAL.remove(); // LOG4J2-323
  }
  /** {@inheritDoc} */
  @Override
  @SuppressWarnings("unchecked")
  public <T> T createRingBufferProxy(
      final Class<T> proxyInterface,
      final Disruptor<ProxyMethodInvocation> disruptor,
      final OverflowStrategy overflowStrategy,
      final T... implementations) {
    validator.validateAll(disruptor, proxyInterface);

    if (implementations.length < 1) {
      throw new IllegalArgumentException("Must have at least one implementation");
    } else if (implementations.length == 1) {
      return createRingBufferProxy(proxyInterface, disruptor, overflowStrategy, implementations[0]);
    }

    final EventHandler<ProxyMethodInvocation>[] handlers = new EventHandler[implementations.length];
    for (int i = 0; i < implementations.length; i++) {
      handlers[i] = createMultipleImplementationHandlerChain(implementations[i]);
      disruptor.handleEventsWith(handlers[i]);
    }
    disruptor.after(handlers).then(new ResetHandler());

    final ArgumentHolderGenerator argumentHolderGenerator = new ArgumentHolderGenerator(classPool);
    argumentHolderGenerator.createArgumentHolderClass(proxyInterface);

    prefillArgumentHolderObjects(disruptor.getRingBuffer(), argumentHolderGenerator);

    final Map<Method, Invoker> methodToInvokerMap =
        createMethodToInvokerMap(proxyInterface, argumentHolderGenerator);

    return generateProxy(
        proxyInterface,
        disruptor.getRingBuffer(),
        methodToInvokerMap,
        overflowStrategy,
        argumentHolderGenerator);
  }
  public void shutdownGracefully() throws IllegalStateException {
    if (disruptor == null) {
      throw new IllegalStateException("disruptor == null, call init");
    }

    for (ExchangeEventProducer<T> producer : producers) {
      producer.stop();
    }

    try {
      disruptor.shutdown(shutdownTimeout, TimeUnit.MINUTES);
    } catch (TimeoutException ex) {
      LOG.error(ex.getMessage());
    }
  }
 /**
  * LOG4J2-471: prevent deadlock when RingBuffer is full and object being logged calls Logger.log()
  * from its toString() method
  *
  * @param info threadlocal information - used to determine if the current thread is the background
  *     appender thread
  * @param theDisruptor used to check if the buffer is full
  * @param fqcn fully qualified caller name
  * @param level log level
  * @param marker optional marker
  * @param message log message
  * @param thrown optional exception
  * @return {@code true} if the event has been logged in the current thread, {@code false} if it
  *     should be passed to the background thread
  */
 private boolean logMessageInCurrentThread(
     Info info,
     final Disruptor<RingBufferLogEvent> theDisruptor,
     final String fqcn,
     final Level level,
     final Marker marker,
     final Message message,
     final Throwable thrown) {
   if (info.isAppenderThread && theDisruptor.getRingBuffer().remainingCapacity() == 0) {
     // bypass RingBuffer and invoke Appender directly
     final ReliabilityStrategy strategy = privateConfig.loggerConfig.getReliabilityStrategy();
     strategy.log(this, getName(), fqcn, marker, level, message, thrown);
     return true;
   }
   return false;
 }
 @Override
 public void stop() throws Exception {
   initProcess();
   disruptor.shutdown();
 }
 /** Returns {@code true} if the specified disruptor still has unprocessed events. */
 private static boolean hasBacklog(final Disruptor<?> theDisruptor) {
   final RingBuffer<?> ringBuffer = theDisruptor.getRingBuffer();
   return !ringBuffer.hasAvailableCapacity(ringBuffer.getBufferSize());
 }
 /**
  * Creates and returns a new {@code RingBufferAdmin} that instruments the ringbuffer of the {@code
  * AsyncLogger}.
  *
  * @param contextName name of the global {@code AsyncLoggerContext}
  * @return a new {@code RingBufferAdmin} that instruments the ringbuffer
  */
 public static RingBufferAdmin createRingBufferAdmin(final String contextName) {
   return RingBufferAdmin.forAsyncLogger(disruptor.getRingBuffer(), contextName);
 }
 @Override
 public void start() throws Exception {
   initProcess();
   disruptor.start();
 }
 public void close() {
   for (Disruptor<RecordsHolder> disruptor : this.disruptorMap.values()) {
     disruptor.shutdown();
   }
 }