Java Code Examples for com.lmax.disruptor.dsl.ProducerType#MULTI

The following examples show how to use com.lmax.disruptor.dsl.ProducerType#MULTI . 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: FastEventServiceImpl.java    From redtorch with MIT License 6 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
	if ("BusySpinWaitStrategy".equals(waitStrategy)) {
		disruptor = new Disruptor<FastEvent>(new FastEventFactory(), 65536, DaemonThreadFactory.INSTANCE,
				ProducerType.MULTI, new BusySpinWaitStrategy());
	} else if ("SleepingWaitStrategy".equals(waitStrategy)) {
		disruptor = new Disruptor<FastEvent>(new FastEventFactory(), 65536, DaemonThreadFactory.INSTANCE,
				ProducerType.MULTI, new SleepingWaitStrategy());
	} else if ("BlockingWaitStrategy".equals(waitStrategy)) {
		disruptor = new Disruptor<FastEvent>(new FastEventFactory(), 65536, DaemonThreadFactory.INSTANCE,
				ProducerType.MULTI, new BlockingWaitStrategy());
	} else {
		disruptor = new Disruptor<FastEvent>(new FastEventFactory(), 65536, DaemonThreadFactory.INSTANCE,
				ProducerType.MULTI, new YieldingWaitStrategy());
	}
	ringBuffer = disruptor.start();
}
 
Example 2
Source File: TransactionResolver.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
public TransactionResolver(TxnSupplier txnSupplier, int numThreads, int bufferSize) {
     this.txnSupplier = txnSupplier;
     this.consumerThreads = MoreExecutors.namedThreadPool(numThreads, numThreads, "txn-resolve-%d", 60, true);
     this.consumerThreads.allowCoreThreadTimeOut(true);

     int bSize=1;
     while(bSize<bufferSize)
         bSize<<=1;

     disruptor = new Disruptor<>(new EventFactory<TxnResolveEvent>() {
@Override
public TxnResolveEvent newInstance() {
              return new TxnResolveEvent();
}
     },bSize,consumerThreads,
             ProducerType.MULTI,
             new BlockingWaitStrategy());
     disruptor.handleEventsWith(new ResolveEventHandler());
     ringBuffer = disruptor.getRingBuffer();
     disruptor.start();
 }
 
Example 3
Source File: AsyncReadResolver.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
public AsyncReadResolver(int maxThreads,int bufferSize,
                         TxnSupplier txnSupplier,
                         RollForwardStatus status,
                         TrafficControl trafficControl,
                         KeyedReadResolver synchronousResolver){
    this.txnSupplier=txnSupplier;
    this.trafficControl=trafficControl;
    this.status=status;
    this.synchronousResolver = synchronousResolver;
    consumerThreads=new ThreadPoolExecutor(maxThreads,maxThreads,
            60,TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(),
            new ThreadFactoryBuilder().setNameFormat("readResolver-%d").setDaemon(true).build());

    int bSize=1;
    while(bSize<bufferSize)
        bSize<<=1;
    disruptor=new Disruptor<>(new ResolveEventFactory(),bSize,consumerThreads,
            ProducerType.MULTI,
            new BlockingWaitStrategy()); //we want low latency here, but it might cost too much in CPU
    disruptor.handleEventsWith(new ResolveEventHandler());
    ringBuffer=disruptor.getRingBuffer();
}
 
Example 4
Source File: DbMessageStore.java    From ballerina-message-broker with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
DbMessageStore(MessageDao messageDao, int bufferSize, int maxDbBatchSize) {
    ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
            .setNameFormat("DisruptorMessageStoreThread-%d").build();

    disruptor = new Disruptor<>(DbOperation.getFactory(),
                                bufferSize, namedThreadFactory, ProducerType.MULTI, new
                                        SleepingBlockingWaitStrategy());

    disruptor.setDefaultExceptionHandler(new DbStoreExceptionHandler());

    disruptor.handleEventsWith(new DbEventMatcher(bufferSize))
             .then(new DbAccessHandler(messageDao, maxDbBatchSize))
             .then(new FinalEventHandler());
    disruptor.start();
    this.messageDao = messageDao;
}
 
Example 5
Source File: TxTransactionEventPublisher.java    From Raincat with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * disruptor start.
 *
 * @param bufferSize this is disruptor buffer size.
 * @param threads this is disruptor consumer thread size.
 */
