Disruptor helloworld example

随声附和 提交于 2019-12-04 05:13:54
Trevor Bernard

Here is a simple, runnable, example of how to use the Disruptor library. Example is written using version 2.10.4 of the Disruptor library.

https://github.com/trevorbernard/disruptor-examples

I've also cross posted on this thread: The simplest and actual example code of LMAX Disruptor

Here One more from my side. I tried one disruptor example using open source Lmax libraries.
I think idea behind the use of lmax disruptor (not the internals of disruptor) is to create message dispatcher and register event listener like consumer.

You Create a Disruptor, with specifying the message type.

Disruptor<Message> disruptor = new Disruptor<Message>(Message.EVENT_FACTORY, 2048, exec);`

You Create a Handler

 final EventHandler<Message> handler = new EventHandler<Message>() {
        // event will eventually be recycled by the Disruptor after it wraps
        public void onEvent(final Message event, final long sequence, final boolean endOfBatch) throws Exception {
            Integer value = event.getMsg();
            if(value % 10000 == 0){
                System.out.println("ValueEvent: " + value + " Sequence: " + sequence);
                double timeINnanos = (System.nanoTime()-startTime);
                double timetaken = (timeINnanos/1e9);
                System.out.println("Time Taken till now in sec " + timetaken );
            }
        }
    };

Register handler with disruptor

         disruptor.handleEventsWith(handler);

Start that disruptor and pass the returned RingBuffer to your producer

         RingBuffer<Message> ringBuffer = disruptor.start();
         Producer producer = new Producer(ringBuffer);

Full code can be found here Github link

I would suggest you to take a look at the test directory in the LMAX code LMAX Source Code Test Directory . In my opinion its the best source for all kind of things you can do with the LMAX. For the simple example, please take a look at the following link Simple Example

I would also suggest you to take a look at the DSL examples.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!