【问题标题】:How to send and receive messages when IBM Websphere MQ is used [duplicate]使用 IBM Websphere MQ 时如何发送和接收消息 [重复]
【发布时间】:2011-11-04 20:01:52
【问题描述】:

可能重复:
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 是否提供任何 API 来帮助在使用 IBM MQ 时发送和接收消息?

【问题讨论】:

标签: jms


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2016-04-08
    • 1970-01-01
    • 2016-10-31
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-19
    相关资源
    最近更新 更多