private void start(final int bufferSize, final int threads) {
    disruptor = new Disruptor<>(new TxTransactionEventFactory(), bufferSize, r -> {
        return new Thread(null, r, "disruptor-thread-" + INDEX.getAndIncrement());
    }, ProducerType.MULTI, new BlockingWaitStrategy());

    final Executor executor = new ThreadPoolExecutor(threads, threads, 0, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<>(),
            TxTransactionThreadFactory.create("raincat-log-disruptor", false),
            new ThreadPoolExecutor.AbortPolicy());

    TxTransactionEventHandler[] consumers = new TxTransactionEventHandler[threads];
    for (int i = 0; i < threads; i++) {
        consumers[i] = new TxTransactionEventHandler(executor, txCompensationService);
    }
    disruptor.handleEventsWithWorkerPool(consumers);
    disruptor.setDefaultExceptionHandler(new IgnoreExceptionHandler());
    disruptor.start();
}
 
Example 6
Source File: Main.java    From java-concurrent-programming with MIT License 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    Executor executor = Executors.newCachedThreadPool();
    PCDataFactory factory = new PCDataFactory();

    int bufferSize = 1024;
    Disruptor<PCData> disruptor = new Disruptor<PCData>(factory,
            bufferSize,
            executor,
            ProducerType.MULTI,
            new BlockingWaitStrategy()
            );
    disruptor.handleEventsWithWorkerPool(
            new Consumer(),
            new Consumer(),
            new Consumer(),
            new Consumer()
    );
    disruptor.start();

    // 1个生产者 4个消费者
    RingBuffer<PCData> ringBuffer = disruptor.getRingBuffer();
    Producer producer = new Producer(ringBuffer);
    ByteBuffer bb = ByteBuffer.allocate(8);
    for (long l = 0; true; l++) {
        bb.putLong(0, l);
        producer.pushData(bb);
        Thread.sleep(100);
        System.out.println("add data "+l);
    }
}
 
Example 7
Source File: DisruptorBroker.java    From swarm with MIT License 5 votes vote down vote up
public void initialize() throws Exception {
  logger.info("Initializing...");
  logger.info("> parallelism={}", builder.parallelism);
  logger.info("> lowLatency={}", builder.lowLatency);
  logger.info("> bufferSize={}", builder.bufferSize);

  WaitStrategy waitStrategy =
      builder.isLowLatency() ? new BusySpinWaitStrategy() : new BlockingWaitStrategy();
  ProducerType producerType =
      builder.getProducerMode() == ProducerMode.SINGLE ? ProducerType.SINGLE : ProducerType.MULTI;
  EventFactory eventFactory = () -> new Event();

  disruptor =
      new Disruptor(
          eventFactory, builder.bufferSize, getThreadFactory(), producerType, waitStrategy);
  initializeRingBuffer();

  disruptor.handleEventsWithWorkerPool(getWorkersPool());
  // Start the Disruptor, starts all threads running
  disruptor.start();

  logger.info("Initialized");
}
 
Example 8
Source File: AsyncEventServiceImpl.java    From java-trader with Apache License 2.0 5 votes vote down vote up
@Override
public void init(BeansContainer beansContainer) throws Exception {
    //启动disruptor
    disruptor = new Disruptor<AsyncEvent>( new AsyncEventFactory()
        , ConfigUtil.getInt(ITEM_DISRUPTOR_RINGBUFFER_SIZE, 65536)
        , executorService
        , ProducerType.MULTI
        , ConcurrentUtil.createDisruptorWaitStrategy(ConfigUtil.getString(ITEM_DISRUPTOR_WAIT_STRATEGY))
        );
}
 
Example 9
Source File: TradletGroupEngine.java    From java-trader with Apache License 2.0 5 votes vote down vote up
@Override
public void init(BeansContainer beansContainer) {
    super.init(beansContainer);

    ExecutorService executorService = beansContainer.getBean(ExecutorService.class);

    //读取Group特有配置, 如果不存在, 读取通用配置
    String ringBufferSizeStr = ConfigUtil.getString(TradletServiceImpl.ITEM_TRADLETGROUP+"#"+group.getId()+TradletServiceImpl.ITEM_SUFFIX_DISRUPTOR_RINGBUFFER_SIZE);
    if ( StringUtil.isEmpty(ringBufferSizeStr)) {
        ringBufferSizeStr = ConfigUtil.getString(TradletServiceImpl.ITEM_GLOBAL_DISRUPTOR_RINGBUFFER_SIZE);
    }
    String disruptorWaitStrategy = ConfigUtil.getString(TradletServiceImpl.ITEM_TRADLETGROUP+"#"+group.getId()+TradletServiceImpl.ITEM_SUFFIX_DISRUPTOR_WAIT_STRATEGY);
    if ( StringUtil.isEmpty(disruptorWaitStrategy)) {
        disruptorWaitStrategy = ConfigUtil.getString(TradletServiceImpl.ITEM_GLOBAL_DISRUPTOR_WAIT_STRATEGY);
    }
    int ringBufferSize = 4096;
    if ( !StringUtil.isEmpty(ringBufferSizeStr)) {
        ringBufferSize = ConversionUtil.toInt(ringBufferSizeStr);
    }
    disruptor = new Disruptor<TradletEvent>( new TradletEventFactory()
        , ringBufferSize
        , executorService
        , ProducerType.MULTI
        , ConcurrentUtil.createDisruptorWaitStrategy(disruptorWaitStrategy)
        );
    disruptor.handleEventsWith(this);
    ringBuffer = disruptor.start();

}
 
