/** {@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);
  }
 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 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);
  }
  /** {@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);
  }
Ejemplo n.º 5
0
 /**
  * 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;
 }
  @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));
    }
  }
Ejemplo n.º 7
0
  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();
 }
Ejemplo n.º 9
0
 /**
  * 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);
 }
Ejemplo n.º 10
0
 /** 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());
 }