Postgresql - java - activate an application when something in the database happens

I wrote a Java application that should be running or waking up when something happens in the database. For example, insertion into a user table should trigger the sending of usual greetings, passwords, ... letters.

What is the best common practice? I can write my application so that it executes the following request: say every second:

select mail from user where mail_sent = false 

But this is a survey, and I would like to avoid it. Is there a way to launch or launch my Java (push) application triggered by a change in the database?

Hooray!

+7
source share
4 answers

Take a look at NOTIFY

+2
source

PostgreSQL triggers can be written in many languages, including PL / Java. You can set the trigger in tables requiring this monitoring for the corresponding actions (insert, delete, update ...) and run the trigger for notification. This may require some form of interprocess communication, but if Java has both a trigger and a client, these should not be too complicated.

+3
source

Try it. It is easy to understand and will help you a lot.

https://wiki.postgresql.org/wiki/PgNotificationPoller

+1
source

I do not think you can run the application. However, when the application starts, this application may subscribe to listen to events. You can use triggers to send (notify) events for specific actions.

When an application is notified of an event, it can respond with certain actions. See postgresql for an example .

0
source

All Articles