Example 10
Source File: DataEndpointGroup.java    From product-microgateway with Apache License 2.0 5 votes vote down vote up
EventQueue(int queueSize) {
    eventQueuePool = Executors.newCachedThreadPool(
            new DataBridgeThreadFactory("EventQueue"));
    eventQueueDisruptor = new Disruptor<>(new WrappedEventFactory(), queueSize, eventQueuePool,
            ProducerType.MULTI, new BlockingWaitStrategy());
    eventQueueDisruptor.handleEventsWith(new EventQueueWorker());
    this.ringBuffer = eventQueueDisruptor.start();
}
 
Example 11
Source File: DisruptorConfigure.java    From youkefu with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "deprecation" })
@Bean(name="multiupdate")   
   public Disruptor<UserDataEvent> multiupdate() {   
   	Executor executor = Executors.newCachedThreadPool();
   	 MultiUpdateEventFactory factory = new MultiUpdateEventFactory();
   	 Disruptor<UserDataEvent> disruptor = new Disruptor<UserDataEvent>(factory, 1024, executor, ProducerType.MULTI , new BlockingWaitStrategy());
   	 disruptor.handleEventsWith(new MultiUpdateEventHandler());
   	 disruptor.setDefaultExceptionHandler(new UKeFuExceptionHandler());
   	 disruptor.start();
        return disruptor;   
   }
 
Example 12
Source File: LogEventPublisher.java    From fast-family-master with Apache License 2.0 5 votes vote down vote up
public void start() {
    disruptor = new Disruptor<LogEvent>(new LogEventFactory(), 1024, r -> {
        AtomicInteger integer = new AtomicInteger(1);
        return new Thread(null, r, "disruptor-thread-" + integer.getAndIncrement());
    }, ProducerType.MULTI, new BlockingWaitStrategy());
    disruptor.handleEventsWithWorkerPool(new LogEventHandler(logRepository));
    disruptor.setDefaultExceptionHandler(new IgnoreExceptionHandler());
    disruptor.start();
}
 
Example 13
Source File: WorkQueueDispatcher.java    From camunda-bpm-reactor with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public WorkQueueDispatcher(String name,
                           int poolSize,
                           int backlog,
                           final Consumer<Throwable> uncaughtExceptionHandler) {
  this(name, poolSize, backlog, uncaughtExceptionHandler, ProducerType.MULTI, new BlockingWaitStrategy());
}
 
Example 14
Source File: StreamJunction.java    From siddhi with Apache License 2.0 5 votes vote down vote up
/**
 * Create and start disruptor based on annotations given in the streamDefinition.
 */
public void startProcessing() {
    this.exceptionListener = siddhiAppContext.getRuntimeExceptionListener();
    if (!receivers.isEmpty() && async) {
        for (Constructor constructor : Disruptor.class.getConstructors()) {
            if (constructor.getParameterTypes().length == 5) {      // If new disruptor classes available
                ProducerType producerType = ProducerType.MULTI;
                disruptor = new Disruptor<EventExchangeHolder>(
                        new EventExchangeHolderFactory(streamDefinition.getAttributeList().size()),
                        bufferSize, executorService, producerType,
                        new BlockingWaitStrategy());
                disruptor.handleExceptionsWith(siddhiAppContext.getDisruptorExceptionHandler());
                break;
            }
        }
        if (disruptor == null) {
            disruptor = new Disruptor<EventExchangeHolder>(
                    new EventExchangeHolderFactory(streamDefinition.getAttributeList().size()),
                    bufferSize, executorService);
            disruptor.handleExceptionsWith(siddhiAppContext.getDisruptorExceptionHandler());
        }
        if (workers > 0) {
            for (int i = 0; i < workers; i++) {
                disruptor.handleEventsWith(new StreamHandler(receivers, batchSize, streamDefinition.getId(),
                        siddhiAppContext.getName(), faultStreamJunction, onErrorAction, exceptionListener));
            }
        } else {
            disruptor.handleEventsWith(new StreamHandler(receivers, batchSize, streamDefinition.getId(),
                    siddhiAppContext.getName(), faultStreamJunction, onErrorAction, exceptionListener));
        }
        ringBuffer = disruptor.start();
    } else {
        for (Receiver receiver : receivers) {
            if (receiver instanceof StreamCallback) {
                ((StreamCallback) receiver).startProcessing();
            }
        }
    }
}
 
