Display, View, Debug IBM i Data Queues
Data Queues are a common object type in many applications on IBM i Systems: They offer an easy way to queue up data to be read and processed. Easy to load, easy to read, and an easy way to handle sequential data. The only thing that I really don’t like about them is the tricky way you have to poke around in them to look at their contents. But, I found this neat article that highlights some new IBM i *API’s that change to the old way that data queues are handled… we can now retrieve data queue entries without removing them. Yay!
This allows us to peek at Data Queue contents without upsetting programs that are processing the queue itself…
- Find out how many messages are on a *DTAQ
- How long the oldest message had been on the queue
- Find the size supported for a given *DTAQW
IBM i DTAQ API’s
We’ll look at how to answer all of these questions, and more, using the Retrieve Data Queue Description API (QMHQRDQD) and Retrieve Data Queue Message API (QMHRDQM) APIs.
The Retrieve Data Queue Description API retrieves the description and attributes of a data queue. This information includes:
- Number of entries currently on the queue (one of the asked questions)
- Maximum number of messages that was specified using the SIZE parameter of the Create Data Queue (CRTDTAQ) command (again one of the asked questions)
- If the data queue is accessed first-in first-out (FIFO), last-in first-out (LIFO), or by key values (though not asked, we’ll see that this is a key piece of information in order to determine the age of the oldest message)
- Maximum message and key length supported
- If sender information is included with messages
- Quite a bit more
The Retrieve Data Queue Message API retrieves one or more messages from a data queue. Using the API:
- Messages retrieved are not removed from the *DTAQ, so there is no disruption to other jobs processing/reading from the queue
- You can specify if you want the first or last message on the *DTAQ when working with a non-keyed queue
- The returned message information includes the date and time the message was enqueued (which again is needed to determine the age of a given message)
Using these APIs, the following GetDQInfo program provides the answers to the earlier asked questions:
ctl-opt dftactgrp(no); // Entry parameters into this procedure dcl-pi *n; DtaQName char(10) const; DtaQLib char(10) const; end-pi; dcl-pr CvtDatTim extpgm('QWCCVTDT'); *n char(10) const; // InpFmt *n char(20) const options(*varsize); // InpValue *n char(10) const; // OutFmt *n char(20) options(*varsize); // OutValue *n likeds(qusec); // ErrCde *n char(10) const options(*nopass); // InpTZ *n char(10) const options(*nopass); // OutTZ *n char(1) const options(*nopass); // TZInfo *n int(10) const options(*nopass); // LenTZInfo *n char(1) const options(*nopass); // PrecInd *n char(1) const options(*nopass); // InpTimInd end-pr; dcl-pr RtvDQInfo extpgm('QMHQRDQD'); n char(1) options(*varsize); // RcvVar *n int(10) const; // LenRcvVar *n char(8) const; // Format *n char(20) const; // QualDQName end-pr; dcl-pr RtvDQMsg extpgm('QMHRDQM'); n char(1) options(*varsize); // RcvVar *n int(10) const; // LenRcvVar *n char(8) const; // Format *n char(20) const; // QualDQName *n char(16) const; // MsgFltr *n int(10) const; // LenMsgFltr *n char(8) const; // Format *n likeds(qusec); // ErrCde end-pr; dcl-ds DQMsgHdr qualified; Hdr likeds(qmhm010002); MsgDta char(1024); end-ds; dcl-s DQMsgPtr pointer; dcl-ds DQMsg likeds(qmhrme) based(dqmsgptr); dcl-ds DQKeyVal qualified; Hdr likeds(qmhs0200); KeyVal char(256); end-ds; dcl-ds ErrCde qualified; Hdr likeds(qusec); MsgDta char(256); end-ds; dcl-s MsgAge int(10); dcl-s MsgTS timestamp; dcl-s YYMDValue char(20); /copy qsysinc/qrpglesrc,qmhqrdqd /copy qsysinc/qrpglesrc,qmhrdqm /copy qsysinc/qrpglesrc,qusec monitor; QUSBPrv = 0; ErrCde.Hdr.QUSBPrv = %size(ErrCde); RtvDQInfo ( QMHD0100 : %size(QMHD0100) : 'RDQD0100' : (DtaQName + DtaQLib) ); if QMHType01 = '1'; dsply ('DDM *DTAQs are not supported'); *inlr = *on; return; endif; dsply ('Results for *DTAQ ' + %trimr(QMHDQLib) + '/' + %trimr(QMHDQN)); select; when QMHMNES = -1; dsply 'Size specified is 16MB';
when QMHMNES = -2;
dsply 'Size specified is 2GB';
dsply ('Size specified is ' + %char(QMHMNES) +' entries');endsl; dsply ('Currently contains ' + %char(QMHNbrM) + ' messages'); if QMHNbrM > 0; // If any messages on *DTAQ, get first one if QMHKL = 0; // Non-keyed *DTAQ
if QMHuence = 'F';
QMHType = 'F';
QMHType = 'L';
QMHNTBR = 0;
RtvDQMsg ( DQMsgHdr :%size(DQMsgHdr)
: (QMHDQN + QMHDQLib)
// Keyed *DTAQ
DQKeyVal.Hdr.QMHType00 = 'K';
DQKeyVal.Hdr.QMHSO = 'GE';
DQKeyVal.Hdr.QMHNbrTR = 0;
DQKeyVal.Hdr.QMHNbrKR = 0;
DQKeyVal.Hdr.QMHKL00 = QMHKL;
DQKeyVal.KeyVal = *Allx'00';
RtvDQMsg ( DQMsgHdr :%size(DQMsgHdr)
: (QMHDQN + QMHDQLib)
if ErrCde.Hdr.QUSBAvl = 0;
if DQMsgHdr.Hdr.QMHNbrMR > 0;
DQMsgPtr = %addr(DQMsgHdr) + DQMsgHdr.Hdr.QMHOFE;
CvtDatTim ( '*DTS'
MsgTS = %timestamp(YYMDValue:*ISO0);
MsgAge = %diff(%timestamp():MsgTS:*Seconds);
dsply ('Oldest message is ' + %char(MsgAge) + ' seconds old');
dsply 'Message has been processed';
dsply ('Error accessing msg: ' + ErrCde.Hdr.QUSEI);
endif;endif; on-error; dsply ('Error accessing *DTAQ, see job log'); endmon; *inlr = *on; return;
The Get Data Queue Information (GetDQInfo) program defines two input parameters. These are, respectively, the name of the *DTAQ to be accessed and the name of the library containing the *DTAQ. As the Retrieve Data Queue Description API supports the special values *CURLIB and *LIBL for the library, GetDQInfo will also support them as a freebie.
After setting the API ErrCde structure to not return exceptions in the *inzsr subroutine, GetDQInfo calls the Retrieve Data Queue Description API to access attribute information related to the *DTAQ identified by the two parameters passed to GetDQInfo. As with most retrieve type APIs, the QMHQRDQD API has as its first four parameters a receiver variable to return the attribute information in, the size of the receiver variable, the format of the attribute information to be returned in the receiver variable, and the name of the object to be used. QMHQRDQD is, however, different from most other system APIs in that it does not provide for an error code structure.
This is in part due to the age of the API; it was first introduced in V2R1 while the error code structure was not formalized until V2R2, and for some reason the error code structure was never added as an optional parameter when the API was enhanced in later releases in support of 2GB sizes, relational database name for RDB DDM *DTAQs, etc. In any case, this lack of an error code structure is why GetDQInfo uses a monitor when calling the API.
If an exception is encountered, such as the data queue not being found, then GetDQInfo dsplys a message pointing the caller to the job log and ends. GetDQInfo could of course use message-handling APIs as discussed in a much earlier API Corner article, More on Message Handling, to access the actual message(s) and provide more information than just a pointer to the job log, but today we’re talking about data queue APIs.
If the attribute information was successfully retrieved, then a check is made to see if this is a DDM *DTAQ. If so a message is dsplyed that DDM queues are not supported and the program ends. As provided, the GetDQInfo program needs to run on the system (or against the library) where the actual data queue resides.
GetDQInfo then dsplys a message providing the qualified *DTAQ name of the data queue being analyzed. When constructing this dsply text, the variables GetDQInfo uses are the returned field names of QMHDQLib and QMHDQN rather than the parameter values passed to GetDQInfo. This allows the dsply to have the library name the API resolved to, handling situations where special values such as *CURLIB or *LIBL have been used.
GetDQInfo then dsplys two messages, the first showing the maximum size of the data queue and the second the number of messages currently residing on the queue. If one or more messages do currently exist on the data queue (QMHNbrM > 0) GetDQInfo then prepares to call the Retrieve Data Queue Message API, QMHRDQM.
As with QMHQRDQD, the first four parameters defined for QMHRDQM are the receiver variable to return the message information in, the size of the receiver variable, the format of the message information to be returned in the receiver variable, and the qualified name of the *DTAQ. Following these four initial parameters are four additional parameters. The first three additional parameters have to do with selecting which messages(s) are to be returned, and they are message selection information, the length of the message selection information, and the format of the message selection. Following these parameters is the standard system API error code parameter.
There are two message selection formats defined: RDQS0100 for non-keyed data queues and RDQS0200 for keyed data queues. Using the previously returned QMHQRDQD key-length value, QMHKL, GetDQInfo then prepares the appropriate message selection format requesting that the “oldest” message be retrieved.
When the *DTAQ is non-keyed (QMHKL = 0), GetDQInfo needs to determine if the data queue is FIFO or LIFO. This is done by testing the Sequence attribute (QMHuence) returned by QMHQRDQD. If the sequence is FIFO (QMHuence = ‘F’) then the selection type of RDQS0100 (QMHType) is set to select the first (‘F’) message on the queue. Otherwise, the queue must be LIFO (QMHuence = ‘L’) and the selection type is set to the last (‘L’) message on the queue. As a general heads up, I will point out that a DDM *DTAQ will also return a QMHKL value of 0 and that other QMHuence values do exist (a blank for DDM queues, a ‘K’ for keyed queues). In the case of GetDQInfo, this is not a concern due to earlier checks for DDM (QMHType01 = ‘1’) and keyed queues (QMHKL = 0). Having set QMHType, GetDQInfo then sets the number of message text bytes to retrieve (QMHNTBR) to 0 as we really don’t care about the value of the message; all we want is the day and time the message was written to the data queue. The QMHRDQM API is then called using the previously set selection criteria.
When the *DTAQ is keyed, we really need to have some awareness in the program of how the keys are used and constructed. GetDQInfo, for demonstration purposes, simply assumes that the lowest key value represents the oldest message. With that assumption in mind, GetDQInfo sets the selection type (DQKeyVal.Hdr.QMHType00) to ‘K’ for keyed selection, the key search order (DQKeyVal.Hdr.QMHSO) to greater than or equal to (‘GE’), the number of text bytes to retrieve (DQKeyVal.Hdr.QMHNbrTR) to 0, the number of key bytes to retrieve (DQKeyVal.Hdr.QMHNbrKR) to 0, the length of the key (DQKeyVal.Hdr.QMHKL00) to the value of QMHKL (key length) previously returned by QMHQRDQD, and the key to be used (DQKeyVal.KeyVal) to hex 0s. The QMHRDQM API is then called using the previously set selection criteria.
If no error is encountered when calling QMHRDQM (ErrCde.Hdr.QUSBAvl = 0), a check is made to determine if a message was returned (DQMsgHdr.Hdr.QMHNbrMR > 0). This check is to cover the case where there was at least one message on the *DTAQ when QMHQRDQD was called but the messages have been received/removed by other jobs on the system before we were able to call QMHRDQM to access the message.
If no message was returned by QMHRDQM, then an appropriate message is dsplyed and the program ends.
If a message was returned, GetDQInfo accesses the information using the provided offset to first entry (QDMsgHdr.Hdr.QMHOFE). The date and time that the message was written (DQMsg.QMHMDT00) is returned by the API using an internal system format referred to as *DTS. GetDQInfo converts this *DTS value to a YYYYMMDDHHMMSS format using the Convert Date and Time Format (QWCCVTDT) API. For space reasons, I will not go into this API today, but you may see it again in a future API Corner (in fact I was rather surprised to find that I hadn’t previously written about QWCCVTDT). A calculation of the number of seconds that have elapsed since the time the message was written and the current time is then made, the result is dsplyed, and the program ends.
We now have a general-purpose program that can determine the answers to specific data queue-related questions: what’s the backlog on the queue, what’s the age of the oldest message, and what is the maximum size of the queue? By looking at the API documentation, you’ll find that you can also get the answers to quite a few other unasked questions, perhaps some that you’ve always wondered about.
Source – http://www.mcpressonline.com/apis/the-api-corner-what-s-the-status-of-my-data-queue.html