简体   繁体   English

使用 Java 获取 MQ 队列的 ClusterName

[英]Get ClusterName of MQ Queue using Java

I'm building a java application that connects to a MQQueueManager and extracts information about queues.我正在构建一个连接到 MQQueueManager 并提取有关队列的信息的 Java 应用程序。 I'm able to get data like QueueType , MaximumMessageLength and more.我能够获取QueueTypeMaximumMessageLength等数据。 However, I also want the name of the cluster the queue might be in. There is no function that comes with the MQQueue that gives me this information.但是,我还需要队列可能所在的集群的名称。MQQueue 中没有提供此信息的函数。 After searching the internet I found several things pointing in this direction, but no examples.在互联网上搜索后,我发现了一些指向这个方向的东西,但没有例子。

A part of my function that gives me the MaximumDepth is:给我最大MaximumDepth函数的一部分是:

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

( makeConnection is not shown here, it is the function that makes the actual connection to the QueueManager; I also left out the try/catch/finally for less clutter) (这里没有显示makeConnection ,它是与 QueueManager 建立实际连接的函数;我也省略了 try/catch/finally 以减少混乱)

How do I get ClusterName and perhaps other data, that doesn't have a function like queue.getMaximumDepth() ?我如何获取ClusterName和其他数据,它没有像queue.getMaximumDepth()这样的函数?

There are two ways to get information about a queue.有两种方法可以获取有关队列的信息。

The API Inquire call gets operational status of a queue. API Inquire 调用获取队列的操作状态。 This includes things like the name the MQOpen call resolved to or the depth if the queue is local.这包括诸如MQOpen调用解析到的名称或队列(如果队列是本地的)的深度之类的内容。 Much of the q.inquire functionality has been superseded with getter and setter functions on the queue.大多数q.inquire功能已被队列中的 getter 和 setter 函数取代。 If you are not using the v8.0 client with the latest functionality, you are highly advised to upgrade.如果您使用的不是具有最新功能的v8.0 客户端,强烈建议您升级。 It can access all versions of QMgr.它可以访问 QMgr 的所有版本。

The following code is from Getting and setting attribute values in WebSphere MQ classes for Java以下代码来自在 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));

For things that are not part of the API Inquire call, a PCF command is needed.对于不属于 API Inquire 调用的内容,需要一个 PCF 命令。 Programmable Command Format, commonly abbreviated as PCF, is a message format used to pass messages to the command queue and for reading messages from the command queue, event queues and others.可编程命令格式,通常缩写为 PCF,是一种消息格式,用于将消息传递到命令队列以及从命令队列、事件队列等读取消息。

To use a PCF command the calling application must be authorized with +put on SYSTEM.ADMIN.COMMAND.QUEUE and for +dsp on the object being inquired upon.要使用 PCF 命令,调用应用程序必须通过SYSTEM.ADMIN.COMMAND.QUEUE上的+put以及被查询对象上的+dsp获得授权。

IBM provides sample code. IBM 提供了示例代码。
On Windows, please see: %MQ_FILE_PATH%\\Tools\\pcf\\samples在 Windows 上,请参见: %MQ_FILE_PATH%\\Tools\\pcf\\samples
In UNIX flavors, please see: /opt/mqm/samp/pcf/samples在 UNIX 版本中,请参阅: /opt/mqm/samp/pcf/samples
The locations may vary depending on where MQ was installed.位置可能因 MQ 的安装位置而异。

Please see: Handling PCF messages with IBM MQ classes for Java .请参阅: 使用 Java 的 IBM MQ 类处理 PCF 消息 The following snippet is from the PCF_DisplayActiveLocalQueues.java sample program.以下代码片段来自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;
  }
}

After more research I finally found what I was looking for.经过更多的研究,我终于找到了我想要的东西。 This example of IBM: Getting and setting attribute values in WebSphere MQ classes helped me to set up the inquiry. IBM 的这个示例: 在 WebSphere MQ 类中获取和设置属性值帮助我设置查询。

The necessary values I found in this list: Constant Field Values .我在此列表中找到的必要值: Constant Field Values

I also needed to expand the openOptionsArg of accessQueue() , else cluster queues cannot be inquired.我还需要扩大openOptionsArgaccessQueue()否则群集队列无法查询。

Final result: (without makeConnection() )最终结果:(没有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;
  }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM