本文介绍了对于Java同步锁,disruptor onEvent处理程序似乎过快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我同时(一次接一个)向输出中断器发布2个或更多不同事件,并且中断器onEvent通常仅将最后一个要发布的事件发送到IO会话.基本上,事件数据将被覆盖.而我想在会话层上看到每个事件的副本.我曾尝试在锁上同步会话层,但是对于锁来说,发布者通常仍然太快(有时它可以工作,但大多数时候却不行).

I am publishing 2 or more different events to an output disruptor at the same time (one right after the other) and the disruptor onEvent is usually sending only the last to be published event to the IO session. Basically the event data is overwritten. Whereas I want to see a copy of each event on the session layer. I have tried synchronizing the session layer on a lock but the publisher is usually still too fast even for the lock (sometimes it is working but most of the time it's not).

以下是一些代码:

private final Object lock = new Object();

public void onEvent(final FixEvent event, final long sequence, final boolean endOfBatch)
    {
        synchronized(lock)
        {
             sendMessage(event.message);
        }
    }

这件事发生在别人身上吗?除了发布商的提速之外,还有其他解决方案吗?

Has this happened to anyone else? Is there another solution besides a speed bump in the publisher?

这是生产者代码:

public void onData(Message message, SessionID s)
{
    long sequence = ringBuffer.next();  // Grab the next sequence
    try
    {
        FixEvent event = ringBuffer.get(sequence);
        event.set(message, s);
    }
    finally
    {
        ringBuffer.publish(sequence);
    }
}

对于每个事件处理程序,

推荐答案

onEvent将仅针对环形缓冲区中的每个序列被一次调用一次.

onEvent will be called once and once per eventHandler only for each sequence in the ringbuffer.

在没有看到生产者代码的情况下,无法说出为什么您观察到这种行为.

Without seeing the producer code its impossible to to say why you are observing such behaviour.

可能值得添加一些日志记录,以确认每个给定序列号是什么事件.

It might be worth adding some logging to confirm what event is for each given sequence number.

这篇关于对于Java同步锁,disruptor onEvent处理程序似乎过快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 18:25