onkami
onkami

Reputation: 9431

Lmax Disruptor, many consumers - how make consumer take only messages of particular kind and proceed independently?

I plan to have many parallel consumers in my disruptor.

I need each consumer only consume messages that are meant for them.

For instance, I have messages of types A, B, C and I have buffer like

 #1 - type A, #2 - type B, #3 - type C,  #4 - type A, #5 - type C, #6 - type C,  (and so on)

I have consumers for each of the types. How can I achieve that consumer for A will take messages 1 and 4, for type B - message 2, C - messages 3, 5, 6?

Important: I want processing to be independent. Consumers should not be chained, each traveling the buffer independently. Processing of #6 by "type C" consumer may take part earlier than #1 for type A, if consumer for A is slower than for C.

I appreciate an explanation how to do it with LMAX disruptor config.

Upvotes: 3

Views: 1107

Answers (2)

Chris Mowforth
Chris Mowforth

Reputation: 6723

The typical pattern is to use the sequence number- say you have 4 event handlers hanging off a disruptor; if you give each one a unique number, you can select whether to accept the message or not:

void onEvent(T event, long sequence, boolean endOfBatch) throws Exception {
    // instanceNumber could be assigned in a constructor 
   if ((sequence % 4) != instanceNumber) 
        // message isn't for me
        return;
   }
   // do my thing
}

Upvotes: 6

G. Fiedler
G. Fiedler

Reputation: 726

Configure the disruptor to use a single type of object but create multiple disruptors, one for each object type. In the case above, there would be three separate disruptors.

Upvotes: 1

Related Questions