Example 15
Source File: AsyncLoggerConfigDisruptor.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Increases the reference count and creates and starts a new Disruptor and associated thread if none currently
 * exists.
 *
 * @see #stop()
 */
@Override
public synchronized void start() {
    if (disruptor != null) {
        LOGGER.trace("AsyncLoggerConfigDisruptor not starting new disruptor for this configuration, "
                + "using existing object.");
        return;
    }
    LOGGER.trace("AsyncLoggerConfigDisruptor creating new disruptor for this configuration.");
    ringBufferSize = DisruptorUtil.calculateRingBufferSize("AsyncLoggerConfig.RingBufferSize");
    final WaitStrategy waitStrategy = DisruptorUtil.createWaitStrategy("AsyncLoggerConfig.WaitStrategy");

    final ThreadFactory threadFactory = new Log4jThreadFactory("AsyncLoggerConfig", true, Thread.NORM_PRIORITY) {
        @Override
        public Thread newThread(final Runnable r) {
            final Thread result = super.newThread(r);
            backgroundThreadId = result.getId();
            return result;
        }
    };
    asyncQueueFullPolicy = AsyncQueueFullPolicyFactory.create();

    translator = mutable ? MUTABLE_TRANSLATOR : TRANSLATOR;
    factory = mutable ? MUTABLE_FACTORY : FACTORY;
    disruptor = new Disruptor<>(factory, ringBufferSize, threadFactory, ProducerType.MULTI, waitStrategy);

    final ExceptionHandler<Log4jEventWrapper> errorHandler = DisruptorUtil.getAsyncLoggerConfigExceptionHandler();
    disruptor.setDefaultExceptionHandler(errorHandler);

    final Log4jEventWrapperHandler[] handlers = {new Log4jEventWrapperHandler()};
    disruptor.handleEventsWith(handlers);

    LOGGER.debug("Starting AsyncLoggerConfig disruptor for this configuration with ringbufferSize={}, "
            + "waitStrategy={}, exceptionHandler={}...", disruptor.getRingBuffer().getBufferSize(), waitStrategy
            .getClass().getSimpleName(), errorHandler);
    disruptor.start();
    super.start();
}
 
