Search code examples
javaconcurrencydisruptor-patternlmax

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.


Solution

  • 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
    }