Java Disruptor helloworld 示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9826512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:12:55  来源:igfitidea点击:

Disruptor helloworld example

javadisruptor-pattern

提问by Felix

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?

我想学习Disruptor 框架。谁能给我一个helloworld的例子,它可以用Java程序语言在main方法中运行?

采纳答案by 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.

这是一个简单的、可运行的示例,说明如何使用 Disruptor 库。示例是使用 Disruptor 库的 2.10.4 版编写的。

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

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

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

我也在这个线程上交叉发布:LMAX Disruptor 的最简单和实际的示例代码

回答by Rohit Sachan

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.

这里还有一个来自我的身边。我使用开源 Lmax 库尝试了一个破坏者示例。
我认为使用 lmax 干扰器(不是干扰器的内部)背后的想法是创建消息调度程序并像消费者一样注册事件监听器。

You Create a Disruptor, with specifying the message type.

您创建一个 Disruptor,并指定消息类型。

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 传递给您的生产者

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

Full code can be found here Github link

完整代码可以在这里找到 Github 链接

回答by Harry

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

我建议您查看 LMAX 代码LMAX 源代码测试目录中的测试目录 。在我看来,它是您可以使用 LMAX 做的所有事情的最佳来源。对于简单的例子,请看下面的链接Simple Example

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

我还建议您查看DSL 示例。