0

System A - updates a postgresql table say Test, Each time the table is updated with new entry then,

System B (Spring boot project) – should somehow listen to dbChange (Test) and read the new entries

I tried @Eventlistener in system B to get the updated data but no luck. can someone help how to get the data from table into a seperate system B when a table has been updated?

Andrew Regan
  • 5,087
  • 6
  • 37
  • 73
sirisha
  • 1
  • 1
  • 1
  • Possible duplicate of [How can I send some http request from postgresql function or trigger](https://stackoverflow.com/questions/3325292/how-can-i-send-some-http-request-from-postgresql-function-or-trigger) – Hintham Sep 23 '19 at 07:50

2 Answers2

1

did you see "TRIGGER" for postgres? It allow you to do something after or before specific action is performed. Every trigger is related to a specific Table or view.

Postgress' documentation says:

CREATE [ CONSTRAINT ] TRIGGER name { BEFORE | AFTER | INSTEAD OF } { event [ OR ... ] }  
    ON table  
    [ FROM referenced_table_name ]  
    [ NOT DEFERRABLE | [ DEFERRABLE ] { INITIALLY IMMEDIATE | INITIALLY DEFERRED } ]  
    [ FOR [ EACH ] { ROW | STATEMENT } ]  
    [ WHEN ( condition ) ]  
    EXECUTE PROCEDURE function_name ( arguments )  

where event can be one of:

INSERT,  
UPDATE [ OF column_name [, ... ] ],  
DELETE,  
TRUNCATE  

And simple description:

CREATE TRIGGER creates a new trigger. The trigger will be associated with the specified table or view and will execute the specified function function_name when certain events occur.

The trigger can be specified to fire before the operation is attempted on a row (before constraints are checked and the INSERT, UPDATE, or DELETE is attempted); or after the operation has completed (after constraints are checked and the INSERT, UPDATE, or DELETE has completed); or instead of the operation (in the case of inserts, updates or deletes on a view). If the trigger fires before or instead of the event, the trigger can skip the operation for the current row, or change the row being inserted (for INSERT and UPDATE operations only). If the trigger fires after the event, all changes, including the effects of other triggers, are "visible" to the trigger.

If you want to read more you can read official Postgres' documentation

You can use triggers to do HTTP(S) requests to one specific endpoint in order to say to your spring application that something is just changed on your DB.

Community
  • 1
  • 1
Luke
  • 516
  • 2
  • 10
0

You can use RepositoryEventHandler to handle persistence events ( after add, after updated) and publish events using a messaging system (RabbitMQ, ActiveMQ, Kafka)

enter image description here

https://www.baeldung.com/spring-data-rest-events

Mounir Messaoudi
  • 343
  • 1
  • 10