使用 Java 获取 MQ 队列的 ClusterName

Get ClusterName of MQ Queue using Java

我正在构建一个 java 应用程序,它连接到 MQQueueManager 并提取有关队列的信息。我能够获取 QueueTypeMaximumMessageLength 等数据。但是,我还想要队列可能所在的集群的名称。MQQueue 没有提供给我此信息的函数。在互联网上搜索后,我发现了一些指向这个方向的东西,但没有例子。

给我 MaximumDepth 的函数的一部分是:

    queueManager = makeConnection(host, portNo, qMgr, channelName);
    queue = queueManager.accessQueue(queueName, CMQC.MQOO_INQUIRE);
    maxQueueDepth = queue.getMaximumDepth();

makeConnection 未在此处显示,它是与 QueueManager 建立实际连接的函数;为了减少混乱,我还省略了 try/catch/finally)

如何获取 ClusterName 以及其他没有 queue.getMaximumDepth() 功能的数据?

有两种方法可以获取有关队列的信息。

API 查询调用获取队列的运行状态。这包括诸如 MQOpen 调用解析到的名称或深度(如果队列是本地的)之类的东西。许多 q.inquire 功能已被队列中的 getter 和 setter 功能取代。如果您没有使用具有最新功能的 v8.0 client,强烈建议您升级。它可以访问所有版本的QMgr。

以下代码来自Getting and setting attribute values in WebSphere MQ classes for Java

// inquire on a queue
final static int MQIA_DEF_PRIORITY = 6;
final static int MQCA_Q_DESC = 2013;
final static int MQ_Q_DESC_LENGTH = 64;

int[] selectors  = new int[2];
int[] intAttrs   = new int[1];
byte[] charAttrs = new byte[MQ_Q_DESC_LENGTH]

selectors[0] = MQIA_DEF_PRIORITY;
selectors[1] = MQCA_Q_DESC;

queue.inquire(selectors,intAttrs,charAttrs);

System.out.println("Default Priority = " + intAttrs[0]);
System.out.println("Description : " + new String(charAttrs,0));

对于不属于 API 查询调用的部分,需要 PCF 命令。 Programmable Command Format,通常缩写为PCF,是一种消息格式,用于向命令队列传递消息,以及从命令队列、事件队列等读取消息。

要使用 PCF 命令,调用应用程序必须在 SYSTEM.ADMIN.COMMAND.QUEUE 上获得 +put 授权,在被查询的对象上获得 +dsp 授权。

IBM 提供示例代码。
关于Windows,请看:%MQ_FILE_PATH%\Tools\pcf\samples
在 UNIX 版本中,请参阅:/opt/mqm/samp/pcf/samples
这些位置可能会有所不同,具体取决于 MQ 的安装位置。

请参阅:Handling PCF messages with IBM MQ classes for Java。以下代码段来自 PCF_DisplayActiveLocalQueues.java 示例程序。

  public static void DisplayActiveLocalQueues(PCF_CommonMethods pcfCM) throws PCFException,
      MQDataException, IOException {
    // Create the PCF message type for the inquire.
    PCFMessage pcfCmd = new PCFMessage(MQConstants.MQCMD_INQUIRE_Q);

    // Add the inquire rules.
    // Queue name = wildcard.
    pcfCmd.addParameter(MQConstants.MQCA_Q_NAME, "*");

    // Queue type = LOCAL.
    pcfCmd.addParameter(MQConstants.MQIA_Q_TYPE, MQConstants.MQQT_LOCAL);

    // Queue depth filter = "WHERE depth > 0".
    pcfCmd.addFilterParameter(MQConstants.MQIA_CURRENT_Q_DEPTH, MQConstants.MQCFOP_GREATER, 0);

    // Execute the command. The returned object is an array of PCF messages.
    PCFMessage[] pcfResponse = pcfCM.agent.send(pcfCmd);

    // For each returned message, extract the message from the array and display the
    // required information.
    System.out.println("+-----+------------------------------------------------+-----+");
    System.out.println("|Index|                    Queue Name                  |Depth|");
    System.out.println("+-----+------------------------------------------------+-----+");

    for (int index = 0; index < pcfResponse.length; index++) {
      PCFMessage response = pcfResponse[index];

      System.out.println("|"
          + (index + pcfCM.padding).substring(0, 5)
          + "|"
          + (response.getParameterValue(MQConstants.MQCA_Q_NAME) + pcfCM.padding).substring(0, 48)
          + "|"
          + (response.getParameterValue(MQConstants.MQIA_CURRENT_Q_DEPTH) + pcfCM.padding)
              .substring(0, 5) + "|");
    }

    System.out.println("+-----+------------------------------------------------+-----+");
    return;
  }
}

经过更多研究,我终于找到了我要找的东西。 IBM的这个例子:Getting and setting attribute values in WebSphere MQ classes帮我设置了查询。

我在此列表中找到的必要值:Constant Field Values

我还需要扩展accessQueue()openOptionsArg,否则无法查询集群队列。

最终结果: (没有 makeConnection()

public class QueueManagerServices {

final static int MQOO_INQUIRE_TOTAL = CMQC.MQOO_FAIL_IF_QUIESCING | CMQC.MQOO_INPUT_SHARED | CMQC.MQOO_INQUIRE;

MQQueueManager queueManager = null;
String cluster = null;
MQQueue queue = null;

public String getcluster(String host, int portNo, String qMgr, String channelName){

 try{
  queueManager = makeConnection(host, portNo, qMgr, channelName);
  queue = queueManager.accessQueue(queueName, MQOO_INQUIRE_TOTAL);

  int MQCA_CLUSTER_NAME = 2029;
  int MQ_CLUSTER_NAME_LENGTH = 48;

  int[] selectors = new int[1];
  int[] intAttrs = new int[1];
  byte[] charAttrs = new byte[MQ_CLUSTER_NAME_LENGTH];

  selectors[0] = MQCA_CLUSTER_NAME;

  queue.inquire(selectors, intAttrs, charAttrs);

  cluster = new String (charAttrs);

  } catch (MQException e) {
      System.out.println(e);
  } finally {
      if (queue != null){
        queue.close();
      }
      if (queueManager != null){
        queueManager.disconnect();
      }
  }
  return cluster;
  }
 }