问题
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