使用Java获取MQ队列的ClusterName
我正在构建一个连接到MQQueueManager并提取队列信息的java应用程序。我能够获得诸如使用Java获取MQ队列的ClusterName,java,ibm-mq,Java,Ibm Mq,我正在构建一个连接到MQQueueManager并提取队列信息的java应用程序。我能够获得诸如QueueType、MaximumMessageLength等数据。但是,我还需要队列可能所在的集群的名称。MQQueue中没有提供此信息的函数。在搜索互联网后,我发现了一些指向这个方向的东西,但没有任何例子 我的函数中提供最大深度的一部分是: queueManager = makeConnection(host, portNo, qMgr, channelName); queue =
QueueType
、MaximumMessageLength
等数据。但是,我还需要队列可能所在的集群的名称。MQQueue中没有提供此信息的函数。在搜索互联网后,我发现了一些指向这个方向的东西,但没有任何例子
我的函数中提供最大深度的一部分是:
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 Inquire调用获取队列的操作状态。这包括MQOpen
调用解析到的名称或队列为本地时的深度。队列中的大部分q.inquire
功能已被getter和setter函数取代。如果您未使用具有最新功能的,强烈建议您升级。它可以访问QMgr的所有版本
以下代码来自
对于不属于API Inquire调用的内容,需要使用PCF命令。可编程命令格式(通常缩写为PCF)是一种消息格式,用于将消息传递到命令队列,以及从命令队列、事件队列和其他队列读取消息
要使用PCF命令,调用应用程序必须在系统管理命令队列
上获得+put
的授权,并在被查询对象上获得+dsp
的授权
IBM提供了示例代码。
在Windows上,请参阅:%MQ\u文件\u路径%\Tools\pcf\samples
在UNIX版本中,请参见:/opt/mqm/samp/pcf/samples
位置可能因安装MQ的位置而异
请参阅:。以下代码段来自PCF\u 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;
}
}
public static void DisplayActiveLocalQueues(PCF_CommonMethods pcfCM)抛出PCFEException,
MQDataException,IOException{
//为查询创建PCF消息类型。
PCFMessage pcfCmd=新的PCFMessage(MQConstants.MQCMD_INQUIRE_Q);
//添加查询规则。
//队列名称=通配符。
pcfCmd.addParameter(MQConstants.MQCA_Q_NAME,“*”);
//队列类型=本地。
pcfCmd.addParameter(MQConstants.MQIA_Q_类型,MQConstants.MQQT_本地);
//队列深度过滤器=“其中深度>0”。
pcfCmd.addFilterParameter(MQConstants.MQIA_CURRENT_Q_DEPTH,MQConstants.MQCFOP_GREATER,0);
//执行命令。返回的对象是PCF消息数组。
PCFMessage[]pcfResponse=pcfCM.agent.send(pcfCmd);
//对于每个返回的消息,从数组中提取消息并显示
//所需信息。
System.out.println(“+----+-----------------------------------------------------------------------------------------------++”);
System.out.println(“|Index | Queue Name | Depth |”);
System.out.println(“+----+-----------------------------------------------------------------------------------------------++”);
对于(int index=0;index
经过更多的研究,我终于找到了我想要的东西。
IBM的这个例子:帮助我建立了查询
我在此列表中找到的必要值:
我还需要扩展accessQueue()
的openOptionsArg
,否则无法查询集群队列
最终结果:
(不带makeConnection()
)
非常感谢。我在询问电话中找到了信息,但我无法让它工作。你也有这样的例子吗?查看%MQ\u FILE\u PATH%\Tools\soap\samples\java\clients\RunIvt.java
我找不到可以在这个例子中使用的东西;查询队列的唯一部分是在最后一个类中,它使用标准函数(q.getOpenInputCount()
),但可能我没有正确查找。对于您的查询,请不要硬编码值,而是使用MQ定义的值。i、 e.CMQC.MQIA_DEF_PRIORITY、CMQC.MQCA_Q_DESC和CMQC.MQ_Q_DESC u length请不要硬编码值,而是使用MQ定义的值。i、 e.CMQC.MQ_CLUSTER_NAME_LENGTH和CMQC.MQCA_CLUSTER_NAME同样,您应该将queue.close()包装在try/catch中,因为如果它引发异常,则断开连接将永远不会被调用。当然,断开连接应该包装在try/catch中,以控制断开连接的任何故障。
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;
}
}
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;
}
}