How to subscribe to notifications in Spring app on database change via Reactive PostgreSQL client.
- Runs docker container with postgres db.
- Creates and seeds table
users
- Creates trigger on table
users
on CREATE/UPDATE operations - Subscribes to notifications on
user_update
topic from Postgres - Every time table
users
is appended/updated from any source (even manual command execution), Spring app receives this event and prints it to stdout
- Install Docker
- Install Java 8+
- Install Gradle
- Clone project to local machine.
-
Go to project root directory.
-
Run
docker-compose up -d
to init container with database. -
Run
gradle bootRun
in terminal to start project.
Or import and run project in IntelliJ IDEA -
Connect to database:
- host:
localhost
- port:
54320
- db name:
postgres
- username:
postgres
- password:
test
In resources/data.sql you can find already created users.
- host:
-
Make some changes in
users
, e.g. execute this SQL query:
update users set role='test1' where first_name = 'Pavel'
-
In terminal you will see message like:
user has been updated {"operation" : "UPDATE", "user" : {"id":1,"first_name":"Pavel","last_name":"Gordon","role":"test1"}}
-
Run
docker-compose down
to tear down container with database.