disruptor onEvent handler appears to be too fast for java synchronized lock

孤街醉人 提交于 2019-12-12 03:59:30

问题


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).

Here's some code:

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?

Here is the producer code:

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);
    }
}

回答1:


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.



来源:https://stackoverflow.com/questions/32566752/disruptor-onevent-handler-appears-to-be-too-fast-for-java-synchronized-lock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!