当前位置: 首页>>代码示例>>Java>>正文


Java Disruptor.getRingBuffer方法代码示例

本文整理汇总了Java中com.lmax.disruptor.dsl.Disruptor.getRingBuffer方法的典型用法代码示例。如果您正苦于以下问题:Java Disruptor.getRingBuffer方法的具体用法?Java Disruptor.getRingBuffer怎么用?Java Disruptor.getRingBuffer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.lmax.disruptor.dsl.Disruptor的用法示例。


在下文中一共展示了Disruptor.getRingBuffer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: offer

import com.lmax.disruptor.dsl.Disruptor; //导入方法依赖的package包/类
/**
 * add the data obj to the disruptor named the field name
 *
 * @param name
 * @param obj
 */
public void offer(String name, Object obj) {
    Disruptor<DisruptorEvent> disruptor = DISRUPTOR_MAP.get(name);
    AssertUtil.canNotEmpty(disruptor, "the disruptor is not exist!name:" + name);
    RingBuffer<DisruptorEvent> ringBuffer = disruptor.getRingBuffer();
    //请求下一个事件序号;
    long sequence = ringBuffer.next();
    try {
        //获取该序号对应的事件对象;
        DisruptorEvent event = ringBuffer.get(sequence);
        event.setData(obj);
    } finally {
        //发布事件;
        ringBuffer.publish(sequence);
    }
}
 
开发者ID:nuls-io,项目名称:nuls,代码行数:22,代码来源:DisruptorUtil.java

示例2: main

import com.lmax.disruptor.dsl.Disruptor; //导入方法依赖的package包/类
public static void main(String[] args) throws InterruptedException
{
    Disruptor<ObjectBox> disruptor = new Disruptor<ObjectBox>(
        ObjectBox.FACTORY, RING_SIZE, Executors.newCachedThreadPool(), ProducerType.MULTI,
        new BlockingWaitStrategy());
    disruptor.handleEventsWith(new Consumer()).then(new Consumer());
    final RingBuffer<ObjectBox> ringBuffer = disruptor.getRingBuffer();
    Publisher p = new Publisher();
    IMessage message = new IMessage();
    ITransportable transportable = new ITransportable();
    String streamName = "com.lmax.wibble";
    System.out.println("publishing " + RING_SIZE + " messages");
    for (int i = 0; i < RING_SIZE; i++)
    {
        ringBuffer.publishEvent(p, message, transportable, streamName);
        Thread.sleep(10);
    }
    System.out.println("start disruptor");
    disruptor.start();
    System.out.println("continue publishing");
    while (true)
    {
        ringBuffer.publishEvent(p, message, transportable, streamName);
        Thread.sleep(10);
    }
}
 
开发者ID:winwill2012,项目名称:disruptor-code-analysis,代码行数:27,代码来源:MultiProducerWithTranslator.java

示例3: Source

import com.lmax.disruptor.dsl.Disruptor; //导入方法依赖的package包/类
/**
 * Create a new source.
 * <p>This method will prepare the instance with some needed variables
 * in order to be started later with the start method (implemented by children).
 *
 * @param parsersManager Instance of ParserManager that will serve parsers to this source instance
 * @param eventHandler Instance of EventHandler that will receive the events generated by this source instance
 * @param properties Map of properties associated with this source
 */

public Source(ParsersManager parsersManager, EventHandler eventHandler, Map<String, Object> properties) {
    // Save the references for later use
    this.parsersManager = parsersManager;
    this.properties = properties;

    // Create the ring buffer for this topic and start it
    Disruptor<MapEvent> disruptor = new Disruptor<>(new MapEventFactory(), ConfigData.getRingBufferSize(), Executors.newCachedThreadPool());
    disruptor.handleEventsWith(eventHandler);
    disruptor.start();

    // Create the event producer that will receive the events produced by
    // this source instance
    eventProducer = new EventProducer(disruptor.getRingBuffer());
    prepare();
}
 
开发者ID:redBorder,项目名称:cep,代码行数:26,代码来源:Source.java

示例4: PersonHelper

import com.lmax.disruptor.dsl.Disruptor; //导入方法依赖的package包/类
public PersonHelper() {
    //参数1 事件
    //参数2 单线程使用
    //参数3 等待策略
    EventFactory<PersonEvent> eventFactory = PersonEvent.EVENT_FACTORY;
    ExecutorService executor = Executors.newSingleThreadExecutor();
    int ringBufferSize = 4; // RingBuffer 大小,必须是 2 的 N 次方;

    Disruptor<PersonEvent> disruptor = new Disruptor<>(eventFactory,
            ringBufferSize, executor, ProducerType.SINGLE,
            new YieldingWaitStrategy());

    ringBuffer = disruptor.getRingBuffer();
    //获取生产者的位置信息
    sequenceBarrier = ringBuffer.newBarrier();
    //消费者
    handler = new PersonEventHandler();
    //事件处理器,监控指定ringBuffer,有数据时通知指定handler进行处理
    batchEventProcessor = new BatchEventProcessor<>(ringBuffer, sequenceBarrier, handler);
    //传入所有消费者线程的序号
//        ringBuffer.setGatingSequences(batchEventProcessor.getSequence());

  }
 
