2009-10-06 267 views
4

嗨有誰知道如何使用IBM MQ創建消息監聽器?我知道如何使用JMS規範來做到這一點,但我不確定如何爲IBM MQ做到這一點。任何鏈接或指針非常感謝。IBM MQ消息監聽器

+0

如果你知道如何做到這一點的JMS,爲什麼你需要使它具體化MQ? – skaffman 2009-10-28 09:42:31

+0

嘿,你能告訴我如何使用JMS連接到IBM MQ,就像我想知道如何指定隊列管理器,通道等 – 2010-02-24 06:19:23

回答

4

看一看IBM的幫助:Writing WebSphere MQ base Java applications

IBM具有與隊列交互的API。以下是他們的示例:

import com.ibm.mq.*;   // Include the WebSphere MQ classes for Java package 


public class MQSample 
{ 
    private String qManager = "your_Q_manager"; // define name of queue 
               // manager to connect to. 
    private MQQueueManager qMgr;     // define a queue manager 
               // object 
    public static void main(String args[]) { 
    new MQSample(); 
    } 

    public MQSample() { 
    try { 

     // Create a connection to the queue manager 

     qMgr = new MQQueueManager(qManager); 

     // Set up the options on the queue we wish to open... 
     // Note. All WebSphere MQ Options are prefixed with MQC in Java. 

     int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | 
         MQC.MQOO_OUTPUT ; 

     // Now specify the queue that we wish to open, 
     // and the open options... 

     MQQueue system_default_local_queue = 
       qMgr.accessQueue("SYSTEM.DEFAULT.LOCAL.QUEUE", 
           openOptions); 

     // Define a simple WebSphere MQ message, and write some text in UTF format.. 

     MQMessage hello_world = new MQMessage(); 
     hello_world.writeUTF("Hello World!"); 

     // specify the message options... 

     MQPutMessageOptions pmo = new MQPutMessageOptions(); // accept the // defaults, 
                  // same as MQPMO_DEFAULT 

     // put the message on the queue 

     system_default_local_queue.put(hello_world,pmo); 

     // get the message back again... 
     // First define a WebSphere MQ message buffer to receive the message into.. 

     MQMessage retrievedMessage = new MQMessage(); 
     retrievedMessage.messageId = hello_world.messageId; 

     // Set the get message options... 

     MQGetMessageOptions gmo = new MQGetMessageOptions(); // accept the defaults 
                  // same as MQGMO_DEFAULT 
     // get the message off the queue... 

     system_default_local_queue.get(retrievedMessage, gmo); 

     // And prove we have the message by displaying the UTF message text 

     String msgText = retrievedMessage.readUTF(); 
     System.out.println("The message is: " + msgText); 
     // Close the queue... 
     system_default_local_queue.close(); 
     // Disconnect from the queue manager 

     qMgr.disconnect(); 
    } 
     // If an error has occurred in the above, try to identify what went wrong 
     // Was it a WebSphere MQ error? 
    catch (MQException ex) 
    { 
     System.out.println("A WebSphere MQ error occurred : Completion code " + 
         ex.completionCode + " Reason code " + ex.reasonCode); 
    } 
     // Was it a Java buffer space error? 
    catch (java.io.IOException ex) 
    { 
     System.out.println("An error occurred whilst writing to the message buffer: " + ex); 
    } 
    } 
} // end of sample 

我不確定IBM jar是否位於基本Maven回購站。我知道在過去,我必須從本地IBM安裝中提取它們,並將它們放入本地SVN回購。我正在使用以下罐子:

<dependency> 
    <groupId>com.ibm</groupId> 
    <artifactId>com.ibm.mq</artifactId> 
    <version>5.3.00</version> 
    <scope>compile</scope> 
</dependency> 
    <dependency> 
    <groupId>com.ibm</groupId> 
    <artifactId>com.ibm.mq.pcf</artifactId> 
    <version>5.3.00</version> 
    <scope>compile</scope> 
</dependency> 
    <dependency> 
    <groupId>com.ibm</groupId> 
    <artifactId>com.ibm.mqbind</artifactId> 
    <version>5.3.00</version> 
    <scope>compile</scope> 
