姚博文 postgresql listen/notify push message

使用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;
}
}
}
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。