Last active
July 21, 2020 17:53
-
-
Save thospfuller/6b6a81ec896201b98b0516e8772c2105 to your computer and use it in GitHub Desktop.
An example implementation of the com.impossibl.postgres.api.jdbc.PGNotificationListener specification.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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