A similar answer here: how to create a database listener using java?
You can do this with a message queue that supports transactions and simply turn off the message when the transaction ends or (close the connection) for databases that do not support notifications. This is for the most part you will have to manually notify and track what to notify.
Spring provides some automated transaction support for AMQP and JMS . A simpler alternative that you can use is Guava AsyncEventBus, but this will only work for one JVM. For all of the options below, I recommend that you tell the rest of your message queue platform.
Option - Non-settlement not related to the database
ORM option
Some libraries, such as the Hibernate JPA, have entity listeners that make this easier, but that is because they assume that they control all CRUDing.
For regular JDBC you will have to do your own bookkeeping. That is, after the connection is fixed or closed, you then send a message to MQ that something is updated.
JDBC parsing
One difficult option for doing accounting is to wrap / decorate your java.sql.DataSource and / or java.sql.Connection in your custom, for example, so that you send a message to commit() (and close). I believe that some federated caching systems do this. You can capture the executed SQL and parse it to see if its INSERT or UPDATE, but with very sophisticated parsing and metadata, you will not get row level listening. Unfortunately, I have to admit that this is one of the benefits provided by ORM, as it knows that you are updating.
Option Dao
The best option, if you are not using ORM, simply manually send a message to the DAO after the transaction is closed, that the row has been updated. Just make sure the transaction is closed before sending the message.
Option - non-database polling
Follow @GlenBest guidelines to some extent.
I am a couple of things that I would do differently. I would select a timer or make it so that only one server starts a timer (i.e. a scheduler). I would just use a ScheduledExecutorService (preferably wrapping it in a Guava ListenerScheduledExecutorService ) instead of Quartz (IMHO uses quartz to poll super overkill).
Far from all of your tables that you want to see must add a "notified" column.
Then you do something like:
// BEGIN Transaction List<String> ids = execute("SELECT id FROM table where notified = 'f'"); //If db not transactional either insert ids in a tmp table or use IN clause execute("update table set notified = 't' where notified = 'f'") // COMMIT Transaction for (String id : ids) { mq.sendMessage(table, id); }
Option - db
With Postgres NOTIFY you still need to interrogate to some extent so that you complete most of the above and then send a message to the bus.