开发者ID:whyDK37,项目名称:pinenut,代码行数:24,代码来源:PersonHelper.java

示例5: post

import com.lmax.disruptor.dsl.Disruptor; //导入方法依赖的package包/类
public void post(Object event) {
    Class<?> eventClass = event.getClass();
    Disruptor<BaseEvent> eventDisruptor = disruptorPool.get(eventClass);
    if(eventDisruptor == null) {
        List<RegistedEventHandler> registedEventHandlers = handlesMap.get(eventClass);
        if(registedEventHandlers == null || registedEventHandlers.size() == 0) {
            throw new RuntimeException("The " + eventClass.getSimpleName() + " event dosen't regist any eventHandler.");
        }
        eventDisruptor = createDisruptor(eventClass, registedEventHandlers);
        disruptorPool.put(eventClass, eventDisruptor);
    }
    // check whether has event Repository definition.
    if(eventRepository != null) {
    	eventRepository.save(event);
    }
    RingBuffer<BaseEvent> ringBuffer = eventDisruptor.getRingBuffer();
    BaseEventProducer producer = new BaseEventProducer(ringBuffer);
    producer.onData(event);
}
 
开发者ID:Coralma,项目名称:smart-cqrs,代码行数:20,代码来源:EventBus.java

示例6: stop

import com.lmax.disruptor.dsl.Disruptor; //导入方法依赖的package包/类
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
    temp.shutdown();

    // wait up to 10 seconds for the ringbuffer to drain
    final RingBuffer<RingBufferLogEvent> ringBuffer = temp.getRingBuffer();
    for (int i = 0; i < MAX_DRAIN_ATTEMPTS_BEFORE_SHUTDOWN; i++) {
        if (ringBuffer.hasAvailableCapacity(ringBuffer.getBufferSize())) {
            break;
        }
        try {
            // give ringbuffer some time to drain...
            Thread.sleep(HALF_A_SECOND);
        } catch (final InterruptedException e) {
            // ignored
        }
    }
    executor.shutdown(); // finally, kill the processor thread
}
 
开发者ID:OuZhencong,项目名称:log4j2,代码行数:24,代码来源:AsyncLogger.java

示例7: lessonStdCountUpdateCmdProcessor

import com.lmax.disruptor.dsl.Disruptor; //导入方法依赖的package包/类
@Bean
public ItemAmountUpdateCommandProcessor lessonStdCountUpdateCmdProcessor() {

  CommandEventProducer<ItemAmountUpdateCommand>[] commandEventProducerList = new CommandEventProducer[conf.getNum()];

  for (int i = 0; i < conf.getNum(); i++) {

    ItemAmountUpdateCommandBuffer cmdBuffer = new ItemAmountUpdateCommandBuffer(conf.getSqlBufferSize());
    ItemAmountUpdateCommandExecutor cmdExecutor = new ItemAmountUpdateCommandExecutor(jdbcTemplate);

    Disruptor<CommandEvent<ItemAmountUpdateCommand>> disruptor = new Disruptor<>(
        new CommandEventFactory(),
        conf.getQueueSize(),
        Executors.defaultThreadFactory());

    disruptor
        .handleEventsWith(new CommandEventDbHandler(cmdBuffer, cmdExecutor))
        .then(new CommandEventGcHandler())
    ;
    // disruptor 的异常处理是这样的,
    // 不论这种形式 A->B, 还是这种形式 A,B->C,D, 只有抛出异常的那个handler会中断执行
    disruptor.setDefaultExceptionHandler(new CommandEventExceptionHandler());

    commandEventProducerList[i] = new CommandEventProducer<>(disruptor.getRingBuffer());

    BeanRegisterUtils.registerSingleton(
        applicationContext,
        "CommandEvent<ItemAmountUpdateCommand>_DisruptorLifeCycleContainer_" + i,
        new DisruptorLifeCycleContainer("CommandEvent<ItemAmountUpdateCommand>_Disruptor_" + i, disruptor,
            StartupOrderConstants.DISRUPTOR_ITEM_UPDATE));

  }

  ItemAmountUpdateCommandProcessor cmdProcessor = new ItemAmountUpdateCommandProcessor(commandEventProducerList);

  commandDispatcher.registerCommandProcessor(cmdProcessor);

  return cmdProcessor;

}
 
