【发布时间】:2011-11-04 20:01:52
【问题描述】:
我知道 JMS 是 sun 提供的消息传递标准,而 IBM Websphere MQ 是 JMS 的实现。
我一直使用 JMS,从未使用过 MQ。所以我有几个问题。
使用 JMS,我将在应用服务器中配置连接工厂和队列,并使用以下代码发送和接收消息。在 JMS 中,我们使用 javax.jms.* 包。
发送消息代码
Context context = new InitialContext();
QueueConnectionFactory queueConnectionFactory =`enter code here`
(QueueConnectionFactory) context.lookup("QueueConnectionFactory");
String queueName = "MyQueue";
Queue queue = (Queue) context.lookup(queueName);
queueConnection =
queueConnectionFactory.createQueueConnection() ;
QueueSession queueSession =
queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
QueueSender queueSender = queueSession.createSender(queue);
TextMessage message = queueSession.createTextMessage();
message.setText("This is a TextMessage");
queueSender.send(message) ;
接收消息代码
Context context = new InitialContext();
QueueConnectionFactory queueConnectionFactory =(QueueConnectionFactory) context.lookup("QueueConnectionFactory");
String queueName = "MyQueue";
Queue queue = (Queue) context.lookup(queueName);
QueueConnection queueConnection =queueConnectionFactory.createQueueConnection() ;
QueueSession queueSession = queueConnection.createQueueSession (false, •*■ Session.AUTO_ACKNOWLEDGE) ;
QueueReceiver queueReceiver = queueSession.createReceiver(queue);
queueConnection.start() ;
Message message = queueReceiver.receive(1) ;
请告诉我在使用 IBM Websphere MQ 时如何发送接收消息。 IBM 是否提供任何 API 来帮助在使用 IBM MQ 时发送和接收消息?
【问题讨论】:
标签: jms