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?
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.
来源:https://stackoverflow.com/questions/9826512/disruptor-helloworld-example