Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Listener for MongoDB data insertion

I have a requirement where I need to display data inserted into a particular MongoDB collection onto a User's dashboard in real time. Please note data maybe inserted by this user or other users. The dashboard is part of a Spring MVC web-application. The MongoDB data-layer is written in Spring Data.

I intend to use Server-sent events approach to push the newly inserted data to the dashboard. I am looking for an efficient way to listen to data insertion using Spring. I am even open to a non-Spring approach to implement the Listener that will eventually talk to my Spring SSE emitter.

like image 205
wildthing81 Avatar asked Sep 05 '25 03:09

wildthing81


2 Answers

If all the save goes through your spring-data layer, then you can make use of Mongo Listener Life cycle events docs.

@Component
public class MongoListener extends AbstractMongoEventListener<Account> 
{

    @Override
    public void onAfterSave(AfterSaveEvent<E> event) {

            if (LOG.isDebugEnabled()) {
                LOG.debug("onAfterSave({}, {})", event.getSource(), event.getDocument());
            }
        }
}

If not you would have to read mongo oplog and process or create a capped collection and use tailable cursors.
here is a sample project using tailable cursors.

like image 51
pvpkiran Avatar answered Sep 07 '25 17:09

pvpkiran


You can do it via OpLog collection and Tailable Cursors in MongoDB. For example, get MongoDB OpLog Collection by using flags QUERYOPTION_TAILABLE | QUERYOPTION_AWAITDATA via your MongoDB framework (for example MongoDB Java Driver) and do the following query

MongoCursor<> cursor = db.getCollection('oplog.rs').find({ns:"collectionName", op:"i"})

where "collectionName" is the name of your collection and "i" is insert operation. After receiving events from the cursor you can send the events into a shared stream.

Unfortunately, I'm not familiar with Spring Data to provide an example for this, but the approach should be the same.

like image 20
statut Avatar answered Sep 07 '25 18:09

statut