首先...我对这个场景有疑问。这是某种测试/练习,还是我们在谈论真实世界的场景?
所有客户都对电影单独的主题订阅者感兴趣吗?那规模如何?我计划为每部电影设置一个主题,并且可能有兴趣的各方声明持久订阅者(每部电影一个)?这似乎是对持久订阅者的滥用......我建议只使用一个订阅者(在系统 B 中)到“电影上映”事件/主题(来自系统 A),并让一些代码(在系统 B 中)读取所有来自数据库的客户发送电子邮件/消息/任何东西。(如果系统 A 和 B 相同,则使用 EMS 可能不是一个好主意......取决于。)
如果这不是练习,我必须评论:不要使用 MOM(EMS、ActiveMQ)来做 DBMS(Oracle、PostGreSQL)的工作!
完成免责声明部分后,我建议采用异步订阅方法(这两个剪辑用于 EMS 示例目录。文件tibjmsAsyncMsgConsumer.java)。
从构造函数中提取(主类必须实现ExceptionListener、MessageListener):
ConnectionFactory factory = new com.tibco.tibjms.TibjmsConnectionFactory(serverUrl);
/* create the connection */
connection = factory.createConnection(userName,password);
/* create the session */
session = connection.createSession();
/* set the exception listener */
connection.setExceptionListener(this);
/* create the destination */
if (useTopic)
destination = session.createTopic(name);
else
destination = session.createQueue(name);
System.err.println("Subscribing to destination: "+name);
/* create the consumer */
msgConsumer = session.createConsumer(destination);
/* set the message listener */
msgConsumer.setMessageListener(this);
/* start the connection */
connection.start();
然后在每次消息到达时调用该方法。
public void onMessage(Message msg)
{
try
{
System.err.println("Received message: " + msg);
}
catch (Exception e)
{
System.err.println("Unexpected exception in the message callback!");
e.printStackTrace();
System.exit(-1);
}
}