MQSCX example script: Age of a Queue

There are a number of example MQSCX script command files available from the MQGem website. One of them, age.mqx, allows you to discover which queues with a RETINTVL specified, have been around longer than originally planned. This post will walk through that script file to explain how it works.

Introduction to the Script

The purpose of the age.mqx command file is to calculate the ages of all the local queues. It then compares the age to the RETINTVL setting for the queue. It prints out a message saying how long the queue should remain for or whether it should be deleted. Lastly the script prints out the name of the oldest queue.

Display each Queue

So, we clearly we need to issue a DISPLAY Q(*) type command. Because we wish to know their ages we have to ask for the creation date and time to be returned, which are CRDATE and CRTIME respectively. We also need to know the RETINTVL value so we add that as well.

* Get all of the queues

Calculate the age of each queue

Now, inside the foreach() block we see the line

  @age = _time - mqtime(CRDATE,CRTIME)

This introduces the notion of variables. A variable is just a simple way of storing a value for something. A variable starting with a ‘@’ is known as a user variable. These can be any name that is meaningful to you up to 30 characters. Another type of variable are those starting with ‘_’. These are known as system variables and are provided by MQSCX itself. You cannot change the value of a system variable. Here we see the variable _time being used which, perhaps not surprisingly, is the current time in seconds.

We also see a call to mqtime() which is a function provided by MQSCX to convert a date and time into the number of seconds. By subtracting the queue time from the current time we can therefore determine the queue’s age in seconds. The following line divides this time by the number of seconds in an hour and we therefore have how old the queue is in hours. We also calculate the age of the queue in days.

  * Convert seconds to hours
  @age_hours = @age / (60 * 60)
  * Convert seconds to days
  @age_days  = @age_hours / 24

The following few lines are fairly self-explanatory but it shows that you can have conditional processing. Here we are saying that if the age of this queue is older than any queue we’ve seen so fair we remember its age and name.

if (@age_days > @oldest)
  @oldest  = @age_days
  @oldestq = QUEUE

Check the Retain Interval

if (RETINTVL != 999999999)
  * How long should we retain
  @retain = RETINTVL - @age_hours
  if (@retain > 0)
    print :48:queue,"has",:6p0:@retain,"hours left"
    print :48:queue,"expired by",:p0:-@retain,"hours"

We then check the value of RETINTVL for this queue. If it doesn’t have the ‘infinite’ setting then we check to see how this retention interval compares with the current age of the queue. The queue can either have a certain time to live or it should already have been deleted. We print out a message for whichever of these situations is true for this queue. This print statement uses the formatting option p0. All this does is set the precision of the @retain variable. Since @retain comes from @agehours which is essentially the result of a divide instruction it will be a real number. Something like, 123.45546. This looks rather ugly and the decimal places don’t contribute very much so by specifying a precision of 0 we tell the print instruction not to bother to output the decimal places at all.

The last thing the script does is print out the oldest queue just for good measure.

print "The oldest queue is",@oldestq,"at",:p0:@oldest,"days."

The best way to understand scripts is of course to have a go with them yourself. There are various examples in the download, so why not try them out yourself. If you are not currently an MQSCX licence holder, you may email to request a trial licence.


One thought on “MQSCX example script: Age of a Queue

  1. Pingback: MQSCX example script: Age of a Queue | Dinesh Ram Kali.

The team at MQGem would love to hear what you think. Leave your comments here.

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s