Disruptor helloworld example
Asked Answered
H

3

7

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?

Hogg answered 22/3, 2012 at 16:28 Comment(1)
See [The simplest and actual example code of LMAX Disruptor][1]. [1]: #9169102Menedez
R
13

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

Redtop answered 24/1, 2013 at 5:7 Comment(0)
B
4

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

Brandwein answered 14/1, 2014 at 6:38 Comment(0)
C
2

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.

Coquetry answered 11/1, 2016 at 7:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.