开发者ID:bighector,项目名称:-artemis-disruptor-miaosha,代码行数:41,代码来源:ItemAmountUpdateProcessorConfiguration.java

示例8: create

import com.lmax.disruptor.dsl.Disruptor; //导入方法依赖的package包/类
/**
 * 得到返回的结果后, 必须执行 {@link DisruptorJmsMessageSender#getDisruptor()}.start() 才可以使用
 *
 * @param session
 * @param messageProducer
 * @param dupMessageDetectStrategy
 * @param ringBufferSize           必须是2的次方
 * @return
 * @throws JMSException
 */
public static DisruptorJmsMessageSender create(
    Session session,
    MessageProducer messageProducer,
    DupMessageDetectStrategy dupMessageDetectStrategy,
    int ringBufferSize
) throws JMSException {

  Disruptor<PayloadEvent> disruptor = new Disruptor<>(
      PayloadEvent::new,
      ringBufferSize,
      Executors.defaultThreadFactory()
  );

  PayloadEventProducer payloadEventProducer = new PayloadEventProducer(disruptor.getRingBuffer());

  DisruptorJmsMessageSender messageSender = new DisruptorJmsMessageSender();
  messageSender.setSession(session);
  messageSender.setMessageProducer(messageProducer);
  messageSender.setPayloadEventProducer(payloadEventProducer);
  if (dupMessageDetectStrategy != null) {
    messageSender.setDupMessageDetectStrategy(dupMessageDetectStrategy);
  }

  disruptor.handleEventsWith(messageSender);

  messageSender.setDisruptor(disruptor);

  return messageSender;

}
 
开发者ID:bighector,项目名称:-artemis-disruptor-miaosha,代码行数:41,代码来源:DisruptorJmsMessageSenderFactory.java

示例9: loop

import com.lmax.disruptor.dsl.Disruptor; //导入方法依赖的package包/类
default void loop(Disruptor disruptor, PcapHandle handle) throws Exception {
    RingBuffer<PacketContainer> ringBuffer = disruptor.getRingBuffer();

    handle.loop(0, (RawPacketListener) packet -> {
        long sequence = ringBuffer.next();

        PacketContainer container = ringBuffer.get(sequence);
        container.clear();
        container.setDlt(handle.getDlt());
        container.setTimestamp(handle.getTimestamp());
        container.setRawPacket(packet);

        ringBuffer.publish(sequence);
    });
}
 
开发者ID:sip3io,项目名称:tapir,代码行数:16,代码来源:PcapStream.java

示例10: main

import com.lmax.disruptor.dsl.Disruptor; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
        Executor executor = Executors.newCachedThreadPool();

        LongEventFactory eventFactory = new LongEventFactory();
        int bufferSize = 1024;

//        Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(eventFactory, bufferSize, executor);
//        disruptor.handleEventsWith(new LongEventHandler());
//        disruptor.start();

        Disruptor<LongEvent> disruptor = new Disruptor<>(LongEvent::new, bufferSize, executor);
        disruptor.handleEventsWith((event, sequence, endOfBatch) -> {System.out.println("Event: " + event);
            System.out.println("CurrentThreadName:" + Thread.currentThread().getName());
        });
        disruptor.start();

        RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
        LongEventProducer producer = new LongEventProducer(ringBuffer);

        ByteBuffer bb = ByteBuffer.allocate(8);
        for (long l = 0; true; l++) {
            bb.putLong(0, l);
            ringBuffer.publishEvent((event, sequence, buffer) -> event.set(buffer.getLong(0)), bb);

           // producer.onData(bb);
            //Thread.sleep(1000);
        }
    }
 
开发者ID:compasses,项目名称:elastic-rabbitmq,代码行数:29,代码来源:LongEventMain.java

示例11: publishBufferedExchanges

