MainframeSupports
tip week 46/2009:

MQ is a brilliant system for sending messages from one system to another. After the introduction of MQ to the customers only a fraction of a second went by before the customers started sending replies back. In reality most installations use MQ for remote procedure calls with all the problems that follow, because the caller and the called part operates as two separate units of work for the same transction.

Another aspect of the use of MQ for remote procedure calls is the timeout challenge. The program issuing the remote procedure call cannot wait forever for an answer. Therefore everyone sets up a maximum waiting time. When the waiting time has expired the issuer of the call continues independent of whether the reply turns up or not. If the reply shows up later it will fill up in the reply queue. If it is important for the application to process a delayed reply you will have to code some kind of post processing. If it is less important (maybe a query which might be repeated) the reply must be removed to avoid the reply queue from running full. Actually MQ can clean up a reply queue automatically and that is what this tip is all about.

In the message descriptor (the MQMD area) there is a field named MQMD-EXPIRY (if you prefer programming languages using underscores please replace the dash with an underscore). The default value for MQMD-EXPIRY is the value of the MQ constant MQEI-UNLIMITED which contains the value -1 (high values). This value means that the message will stay in the queue until someone MQGET's it. You may assign MQMD-EXPIRY a positive integer value indicating the number of one tenth of a second you want the message to stay alive in. If you assign a value of zero to MQMD-EXPIRY the MQPUT or MQPUT1 fails.

Please assume that you do not want to wait more than 10 seconds for the reply from your remote procedure call and that a delayed answer does not need to be post processed. Then you can set MQMD-EXPIRY to 100 on your request message (the one you send to the remote system) and the remote system can send back the reply using the same MQMD-EXPIRY value. The result is that delayed messages simply disappears automatically. That is highly useful. The important part is to set MQMD-EXPIRY to the same value as the waiting time. Please be aware that MQ waiting time is in hundredth of a second and not in tenth which is quite confusing.

When you issue MQGET the returned value of the field MQMD-EXPIRY will no longer contain the original value present when the MQPUT was issued. MQMD-EXPIRY is counted down by MQ on its way from sender to receiver. If you know that the sender sets MQMD-EXPIRY to 100 and it contains 70 when you MQGET the message it has been underway for app. three seconds. You can use the the MQMD-EXPIRY value from the MQGET in the reply message which will give you the most optimal automatic cleanup, because the two messages together do not need to live more than 10 seconds. Another advantage of this approach is that the remote system does not need to know the waiting time of the caller.

Previous tip in english        Sidste danske tip        Tip list