Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disruptor helloworld example

I want to learn the Disruptor framework. Who can give me a helloworld example which can run in the main method with Java program language?

like image 693
Felix Avatar asked Mar 22 '12 16:03

Felix


2 Answers

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

like image 90
Trevor Bernard Avatar answered Sep 23 '22 19:09

Trevor Bernard


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

like image 31
Rohit Sachan Avatar answered Sep 22 '22 19:09

Rohit Sachan