Using MQ Batch Toolkit to Clear Many Queues

Here is another simple script that uses MQ Batch Toolkit (MQBT) to clear hundreds of queues at the same time.

Let’s say you have an MQ sandbox or development queue manager that has hundreds of queues that need to be cleared nightly. If all of the queue names have the same high-level qualifier, then the following Windows batch file (i.e. ClearQ.bat) will quickly take care of the job.

Here is the Windows batch file called ClearQ.bat:

@echo off
setlocal

if [%1]==[] echo Queue Manager Profile was not specified && goto Usage
if [%2]==[] echo Queue Name was not specified && goto Usage

cd /D C:\Capitalware\MQBT\
mqbt.exe QLIST -p %1 -k %2 -t L -f qdepth.txt -D
FOR /F "tokens=1,2" %%A in (qdepth.txt) DO (

   if %%B GTR 0 (
      mqbt.exe ClearQ -p %1 -q %%A
   )
)
del qdepth.txt
goto :DONE

:Usage
echo Usage: %0 QMgr_Profile_Name Queue_Name
goto :DONE

:DONE
endlocal

Breakdown of the script:

    The script expects 2 parameters:

  • The MQBT queue manager profile name
  • The mask or wildcard for queue names

i.e.

ClearQ.bat MQWT1 ABC.*

Line # 8 issues the QDepth function to retrieve the depth of the queue and writes it to a file called ‘qdepth.txt’.

Line # 9 loops over the file ‘qdepth.txt’ and tokenizes the values of each line.

Line # 11 checks if current queue depth is greater than the zero, if true then issue MQBT ClearQ function is executed

Note: MQBT ClearQ function has some smarts built into it. If the queue’s IPPROCs and OPPROCs (open input & output queue handles) are both zero and the Command Server is running, then MQBT will issue a PCF CLEARQ command. Otherwise, MQBT will destructively get all messages from the queue. Therefore, MQBT will make sure all messages are removed from the queue one way or another.

Hopefully, this blog posting will give people ideas on how to extend the use of MQBT.

Regards,
Roger Lacroix
Capitalware Inc.

This entry was posted in Capitalware, IBM i (OS/400), IBM MQ, IBM MQ Appliance, Java, Linux, macOS (Mac OS X), MQ Batch Toolkit, Unix, Windows.

Comments are closed.