Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An example implementation of the com.impossibl.postgres.api.jdbc.PGNotificationListener specification.
@Grapes(
@Grab(group='com.impossibl.pgjdbc-ng', module='pgjdbc-ng', version='0.8.4')
)
import com.impossibl.postgres.api.jdbc.PGConnection
import com.impossibl.postgres.api.jdbc.PGNotificationListener
import com.impossibl.postgres.jdbc.PGDataSource
PGDataSource dataSource = new PGDataSource();
dataSource.setHost("0.0.0.0")
dataSource.setPort(5432)
dataSource.setDatabaseName("testdb")
dataSource.setUser("postgres")
dataSource.setPassword("password")
final def pgNotificationListener = new PGNotificationListener () {
@Override
public void notification(int processId, String channelName, String payload) {
println("processId $processId, channelName: $channelName, payload: $payload")
}
}
final def connection = (PGConnection) dataSource.getConnection()
connection.addNotificationListener(pgNotificationListener)
final def statement = connection.createStatement()
statement.execute("LISTEN examplechannel")
statement.close()
def time = 60 * 60 * 1000
println "Will sleep for $time milliseconds..."
try {
Thread.sleep (time)
} catch (Throwable thrown) {
thrown.printStackTrace (System.err)
} finally {
connection.close ()
}
print "...done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment