Reputation: 774
I have written a Java application which should be started or woken up when 'something' happens in the database. For example an insert into the user table should trigger the sending of the usual welcome, password, ... mails.
What is the best common practice to do this? I can write my application such that it executes the following query let's say every second:
select mail from user where mail_sent = false
But this is polling, and I would like to avoid it. Is there a way to start or wake-up my Java application (push) initiated by a change in the database?
Cheers!
Upvotes: 7
Views: 3870
Reputation: 522
Try this..It is easy to understand and will help u a lot
https://wiki.postgresql.org/wiki/PgNotificationPoller
Upvotes: 1
Reputation: 109265
I don't think it is possible to start an application. However when you have an application running, that application can subscribe to listen for events. You can use triggers to post (notify) events for the certain actions.
When the application is notified of the event it can respond with certain actions. See the postgresql site for an example.
Upvotes: 0
Reputation: 12019
Triggers in PostgreSQL can be written in a host of languages, amongst which is PL/Java. You could set a trigger on the tables that require this monitoring for the relevant actions (insert, delete, update...) and have the trigger take care of the notification. It might require some form of inter-process communication, but if both trigger and client are written in Java that shouldn't prove too difficult.
Upvotes: 3