com.lmax.disruptor.WorkHandler Java Examples

The following examples show how to use com.lmax.disruptor.WorkHandler. 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: DisruptorWorkerPoolExecutionStrategy.java    From gridgo with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
public DisruptorWorkerPoolExecutionStrategy(ProducerType type, final int bufferSize, final int numWorkers,
        final WaitStrategy waitStrategy, final ThreadFactory threadFactory) {
    this.disruptor = new Disruptor<>(ExecutionContextEvent::new, bufferSize, threadFactory, type, waitStrategy);
    WorkHandler<ExecutionContextEvent<T, H>>[] workers = new WorkHandler[numWorkers];
    for (int i = 0; i < numWorkers; i++) {
        workers[i] = event -> {
            event.getContext().execute();
        };
    }
    this.disruptor.handleEventsWithWorkerPool(workers);
}
 
Example #2
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 #3
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);
}