【发布时间】:2012-04-07 06:20:19
【问题描述】:
我想学习Disruptor framework。谁能给我一个可以用Java程序语言在main方法中运行的helloworld例子?
【问题讨论】:
-
参见【LMAX Disruptor 最简单实际的示例代码】[1]。 [1]:stackoverflow.com/questions/9168602/…
我想学习Disruptor framework。谁能给我一个可以用Java程序语言在main方法中运行的helloworld例子?
【问题讨论】:
这是一个简单、可运行的示例,说明如何使用 Disruptor 库。示例是使用 Disruptor 库的 2.10.4 版本编写的。
https://github.com/trevorbernard/disruptor-examples
我也发过这个帖子:The simplest and actual example code of LMAX Disruptor
【讨论】:
这里还有一个在我身边。我尝试了一个使用开源 Lmax 库的破坏者示例。
我认为使用 lmax 中断器(而不是中断器的内部)背后的想法是创建消息调度程序并像消费者一样注册事件侦听器。
您创建一个 Disruptor,并指定消息类型。
Disruptor<Message> disruptor = new Disruptor<Message>(Message.EVENT_FACTORY, 2048, exec);`
你创建一个处理程序
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 );
}
}
};
向中断器注册处理程序
disruptor.handleEventsWith(handler);
启动中断器并将返回的 RingBuffer 传递给您的生产者
RingBuffer<Message> ringBuffer = disruptor.start();
Producer producer = new Producer(ringBuffer);
完整的代码可以在这里找到 Github link
【讨论】:
我建议你看一下 LMAX 代码LMAX Source Code Test Directory 中的测试目录。在我看来,它是你可以用 LMAX 做的所有事情的最佳来源。简单的例子请看下面的链接Simple Example
我还建议你看看DSL examples.
【讨论】: