mq java 怎么判断队列为空
发布网友
发布时间:2022-04-26 15:12
我来回答
共1个回答
热心网友
时间:2023-10-11 04:43
MQException
该类包含WebSphere MQ 完成代码和错误代码常量的定义。以MQCC_开始的常量是WebSphere MQ 完成代码,而以MQRC_开始的常量则是WebSphere MQ 原因代码。只要出现WebSphere MQ
错误,就会给出MQException。
MQGetMessageOptions
该类包含控制MQQueue.get()方法行为的选项。
MQManagedObject
该类是MQQueueManager、MQQueue 和MQProcess 类的超类。它提供查询并设置这些资源属性的能力。
------解决方案--------------------
去取一次,得到 2033 错误就是没有消息符合你的条件。
使用 PCF 查询队列资料:
/**
* @return current depth of queue connected currently.
* @throws Exception
*/
public QueueInfo queryQueueInfo() throws Exception {
if (!checkStatus2(this.queueManager)) {
throw new IllegalStateException("Not Connected to queue manager.");
}
PCFMessageAgent agent = null;
try {
agent = new PCFMessageAgent(this.queueManager);
// Inquiry Queue Name & Current Depth.
int[] attrs = {
CMQC.MQCA_Q_NAME, CMQC.MQIA_CURRENT_Q_DEPTH,
CMQC.MQIA_OPEN_INPUT_COUNT, CMQC.MQIA_OPEN_OUTPUT_COUNT,
CMQC.MQIA_Q_TYPE, CMQC.MQIA_DEFINITION_TYPE, CMQC.MQIA_INHIBIT_GET,
CMQC.MQIA_INHIBIT_PUT };
PCFParameter[] parameters = {
new MQCFST(CMQC.MQCA_Q_NAME , getInputQueue().getText().trim()),
new MQCFIL(CMQCFC.MQIACF_Q_ATTRS , attrs) };
// logger.log("Querying current depth of current queue.");
MQMessage[] responses = agent.send(CMQCFC.MQCMD_INQUIRE_Q, parameters);
QueueInfo info = new QueueInfo();
for (int i = 0; i < responses.length; i++) {
MQCFH cfh = new MQCFH(responses[i]);
// Check the PCF header (MQCFH) in the response message
if (cfh.reason == 0) {
String name = "";
Integer depth = new Integer(0);
for (int j = 0; j < cfh.parameterCount; j++) { // Extract what we want from the returned attributes
PCFParameter p = PCFParameter.nextParameter(responses[i]);
switch (p.getParameter()) {
case CMQC.MQCA_Q_NAME:
name = (String) p.getValue();
info.name = name;
break;
case CMQC.MQIA_CURRENT_Q_DEPTH:
depth = (Integer) p.getValue();
info.depth = depth.intValue();
break;
case CMQC.MQIA_OPEN_INPUT_COUNT:
Integer inputCount = (Integer) p.getValue();
info.inputCount = inputCount.intValue();
break;
case CMQC.MQIA_OPEN_OUTPUT_COUNT:
Integer outputCount = (Integer) p.getValue();
info.outputCount = outputCount.intValue();
break;
case CMQC.MQIA_Q_TYPE:
info.type = ((Integer) p.getValue()).intValue();
break;
case CMQC.MQIA_DEFINITION_TYPE:
info.definitionType = ((Integer) p.getValue()).intValue();
break;
case CMQC.MQIA_INHIBIT_PUT:
info.putNotAllowed = ((Integer) p.getValue()).intValue() == 1;
break; case CMQC.MQIA_INHIBIT_GET:
info.getNotAllowed = ((Integer) p.getValue()).intValue() == 1;
default:
}
}
// System.out.println("Queue " + name + " curdepth " + depth);
return info;
} else {
System.out.println("PCF error:\n" + cfh);
// Walk through the returned parameters describing the error
for (int j = 0; j < cfh.parameterCount; j++) {
System.out.println(PCFParameter.nextParameter(responses[0]));
}
throw new Exception("PCF Error [reason :" + cfh.reason + "]");
}
}
return null;
} catch (Exception e) {
throw e;
} finally {
if (agent != null) {
try {
agent.disconnect();
} catch (Exception e) {
logger.log(e);
}
}
}