Example 16
Source File: FSHLog.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Create an edit log at the given <code>dir</code> location. You should never have to load an
 * existing log. If there is a log at startup, it should have already been processed and deleted
 * by the time the WAL object is started up.
 * @param fs filesystem handle
 * @param rootDir path to where logs and oldlogs
 * @param logDir dir where wals are stored
 * @param archiveDir dir where wals are archived
 * @param conf configuration to use
 * @param listeners Listeners on WAL events. Listeners passed here will be registered before we do
 *          anything else; e.g. the Constructor {@link #rollWriter()}.
 * @param failIfWALExists If true IOException will be thrown if files related to this wal already
 *          exist.
 * @param prefix should always be hostname and port in distributed env and it will be URL encoded
 *          before being used. If prefix is null, "wal" will be used
 * @param suffix will be url encoded. null is treated as empty. non-empty must start with
 *          {@link org.apache.hadoop.hbase.wal.AbstractFSWALProvider#WAL_FILE_NAME_DELIMITER}
 */
public FSHLog(final FileSystem fs, final Path rootDir, final String logDir,
    final String archiveDir, final Configuration conf, final List<WALActionsListener> listeners,
    final boolean failIfWALExists, final String prefix, final String suffix) throws IOException {
  super(fs, rootDir, logDir, archiveDir, conf, listeners, failIfWALExists, prefix, suffix);
  this.minTolerableReplication = conf.getInt("hbase.regionserver.hlog.tolerable.lowreplication",
    CommonFSUtils.getDefaultReplication(fs, this.walDir));
  this.lowReplicationRollLimit = conf.getInt("hbase.regionserver.hlog.lowreplication.rolllimit",
    5);
  this.closeErrorsTolerated = conf.getInt("hbase.regionserver.logroll.errors.tolerated", 2);

  // This is the 'writer' -- a single threaded executor. This single thread 'consumes' what is
  // put on the ring buffer.
  String hostingThreadName = Thread.currentThread().getName();
  // Using BlockingWaitStrategy. Stuff that is going on here takes so long it makes no sense
  // spinning as other strategies do.
  this.disruptor = new Disruptor<>(RingBufferTruck::new,
      getPreallocatedEventCount(),
      Threads.newDaemonThreadFactory(hostingThreadName + ".append"),
      ProducerType.MULTI, new BlockingWaitStrategy());
  // Advance the ring buffer sequence so that it starts from 1 instead of 0,
  // because SyncFuture.NOT_DONE = 0.
  this.disruptor.getRingBuffer().next();
  int maxHandlersCount = conf.getInt(HConstants.REGION_SERVER_HANDLER_COUNT, 200);
  this.ringBufferEventHandler = new RingBufferEventHandler(
      conf.getInt("hbase.regionserver.hlog.syncer.count", 5), maxHandlersCount);
  this.disruptor.setDefaultExceptionHandler(new RingBufferExceptionHandler());
  this.disruptor.handleEventsWith(new RingBufferEventHandler[] { this.ringBufferEventHandler });
  // Starting up threads in constructor is a no no; Interface should have an init call.
  this.disruptor.start();
}
 
Example 17
Source File: Dispatcher.java    From Electrons with MIT License 5 votes vote down vote up
/**
 * 初始化正常管道,任何情况下都会有
 *
 * @param pool 线程池
 */
private void initNormalChannel(ExecutorService pool) {
    Disruptor<ElectronsHolder> normalDis = new Disruptor<>(ElectronsHolder::new, conf.getCircuitLen(), pool, ProducerType.MULTI, new LiteBlockingWaitStrategy());
    WorkHandler[] workHandlers = new WorkHandler[conf.getCircuitNum()];
    Arrays.fill(workHandlers, (WorkHandler<ElectronsHolder>) electronsHolder -> electronsHolder.handle());
    normalDis.handleEventsWithWorkerPool(workHandlers);
    normalDis.handleExceptionsWith(new ElecExceptionHandler("Normal Disruptor"));

    //初始化channel
    Channel normalChannel = new NormalChannel(normalDis);
    //配置限流相关
    normalChannel.confLimitRate(conf.isLimitRate(), conf.getPermitsPerSecond(), conf.isWarmup(), conf.getWarmupPeriod(), conf.getWarmPeriodUnit());
    channelMap.put(NORMAL_CHANNEL_KEY, normalChannel);
}
 
Example 18
Source File: FileDataPublisher.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
EventQueue(int queueSize) {
    eventQueuePool = Executors.newSingleThreadExecutor(
            new DataBridgeThreadFactory("EventQueue"));
    eventQueueDisruptor = new Disruptor<>(new WrappedEventFactory(), queueSize, eventQueuePool,
            ProducerType.MULTI, new BlockingWaitStrategy());
    eventQueueDisruptor.handleEventsWith(new EventQueueWorker());
    this.ringBuffer = eventQueueDisruptor.start();
}
 
Example 19
Source File: DisruptorWorkerPoolExecutionStrategy.java    From gridgo with MIT License 4 votes vote down vote up
public DisruptorWorkerPoolExecutionStrategy(final int bufferSize, final int numWorkers,
        final WaitStrategy waitStrategy, final ThreadFactory threadFactory) {
    this(ProducerType.MULTI, bufferSize, numWorkers, waitStrategy, threadFactory);
}
 
Example 20
Source File: RingBufferDispatcher.java    From camunda-bpm-reactor with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new {@literal RingBufferDispatcher} with the given {@code name}. It will use a {@link RingBuffer} with
 * {@code bufferSize} slots, configured with a producer type of {@link ProducerType#MULTI MULTI}
 * and a {@link BlockingWaitStrategy blocking wait. A given @param uncaughtExceptionHandler} will catch anything not
 * handled e.g. by the owning {@code org.camunda.bpm.extension.reactor.projectreactor.EventBus} or {@code reactor.rx.Stream}.
 *
 * @param name                     The name of the dispatcher
 * @param bufferSize               The size to configure the ring buffer with
 * @param uncaughtExceptionHandler The last resort exception handler
 */
@SuppressWarnings({"unchecked"})
public RingBufferDispatcher(String name,
                            int bufferSize,
                            final Consumer<Throwable> uncaughtExceptionHandler) {
  this(name, bufferSize, uncaughtExceptionHandler, ProducerType.MULTI, new BlockingWaitStrategy());

}