简体   繁体   中英

Database event listener using spring boot

我需要将一个监听器附加到应该调用 spring boot 方法的 db 表中,一旦在表中执行 CRUD 操作(预监听器和后监听器),可以从任何来源创建条目,我怎么能在 spring boot 中做到这一点?

If the entity can be created from any source - eg 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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM