2011-08-26 149 views
1

可能重複:
Using JMS to connect to IBM MQ如何發送和在使用IBM的WebSphere 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 IAM收到的消息。 IBM提供任何API來幫助在使用IBM MQ時發送和接收消息嗎?

+0

檢查此答案http://stackoverflow.com/questions/2324038/using-jms-to-connect-to-ibm-mq/3248980#3248980 –

回答

1

您需要設置一個JNDI命名服務來爲Websphere MQ提供您的JMS對象。 Websphere MQ實用程序是JMSAdmin。如果您擁有Websphere Application Server,則可以使用「Websphere MQ消息傳遞提供程序」設置JMS資源(連接工廠和隊列或主題)。請注意,Websphere MQ中定義的名稱與JNDI名稱不同:您選擇的JNDI名稱將在設置這些綁定時引用Websphere MQ的名稱。