Reputation: 1970
if [[ $(dspmq | grep '(Running)' | grep "$QMgr" | wc -l | tr -d " ") != 1 ]]
The above code is to check if queue manager is running or not.
Is there any command to check whether the given queue name is exists in the queue manager or not?
Upvotes: 4
Views: 5465
Reputation: 10642
Adding another suggestion in addition to what Rob and T.Rob have said.
MQ v7.1 and higher come with the dmpmqcfg command and you can use this to check for a specific queue.
The examples below are in line with your sample that checks if a queue manager is running:
To use dmpmqcfg to check if a queue name of any type exists you could do this:
if dmpmqcfg -m ${QMgr} -t queue -x object -o 1line -n ${QName}|egrep '^DEFINE '; then
echo "Queue ${QName} exists on Queue Manager ${QMgr}
fi
Using the method Rob Parker provided* to check if a queue name of any type exists:
*Note I used DISPLAY Q(
instead of DISPLAY QLOCAL(
if printf "DISPLAY Q(${QName})" | runmqsc ${QMgr} 2>&1 >/dev/null; then
echo "Queue ${QName} exists on Queue Manager ${QMgr}
fi
Your example check for a queue manager Running could be simplified to this:
if dspmq -m ${QMgr}| grep --quiet '(Running)'; then
echo "Queue Manager ${QMgr} is Running"
fi
Upvotes: 4
Reputation: 31832
In addition to what Rob said, the way to do this programmatically is to attempt to open the queue. If the queue exists you get either RC=0
or RC=2
with a Reason Code of 2035 MQRC_NOT_AUTHORIZED
. If the queue does not exist you get back RC=2
with a Reason Code of 2085 MQRC_UNKNOWN_OBJECT_NAME
.
In the event someone else has that queue open for exclusive input you can't open it for input without getting an error, but at least the error tells you the queue exists. To work around that open the queue for inquiry if all you need is to know it exists. That also lets you discover other attributes about it with the API's inquiry options.
Finally, if you have access to the Command Queue, you can drop a PCF command on it that is equivalent to DIS Q(<QUEUE NAME>)
that Rob mentioned. In general, business applications do not need access to the Command Queue but then again business applications do not normally need to inquire as to whether their queue exists or not. That's an administrative function and the app either finds its queue or throws a fatal error. As an MQ Admin I would question any business application that asked for rights to use runmqsc
or that inquired as to whether its queue was there, its channels were up, etc. Most shops I've worked at would not let a business app into Production with that design or privileges.
On the other hand, instrumentation applications routinely need to be able to inquire on things like queue inventory so would be expected to have access to and use the Command Queue for that function, or have access to runmqsc
to inquire from scripts.
Upvotes: 2
Reputation: 774
There's not a specific command but you could use:
printf "DISPLAY QLOCAL(<QUEUE NAME>)" | runmqsc <QM Name>
You will get a Return Code of 10 if it does not exist and 0 if it does. One thing to note, the Queue Manager must be running and you must run the command as someone who has access to the Queue Manager in question otherwise you'll get different return codes! (20 for Queue Manager not running and not authorized)
Given you haven't specified a specific Queue type i've assumed you're looking for QLocal's but you could search on any Queue Type by modifying the above command.
Upvotes: 3