import com.lmax.disruptor.dsl.Disruptor; //导入方法依赖的package包/类
private void publishBufferedExchanges(Disruptor<ExchangeEvent> newDisruptor) {
    //now empty out all buffered Exchange if we had any
    final List<Exchange> exchanges = new ArrayList<Exchange>(temporaryExchangeBuffer.size());
    while (!temporaryExchangeBuffer.isEmpty()) {
        exchanges.add(temporaryExchangeBuffer.remove());
    }
    RingBuffer<ExchangeEvent> ringBuffer = newDisruptor.getRingBuffer();
    //and offer them again to our new ringbuffer
    for (final Exchange exchange : exchanges) {
        publishExchangeOnRingBuffer(exchange, ringBuffer);
    }
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:13,代码来源:DisruptorReference.java

示例12: main

import com.lmax.disruptor.dsl.Disruptor; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception
{
    // Executor that will be used to construct new threads for consumers
    Executor executor = Executors.newCachedThreadPool();

    // Specify the size of the ring buffer, must be power of 2.
    int bufferSize = 1024;

    // Construct the Disruptor
    Disruptor<LongEvent> disruptor = new Disruptor<>(LongEvent::new, bufferSize, executor);

    // Connect the handler
    disruptor.handleEventsWith(DisruptorTests::handleEvent);

    EventHandlerGroup<LongEvent> g = disruptor.handleEventsWith(DisruptorTests::handleEvent);



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

    // Get the ring buffer from the Disruptor to be used for publishing.
    RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();

    //LongEventProducer producer = new LongEventProducer(ringBuffer);

    ByteBuffer bb = ByteBuffer.allocate(8);
    for (long l = 0; true; l++)
    {
        bb.putLong(0, l);
        ringBuffer.publishEvent(DisruptorTests::translate, bb);
        Thread.sleep(1000);
    }
}
 
开发者ID:kriskalish,项目名称:hologram,代码行数:35,代码来源:DisruptorTests.java

示例13: produceEvents

import com.lmax.disruptor.dsl.Disruptor; //导入方法依赖的package包/类
private static void produceEvents(Disruptor<ObjectEvent> disruptor) throws InterruptedException {
	RingBuffer<ObjectEvent> ringBuffer = disruptor.getRingBuffer();
	for (long l = 0; true; l++) {
		String obj = "Test-" + l;
		ringBuffer.publishEvent((event, sequence) -> event.setObject(obj));
		Thread.sleep(1000);
	}
}
 
开发者ID:smallnest,项目名称:DisruptorBootstrap,代码行数:9,代码来源:App.java

示例14: main

import com.lmax.disruptor.dsl.Disruptor; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception
{
    // Executor that will be used to construct new threads for consumers
    Executor executor = Executors.newCachedThreadPool();

    // The factory for the event
    LongEventFactory factory = new LongEventFactory();

    // Specify the size of the ring buffer, must be power of 2.
    int bufferSize = 1024;

    // Construct the Disruptor
    Disruptor<LongEvent> disruptor = new Disruptor<>(factory, bufferSize, executor);

    // Connect the handler
    disruptor.handleEventsWith(new LongEventHandler());

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

    // Get the ring buffer from the Disruptor to be used for publishing.
    RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();

    LongEventProducer producer = new LongEventProducer(ringBuffer);

    ByteBuffer bb = ByteBuffer.allocate(8);
    for (long l = 0; true; l++)
    {
        bb.putLong(0, l);
        producer.onData(bb);
        Thread.sleep(1000);
    }
}
 
开发者ID:aranhakki,项目名称:experimental-performance,代码行数:34,代码来源:LongEventMain.java

示例15: release

import com.lmax.disruptor.dsl.Disruptor; //导入方法依赖的package包/类
/**
 * Decreases the reference count. If the reference count reached zero, the
 * Disruptor and its associated thread are shut down and their references
 * set to {@code null}.
 */
synchronized static void release() {
    if (--count > 0) {
        LOGGER.trace("AsyncLoggerConfigHelper: not shutting down disruptor: ref count is {}.", count);
        return;
    }
    final Disruptor<Log4jEventWrapper> temp = disruptor;
    if (temp == null) {
        LOGGER.trace("AsyncLoggerConfigHelper: disruptor already shut down: ref count is {}.", count);
        return; // disruptor was already shut down by another thread
    }
    LOGGER.trace("AsyncLoggerConfigHelper: shutting down disruptor: ref count is {}.", count);

    // 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
    temp.shutdown();

    // wait up to 10 seconds for the ringbuffer to drain
    final RingBuffer<Log4jEventWrapper> ringBuffer = temp.getRingBuffer();
    for (int i = 0; i < MAX_DRAIN_ATTEMPTS_BEFORE_SHUTDOWN; i++) {
        if (ringBuffer.hasAvailableCapacity(ringBuffer.getBufferSize())) {
            break;
        }
        try {
            // give ringbuffer some time to drain...
            Thread.sleep(HALF_A_SECOND);
        } catch (final InterruptedException e) {
            // ignored
        }
    }
    executor.shutdown(); // finally, kill the processor thread
    executor = null; // release reference to allow GC
}
 
开发者ID:OuZhencong,项目名称:log4j2,代码行数:39,代码来源:AsyncLoggerConfigHelper.java


注:本文中的com.lmax.disruptor.dsl.Disruptor.getRingBuffer方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。