Database event listener using spring boot

10,586

If the entity can be created from any source - e.g. manual insert - this is something which is outside of the scope and context of your running application.

What you're describing is known as the CDC (change data capture) pattern.

To implement CDC in this case you need to use the instrumentation of the underlying database - for example triggers.

As I see this is tagged with MongoDb - triggers are not an option as mongodb doesn't have support for triggers.

If you are using MongoDb v3.6+ you can leverage the new Change Streams feature. This is the official example with Java.

Change streams allow applications to access real-time data changes without the complexity and risk of tailing the oplog. Applications can use change streams to subscribe to all data changes on a single collection, a database, or an entire deployment, and immediately react to them. Because change streams use the aggregation framework, applications can also filter for specific changes or transform the notifications at will.

If you are using earlier versions of MongoDb you can monitor the oplog or use tailable cursors with capped collections.

Another approach would be to look into a 3rd party solution that turns everything happening in the DB as event streams - like for example debezium.

Share:
10,586
Jagadheeswaran Mohan
Author by

Jagadheeswaran Mohan

Updated on June 04, 2022

Comments

  • Jagadheeswaran Mohan
    Jagadheeswaran Mohan almost 2 years

    I need to attach a listener to a table in db which should call a spring boot method, once CRUD operation is performed in the table(pre listeners and post listeners) the entry can be made from any source how can i do that in spring boot?

    • Robert Moskal
      Robert Moskal almost 6 years
      I don't think spring boot is going to give you that power. You can get get such behavior out of your JPA entities. You need to look in a different place!