Jagadheeswaran Mohan
Jagadheeswaran Mohan

Reputation: 381

Database event listener using spring boot

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?

Upvotes: 2

Views: 14674

Answers (2)

hovanessyan
hovanessyan

Reputation: 31463

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.

Upvotes: 3

Edward Aung
Edward Aung

Reputation: 3522

This article explains how to call any program from DB-Trigger.

Therefore, you can just create a Spring Boot java app and make the sys call to your app.

Similar mechanism is also available in Oracle and other DB.

Upvotes: 0

Related Questions