Java Code Examples for com.lmax.disruptor.dsl.Disruptor#getRingBuffer()
The following examples show how to use
com.lmax.disruptor.dsl.Disruptor#getRingBuffer() .
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: DisruptorLogAppenderBase.java From High-concurrent-server with Apache License 2.0 | 7 votes |
@SuppressWarnings("unchecked") @Override public void start() { if (appenderCount == 0) { addError("No attached appenders found."); return; } if (queueSize < 1) { addError("Invalid queue size [" + queueSize + "]"); return; } addInfo("环形缓冲区的大小: " + queueSize); Executor executor = Executors.newCachedThreadPool(); Disruptor<LogValueEvent> disruptor = new Disruptor<LogValueEvent>( LogValueEvent.EVENT_FACTORY, queueSize, executor, ProducerType.MULTI, new SleepingWaitStrategy()); disruptor.handleEventsWith(new LogDisruptorEventHandle()); disruptor.start(); ringBuffer = disruptor.getRingBuffer(); super.start(); }
Example 2
Source File: DisruptorUtil.java From nuls with MIT License | 6 votes |
public void offer(String name, Object obj) { Disruptor<DisruptorData> disruptor = DISRUPTOR_MAP.get(name); AssertUtil.canNotEmpty(disruptor, "the disruptor is not exist!name:" + name); RingBuffer<DisruptorData> ringBuffer = disruptor.getRingBuffer(); // locker.lock(); try { //请求下一个事件序号; long sequence = ringBuffer.next(); try { //获取该序号对应的事件对象; DisruptorData event = ringBuffer.get(sequence); event.setData(obj); } catch (Exception e) { Log.error(e); } finally { //发布事件; ringBuffer.publish(sequence); } } finally { // locker.unlock(); } }
Example 3
Source File: DisruptorProviderManage.java From hmily with Apache License 2.0 | 6 votes |
/** * start disruptor. */ @SuppressWarnings("unchecked") public void startup() { Disruptor<DataEvent<T>> disruptor = new Disruptor<>(new DisruptorEventFactory<>(), size, HmilyThreadFactory.create("disruptor_consumer_" + consumerFactory.fixName(), false), ProducerType.MULTI, new BlockingWaitStrategy()); DisruptorConsumer<T>[] consumers = new DisruptorConsumer[consumerSize]; for (int i = 0; i < consumerSize; i++) { consumers[i] = new DisruptorConsumer<>(consumerFactory); } disruptor.handleEventsWithWorkerPool(consumers); disruptor.setDefaultExceptionHandler(new IgnoreExceptionHandler()); disruptor.start(); RingBuffer<DataEvent<T>> ringBuffer = disruptor.getRingBuffer(); provider = new DisruptorProvider<>(ringBuffer); }
Example 4
Source File: DisruptorExample.java From pragmatic-java-engineer with GNU General Public License v3.0 | 6 votes |
public void test() throws Exception { TestEventFactory factory = new TestEventFactory(); ThreadFactory threadFactory = Executors.defaultThreadFactory(); Disruptor<TestEvent> disruptor = new Disruptor<>(factory, 1024, threadFactory); disruptor.handleEventsWith(new TestEventHandler()); disruptor.start(); RingBuffer<TestEvent> ringBuffer = disruptor.getRingBuffer(); TestEventProducerWithTranslator producer = new TestEventProducerWithTranslator(ringBuffer); ByteBuffer bb = ByteBuffer.allocate(8); for (long l = 0; true; l++) { bb.putLong(0, l); producer.onData(bb); Thread.sleep(1000); } }
Example 5
Source File: LongEventMain.java From elastic-rabbitmq with MIT License | 6 votes |
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); } }
Example 6
Source File: Source.java From cep with GNU Affero General Public License v3.0 | 6 votes |
/** * 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(); }
Example 7
Source File: DisruptorLogAppenderBase.java From NettyFileTransfer with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") @Override public void start() { if (appenderCount == 0) { addError("No attached appenders found."); return; } if (queueSize < 1) { addError("Invalid queue size [" + queueSize + "]"); return; } addInfo("环形缓冲区的大小: " + queueSize); Executor executor = Executors.newCachedThreadPool(); Disruptor<LogValueEvent> disruptor = new Disruptor<LogValueEvent>( LogValueEvent.EVENT_FACTORY, queueSize, executor, ProducerType.MULTI, new SleepingWaitStrategy()); disruptor.handleEventsWith(new LogDisruptorEventHandle()); disruptor.start(); ringBuffer = disruptor.getRingBuffer(); super.start(); }
Example 8
Source File: DisruptorConfig.java From match-trade with Apache License 2.0 | 5 votes |
@Bean public RingBuffer<MatchOrder> ringBuffer() { EventFactory<MatchOrder> factory = new OrderFactory(); int ringBufferSize = 1024 * 1024; ThreadFactory disruptorThreadPool = new ThreadFactoryBuilder().setNameFormat("DisruptorThreadPool").build(); Disruptor<MatchOrder> disruptor = new Disruptor<MatchOrder>(factory, ringBufferSize, disruptorThreadPool, ProducerType.MULTI, new YieldingWaitStrategy()); disruptor.setDefaultExceptionHandler(new MyHandlerException());// Disruptor异常统计 // 单线处理撮合, 并行处理盘口和订单薄 disruptor.handleEventsWithWorkerPool(new MatchHandler(),new MatchHandler()).then(new InputDepthHandler(),new OutDepthHandler()); disruptor.start(); return disruptor.getRingBuffer(); }
Example 9
Source File: Main.java From java-concurrent-programming with MIT License | 5 votes |
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 10
Source File: ItemAmountUpdateProcessorConfiguration.java From artemis-disruptor-miaosha with Apache License 2.0 | 5 votes |
@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; }
Example 11
Source File: DisruptorJmsMessageSenderFactory.java From artemis-disruptor-miaosha with Apache License 2.0 | 5 votes |
/** * 得到返回的结果后, 必须执行 {@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; }
Example 12
Source File: DisruptorEventQueue.java From opencensus-java with Apache License 2.0 | 5 votes |
private static DisruptorEventQueue create() { // Create new Disruptor for processing. Note that Disruptor creates a single thread per // consumer (see https://github.com/LMAX-Exchange/disruptor/issues/121 for details); // this ensures that the event handler can take unsynchronized actions whenever possible. Disruptor<DisruptorEvent> disruptor = new Disruptor<>( DisruptorEventFactory.INSTANCE, DISRUPTOR_BUFFER_SIZE, new DaemonThreadFactory("OpenCensus.Disruptor"), ProducerType.MULTI, new SleepingWaitStrategy(0, 1000 * 1000)); disruptor.handleEventsWith(new DisruptorEventHandler[] {DisruptorEventHandler.INSTANCE}); disruptor.start(); final RingBuffer<DisruptorEvent> ringBuffer = disruptor.getRingBuffer(); DisruptorEnqueuer enqueuer = new DisruptorEnqueuer() { @Override public void enqueue(Entry entry) { long sequence = ringBuffer.next(); try { DisruptorEvent event = ringBuffer.get(sequence); event.setEntry(entry); } finally { ringBuffer.publish(sequence); } } }; return new DisruptorEventQueue(disruptor, enqueuer); }
Example 13
Source File: OrderInsertProcessorConfiguration.java From artemis-disruptor-miaosha with Apache License 2.0 | 4 votes |
@Bean public OrderInsertCommandProcessor courseTakeInsertCmdProcessor() { LOGGER.info("Configure OrderInsertCommandProcessor"); CommandEventProducer<OrderInsertCommand>[] commandEventProducerList = new CommandEventProducer[conf.getNum()]; for (int i = 0; i < conf.getNum(); i++) { OrderInsertCommandBuffer cmdBuffer = new OrderInsertCommandBuffer(conf.getSqlBufferSize()); OrderInsertCommandExecutor cmdExecutor = new OrderInsertCommandExecutor(jdbcTemplate); Disruptor<CommandEvent<OrderInsertCommand>> 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<OrderInsertCommand>_DisruptorLifeCycleContainer_" + i, new DisruptorLifeCycleContainer("CommandEvent<OrderInsertCommand>_Disruptor_" + i, disruptor, StartupOrderConstants.DISRUPTOR_ORDER_INSERT)); } OrderInsertCommandProcessor cmdProcessor = new OrderInsertCommandProcessor(commandEventProducerList); commandDispatcher.registerCommandProcessor(cmdProcessor); return cmdProcessor; }
Example 14
Source File: AppMain.java From perf-workshop with Apache License 2.0 | 4 votes |
public static void main(final String[] args) throws Exception { final CommandLineArgs commandLineArgs = new CommandLineArgs(); new JCommander(commandLineArgs).parse(args); final Disruptor<Packet> packetDisruptor = new Disruptor<>(new Packet.Factory(commandLineArgs.getRecordLength()), commandLineArgs.getBufferSize(), newCachedThreadPool(DAEMON_THREAD_FACTORY), ProducerType.SINGLE, new SpinLoopHintBusySpinWaitStrategy()); final Overrides overrides = new Overrides(commandLineArgs); overrides.init(); final Journaller journaller = new Journaller(SYSTEM_NANO_TIMER, commandLineArgs, overrides.enableJournaller()); journaller.init(); final Histogram[] messageTransitTimeHistograms = new Histogram[commandLineArgs.getNumberOfIterations()]; setAll(messageTransitTimeHistograms, HISTOGRAMS::createHistogramForArray); final Histogram[] interMessageTimeHistograms = new Histogram[commandLineArgs.getNumberOfIterations()]; setAll(interMessageTimeHistograms, HISTOGRAMS::createHistogramForArray); packetDisruptor.handleEventsWith( runOnCpus(wrap(new Accumulator(messageTransitTimeHistograms, interMessageTimeHistograms, SYSTEM_NANO_TIMER, commandLineArgs)::process), "Accumulator", overrides.getAccumulatorThreadAffinity()), runOnCpus(wrap(journaller::process), "Journaller", overrides.getJournallerThreadAffinity())); packetDisruptor.start(); final InputReader inputReader = new InputReader(packetDisruptor.getRingBuffer(), SYSTEM_NANO_TIMER, commandLineArgs); if(commandLineArgs.runSpinners()) { System.out.println("Starting spinner threads to perturb the system"); Spinners.SPINNERS.start(); } System.out.println("Starting replay at " + new Date()); final Thread thread = DAEMON_THREAD_FACTORY.newThread(THREADS.runOnCpu(inputReader::processFiles, overrides.getProducerThreadAffinity())); thread.start(); try { thread.join(); System.out.println("Finished replay at " + new Date()); packetDisruptor.shutdown(1, TimeUnit.MINUTES); } catch (TimeoutException e) { throw new RuntimeException("Consumers did not process remaining events within timeout", e); } finally { Spinners.SPINNERS.stop(); packetDisruptor.halt(); } System.out.println("Pausing for 10 seconds..."); THREADS.sleep(10L, TimeUnit.SECONDS); }
Example 15
Source File: AsyncLoggerConfigDisruptor.java From logging-log4j2 with Apache License 2.0 | 4 votes |
/** * 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()); }
Example 16
Source File: AsyncLoggerDisruptor.java From logging-log4j2 with Apache License 2.0 | 4 votes |
/** * 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()); }