如何在使用IBM Websphere MQ时发送和接收消息

时间:2011-08-26 20:33:51

标签: jms

  

可能重复:
  Using JMS to connect to IBM MQ

我知道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是否提供任何有助于在使用IBM MQ时发送和接收消息的API?

1 个答案:

答案 0 :(得分:1)

您需要设置JNDI命名服务,以便为Websphere MQ提供JMS对象。 Websphere MQ实用程序就是JMSAdmin。如果您有Websphere Application Server,则可以使用“Websphere MQ消息提供程序”设置JMS资源(连接工厂和队列或主题)。请注意,Websphere MQ中定义的名称与JNDI名称不同:您选择在设置这些绑定时JNDI名称将引用Websphere MQ名称。

相关问题