標籤:姚博文 postgresql liste
使用postgresql向客端推送訊息
#listen message_channel;
notify message_channel,‘hello todd‘;
jdbc:
Statement stmt = conn.createStatement();
stmt.execute("LISTEN message_channel");
stmt.close();
notifications = conn.getNotifications();
if (notifications != null && notifications.length > 0) {
for( final Session session: sessions ) {
for (int i=0; i<notifications.length; i++) {
// System.out.println("Got notification: " + notifications[i].getName());
// System.out.println("Got notification: " + notifications[i].getParameter());
// System.out.println("Got notification: " + notifications[i].getPID());
try {
session.getBasicRemote().sendObject(notifications[i].getParameter());
logger.debug("發送了訊息(" + session.getId() + "):" + notifications[i].getParameter());
} catch (Exception e) {
logger.error("session(" + ((session != null)?session.getId():"") + ")發送失敗:" + e.getMessage());
continue;
}
}
}
}
姚博文 postgresql listen/notify push message