1

我的应用程序正在监听 Tibco RV,现在我需要切换到 WebSphere MQ。我找到了这样的代码

Tibrv.open(Tibrv.IMPL_NATIVE);
rvdTransport = new TibrvRvdTransport(...);
TibrvQueue queue = new TibrvQueue();
cmqTransport = new TibrvCmQueueTransport(...);
queueListener = new TibrvCmListener(...);
disp = new TibrvDispatcher(...)

在MQ这边,我们有没有类似的概念?

谢谢

4

1 回答 1

1

简短的回答 - 是的。

当您下载并安装 WMQ 客户端 ( SupportPac MQC71 ) 时,除了 Java 类、诊断实用程序和大量示例代码之外,您还可以获得。在示例程序中,您会发现MQSample.java如下所示:

import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.constants.MQConstants;

public class MQSample {
  public static void main(String args[]) {
    try {
      // Create a connection to the QueueManager
      System.out.println("Connecting to queue manager: " + qManager);
      MQQueueManager qMgr = new MQQueueManager(qManager);

      // Set up the options on the queue we wish to open
      int openOptions = MQConstants.MQOO_INPUT_AS_Q_DEF | MQConstants.MQOO_OUTPUT;

      // Now specify the queue that we wish to open and the open options
      System.out.println("Accessing queue: " + qName);
      MQQueue queue = qMgr.accessQueue(qName, openOptions);

      // Define a simple WebSphere MQ Message ...
      MQMessage msg = new MQMessage();
      // ... and write some text in UTF8 format
      msg.writeUTF("Hello, World!");

      // Specify the default put message options
      MQPutMessageOptions pmo = new MQPutMessageOptions();

      // Put the message to the queue
      System.out.println("Sending a message...");
      queue.put(msg, pmo);

      // Now get the message back again. First define a WebSphere MQ
      // message
      // to receive the data
      MQMessage rcvMessage = new MQMessage();

      // Specify default get message options
      MQGetMessageOptions gmo = new MQGetMessageOptions();

      // Get the message off the queue.
      System.out.println("...and getting the message back again");
      queue.get(rcvMessage, gmo);

      // And display the message text...
      String msgText = rcvMessage.readUTF();
      System.out.println("The message is: " + msgText);

      // Close the queue
      System.out.println("Closing the queue");
      queue.close();

      // Disconnect from the QueueManager
      System.out.println("Disconnecting from the Queue Manager");
      qMgr.disconnect();
      System.out.println("Done!");
    }
    catch (MQException ex) {
      System.out.println("A WebSphere MQ Error occured : Completion Code " + ex.completionCode
          + " Reason Code " + ex.reasonCode);
      ex.printStackTrace();
      for (Throwable t = ex.getCause(); t != null; t = t.getCause()) {
        System.out.println("... Caused by ");
        t.printStackTrace();
      }

    }
    catch (java.io.IOException ex) {
      System.out.println("An IOException occured whilst writing to the message buffer: " + ex);
    }
    return;
  }
}

当然也有 JMS、C、C# 等的示例。显示的示例使用同步(阻塞)GET调用,但如果您想使用回调机制实现,则可以使用异步侦听器方法。

我还建议查看信息中心的应用程序开发部分。 这些是 v7.0 文档,这些是 v7.1 文档。

于 2011-12-06T22:45:55.713 回答