</dependency> 
    <dependency> 
    <groupId>com.ibm</groupId> 
    <artifactId>com.ibm.mqjms</artifactId> 
    <version>5.3.00</version> 
    <scope>compile</scope> 
</dependency> 
+0

嗨,謝謝你的迴應。我已經檢查過這些網站,但我仍然沒有找到使用異步消息監聽器的示例。有任何想法嗎?謝謝。 – x1a0 2009-10-06 15:50:02

+0

我們創建一個線程,該線程在位於隊列管理器中的隊列上每隔X秒查找一次消息。另外,如果這是您真正想要的,您可能需要編輯原始問題。 – Droo 2009-10-07 03:39:51

2

查看上面提供的示例。

具體的線路

MQGetMessageOptions gmo = new MQGetMessageOptions();  
system_default_local_queue.get(retrievedMessage, gmo); 

您可以配置得到拋出一個異常MQRC_NO_MSG_AVAILABLE之前等待指定的時間。或者你可以永遠等待。

gmo.waitInterval= qTimeout; 
gmo.options = MQC.MQGMO_WAIT 

因此,你可以創建一個線程,不斷尋找新的消息,然後將它們傳遞給處理程序。獲取和放置不需要處於同一個線程甚至應用程序中。

我希望這有助於回答你的問題。

6

儘管前面的響應者提到了WMQ Java API,但WMQ也支持JMS,因此這裏有一些資源可以幫助您開始使用。

看看這篇文章:IBM WebSphere Developer Technical Journal: Running a standalone Java application on WebSphere MQ V6.0

而且,如果你已經安裝了完整的WMQ客戶,而不僅僅是抓住罐子,那麼你將有很多的安裝示例代碼。默認情況下,這些文件將位於C:\ Program Files \ IBM \ WebSphere MQ \ tools \ jms或/ opt/mqm/samp中,具體取決於您的平臺。

如果您需要WMQ客戶端安裝介質,請獲取它here。請注意,這是WMQ v7客戶端,而不是v6客戶端。它與v6 QMgr兼容,但自從v6到2011年9月的報廢之後,您應該在v7客戶端上進行新的開發,並且如果可能的話,還需要v7 QMgr。如果雙方都是v7,有很多功能和性能增強。

如果您需要,您可以獲得產品手冊here

最後,請確定何時得到JMS異常以打印鏈接的異常。這不是一個WMQ的東西,而是一個JMS的東西。 Sun爲JMS異常提供了一個多層次的數據結構,真正有趣的部分通常是嵌套的層次。這不是什麼大不了的事,可在幾行來實現:

try { 
    . 
    . code that might throw a JMSException 
    . 
} catch (JMSException je) { 
    System.err.println("caught "+je); 
    Exception e = je.getLinkedException(); 
    if (e != null) { 
    System.err.println("linked exception: "+e); 
    } else { 
    System.err.println("No linked exception found."); 
    } 
} 

這有助於確定一個JMS誤差與輸送誤差之間的差異。例如,JMS安全錯誤可能是WMQ 2035,也可能是JSSE配置,或者應用程序可能無法訪問文件系統中的某些內容。這些值中只有一個值得花費大量的時間來挖掘WMQ錯誤日誌,並且只有通過打印鏈接的異常,您才能分辨出是否是那一個。

2

除現有答案外,還有一個重要的觀點:JMS提供了MessageListener,它允許您以異步回調的形式接收消息。

本地API有沒有等效的功能!必須適當地重複撥打get(...)

在循環
2

得到消息之前,您可以指定如下

gmo.options = MQC.MQGMO_WAIT 
gmo.waitInterval = MQConstants.MQWI_UNLIMITED; 

這使得循環會等到有隊列中的消息。 對我來說,這是以防萬一有人類似於MessageListerner

2

將谷歌計算器的MQ監聽像我一樣...... 這可能是沒有答案的,由於JMS實現,但是這就是我一直在尋找。 事情是這樣的:

MQQueueConnectionFactory cf = new MQQueueConnectionFactory(); 
MQQueueConnection conn = (MQQueueConnection)cf.createQueueConnection(); 
MQQueueSession session = (MQQueueSession)conn.createSession(false, 1); 

