while (true) {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT 1");
rs.close();
stmt.close();
org.postgresql.PGNotification notifications[] = pgconn.getNotifications();
handleNotifications(notifications);
Thread.sleep(100);
}
The problem with this approach[1] is there's a 100ms of latency between polling attempts (the sleep call).LISTEN/NOTIFY is very useful, particularly the transactional nature and not requiring any additional tech stack (e.g. no need for a separate MQ server), but for frequent/high-value/lower latency signalling you're better off using something that doesn't require sleeping/polling.
[1]: That and the lack of exception handling or resource cleanup.