javapostgresql

How do I get asynchronous / event-driven LISTEN/NOTIFY support in Java using a Postgres database?


From what I can tell, the JDBC drivers for LISTEN/NOTIFY in Java do NOT support true event-driven notifications. You have to poll the database every so often to see if there's a new notification.

What options do I have in Java (possibly something other than JDBC?), if any, to get notifications asynchronously in a true event-driven manner without polling?


Solution

  • Use the pgjdbc-ng driver.

    http://impossibl.github.io/pgjdbc-ng/

    It supports async notifications, without polling. I have used it successfully.

    See https://database-patterns.blogspot.com/2014/04/postgresql-nofify-websocket-spring-mvc.html

    Source code: https://bitbucket.org/neilmcg/postgresql-websocket-example

    Oleg has a nice example answer as well