Search code examples
javamongodbdisruptor-patternlmax

how to push list to lmax disruptor in the eventclass


I need to store and get a huge data from mongodb so Im asked to receive and store data using lmax disruptor I have passed few days looking to a simple tutorial on the lmax github account, but I didn t understand it well how to put my special data inside lmax disruptor mechanism wheel


Solution

  • There is a ringBuffer.publishEvents(..) method that accepts the collection of objects. You can use this method if you want to publish a whole batch to ring buffer.

    If you want to consume a batch of data from a ring buffer (in a handler), and process forward with a batch, you can use EventHandler with onEvent() method's parameter 'endOfBatch'. This parameter is a flag that shows you where is a good time to flush a buffer. I use this mechanism in my projects to create a batch. Please take a look at the example below:

    @Component
    public class MyHandler implements EventHandler<Event> {
        private List<Event> batchOfEvents = new LinkedList<>();
    
        @Override
        public void onEvent(Event Event, long sequence, boolean endOfBatch) throws Exception {
            // Add message to batch
            batchOfEvents.add(event);
    
            if (endOfBatch && !batchOfEvents.isEmpty()) {
                // Feed your batch to some Mongo DB dao or any other service
                someService.process(batchOfEvents);
                batchOfEvents.clear();
            }
    }