The API Corner: What's the Status of My Data Queue?

  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Find out by using the Retrieve Data Queue APIs.


In the past month or so, I've received two requests related to accessing data queue (*DTAQ) information. The first request concerned finding out how many messages were currently on a *DTAQ and how long the oldest message had been on the queue. These questions, to me anyway, suggest a management application to periodically poll *DTAQs and determine whether they are backlogged and in need of additional readers to be started and/or are stuck for some reason, for instance an outstanding inquiry message.


The second request was related to finding out the size supported for a given *DTAQ, suggesting a configuration check to determine if *DTAQs are perhaps configured too small. I will admit this request came to me in the form of a CL question, but I see no reason not to include the answer within the context of an RPG solution (in addition to having sent a CL solution back to the individual earlier).


In this article, we'll look at how to answer all of these questions, and more, using the Retrieve Data Queue Description (QMHQRDQD) and Retrieve Data Queue Message (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
  • 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.


h dftactgrp(*no)                                                     


d GetDQInfo       pr                                                 

d  DtaQName                     10a   const                          

d  DtaQLib                      10a   const                          


d GetDQInfo       pi                                                 

d  DtaQName                     10a   const                          

d  DtaQLib                      10a   const                          




d CvtDatTim       pr                  extpgm('QWCCVTDT')             

d  InpFmt                       10a   const                          

d  InpValue                     20a   const options(*varsize)        

d  OutFmt                       10a   const                          

d  OutValue                     20a   options(*varsize)              

d  ErrCde                             likeds(QUSEC)                  

d  InpTZ                        10a   const options(*nopass)         

d  OutTZ                        10a   const options(*nopass)   

d  TZInfo                        1a   const options(*nopass)   

d  LenTZInfo                    10i 0 const options(*nopass)   

d  PrecInd                       1a   const options(*nopass)   

d  InpTimInd                     1a   const options(*nopass)   


d RtvDQInfo       pr                  extpgm('QMHQRDQD')       

d  RcvVar                        1a   options(*varsize)        

d  LenRcvVar                    10i 0 const                    

d  Format                        8a   const                    

d  QualDQName                   20a   const                    


d RtvDQMsg        pr                  extpgm('QMHRDQM')        

d  RcvVar                        1a   options(*varsize)        

d  LenRcvVar                    10i 0 const                    

d  Format                        8a   const                    

d  QualDQName                   20a   const                    

d  MsgFltr                      16a   const                    

d  LenMsgFltr                   10i 0 const                    

d  Format                        8a   const                    

d  ErrCde                             likeds(QUSEC)                   




d DQMsgHdr        ds                  qualified                       

d  Hdr                                likeds(QMHM010002)              

d  MsgDta                     1024a                                   


d DQMsgPtr        s               *                                    

d DQMsg           ds                  likeds(QMHRME)                  

d                                     based(DQMsgPtr)                 


d DQKeyVal        ds                  qualified                       

d  Hdr                                likeds(QMHS0200)                

d  KeyVal                      256a                                   


d ErrCde          ds                  qualified                       

d  Hdr                                likeds(QUSEC)                   

d  MsgDta                      256a                                   




d MsgAge          s             10i 0                               

d MsgTS           s               z                                

d YYMDValue       s             20a                                




 /copy qsysinc/qrpglesrc,qmhqrdqd                                  

 /copy qsysinc/qrpglesrc,qmhrdqm                                   

 /copy qsysinc/qrpglesrc,qusec                                     







    RtvDQInfo(QMHD0100 :%size(QMHD0100) :'RDQD0100'                

              :(DtaQName + DtaQLib));                              


    dsply ('Error accessing *DTAQ, see job log');       

    *inlr = *on;                                         




  if QMHType01 = '1';                                   

    dsply ('DDM *DTAQs are not supported');             

    *inlr = *on;                                        




  dsply ('Results for *DTAQ ' +                         

         %trimr(QMHDQLib) + '/' + %trimr(QMHDQN));      



     when QMHMNES = -1;                                 

          dsply 'Size specified is 16MB';               


     when QMHMNES = -2;                                 

          dsply 'Size specified is 2GB';                



          dsply ('Size specified is ' + %char(QMHMNES) +          

                 ' entries');                                     



  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) :'RDQM0100'            

                 :(QMHDQN + QMHDQLib)                        

                 :QMHS0100 :%size(QMHS0100) :'RDQS0100'       




        // 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) :'RDQM0100'       

                 :(QMHDQN + QMHDQLib)                        

                 :DQKeyVal :%size(DQKeyVal) :'RDQS0200'      




     if ErrCde.Hdr.QUSBAvl = 0;

       if DQMsgHdr.Hdr.QMHNbrMR > 0;                              

           DQMsgPtr = %addr(DQMsgHdr) + DQMsgHdr.Hdr.QMHOFE;           


           CvtDatTim('*DTS' :DQMsg.QMHMDT00                            

                     :'*YYMD' :YYMDValue :ErrCde);                     

           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;   




  *inlr = *on;                                                       



  // ************************************************************   


  begsr *inzsr;                            


    QUSBPrv = 0;                           

    ErrCde.Hdr.QUSBPrv = %size(ErrCde);    





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.

Have Some System API Questions?

As usual, if you have any API questions, send them to me at This email address is being protected from spambots. You need JavaScript enabled to view it..