JMS - 异步消息的使者
2009-09-23 00:00:00 来源:WEB开发网Receiver:
Context context = new InitialContext();
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) context.lookup("QueueConnectionFactoryTest");
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = (Queue) context.lookup("QueueTest");
QueueReceiver queueReceiver = queueSession.createReceiver(queue);
queueConnection.start();
while (true) {
Message m = queueReceiver.receive();
if (m instanceof TextMessage) {
TextMessage message = (TextMessage) m;
System.out.println("Reading message: " + message.getText());
} else {
break;
}
}
queueConnection.close();
由上代码可以看出,sender和receiver的实现基本是相同的,下面我们再来对比一下pub/sub模式的实现,
publisher:
Context context = new InitialContext();
TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) context.lookup("TopicConnectionFactoryTest");
TopicConnection topicConnection = topicConnectionFactory.createTopicConnection();
TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = (Topic) context.lookup("TopicTest");
TopicPublisher topicPublisher = topicSession.createPublisher(topic);
TextMessage sentMessage = topicSession.createTextMessage();
sentMessage.setText("Here is a message" + i);
System.out.println("PUBLISHER THREAD: Publishing message: " + sentMessage.getText());
topicPublisher.publish(sentMessage);
topicConnection.close();
- ››JMS、XML和异构企业
- ››消息称中国移动即将获得iPhone 4销售权
- ››异步调用与线程(总结篇)
- ››消息称联通高层赴美谈引入iPhone4 将带WiFi
- ››消息称微软将在近期发布IE9 beta
- ››消息称台湾最快7到8月引入新版iPhone
- ››消息称微软中国Windows 7正版验证计划或将推迟
- ››消息称富士康今年交付2400万部iPhone 4G
- ››消息称谷歌与运营商分享 Android 广告收入
- ››异步操作和 Web 服务,第 2 部分:构建异步 Web 服...
- ››异步操作和 Web 服务,第 3 部分: 向 Web 服务添加...
- ››消息称联通约见开发者商谈 iPhone 应用软件
更多精彩
赞助商链接