Queue queue = session.createQueue("QUEUE"); 

MQQueueReceiver receiver = (MQQueueReceiver)session.createReceiver(queue); 

receiver.setMessageListener(new YourListener()); 

conn.start(); 

YourListener應該實現MessageListener接口,您將收到你的郵件進入的onMessage(信息MSG)方法。

0

您好,這裏是IBM MQ的消息監聽器的工作示例。在這裏,我用春天也創造豆類等...

package queue.app; 

import javax.annotation.PostConstruct; 
import javax.jms.Message; 
import javax.jms.MessageListener; 
import javax.jms.Queue; 
import javax.jms.QueueConnection; 
import javax.jms.QueueReceiver; 
import javax.jms.QueueSession; 
import javax.jms.Session; 
import javax.jms.TextMessage; 

import org.apache.log4j.Logger; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.stereotype.Component; 

import com.ibm.mq.jms.MQQueue; 
import com.ibm.mq.jms.MQQueueConnectionFactory; 
import com.ibm.msg.client.wmq.WMQConstants; 


@Component 
public class QueueConsumer implements MessageListener{ 

    private Logger logger = Logger.getLogger(getClass()); 

    MQQueueConnectionFactory qcf = new MQQueueConnectionFactory(); 
    QueueConnection qc; 
    Queue queue; 
    QueueSession queueSession; 
    QueueReceiver qr; 

    @Value("${jms.hostName}") 
    String jmsHost; 
    @Value("${jms.port}") 
    String jmsPort; 
    @Value("${jms.queue.name}") 
    String QUEUE_NAME; 
    @Value("${jms.queueManager}") 
    String jmsQueueMgr; 
    @Value("${jms.username}") 
    String jmsUserName; 
    @Value("${jms.channel}") 
    String jmsChannel; 

    @PostConstruct 
    public void init() throws Exception{ 
     qcf.setHostName (jmsHost); 
     qcf.setPort (Integer.parseInt(jmsPort)); 
     qcf.setQueueManager (jmsQueueMgr); 
     qcf.setChannel (jmsChannel); 
     qcf.setTransportType (WMQConstants.WMQ_CM_CLIENT); 
     qc = qcf.createQueueConnection(); 

     queue = new MQQueue(QUEUE_NAME); 
     qc.createQueueSession (false, Session.AUTO_ACKNOWLEDGE); 
     queueSession = qc.createQueueSession (false, Session.AUTO_ACKNOWLEDGE); 
     qr = queueSession.createReceiver(queue); 
     qr.setMessageListener(this); 
     qc.start(); 

    } 


    @Override 
    public void onMessage(Message message) { 
     logger.info("Inside On Message..."); 
     long t1 = System.currentTimeMillis(); 
     logger.info("Message consumed at ...."+t1); 

     try{ 
      if(message instanceof TextMessage) { 
       logger.info("String message recieved >> "+((TextMessage) message).getText()); 
      } 

     }catch(Exception e){ 
      e.printStackTrace(); 
     } 

    } 
} 

下面是依賴我有...

<dependency> 
      <groupId>com.sun.messaging.mq</groupId> 
      <artifactId>fscontext</artifactId> 
      <version>4.2</version> 
      <scope>test</scope> 
     </dependency> 

     <dependency> 
      <groupId>com.ibm</groupId> 
      <artifactId>jms</artifactId> 
      <version>1.0</version> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-jms</artifactId> 
      <version>3.2.17.RELEASE</version> 
     </dependency> 


     <dependency> 
      <groupId>com.ibm</groupId> 
      <artifactId>com.ibm.mq</artifactId> 
      <version>1.0</version> 
     </dependency> 
     <dependency> 
      <groupId>com.ibm</groupId> 
      <artifactId>com.ibm.mq.allclient</artifactId> 
      <version>1.0</version> 
     </dependency> 
     <dependency> 
      <groupId>com.ibm</groupId> 
      <artifactId>com.ibm.mq.jmqi</artifactId> 
      <version>1.0</version> 
     </dependency> 
     <dependency> 
      <groupId>com.ibm</groupId> 
      <artifactId>com.ibm.mqjms</artifactId> 
      <version>1.0</version> 
     </dependency>