25
Thu, Apr
0 New Articles

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

APIs
Typography
  • 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                                     

                                                                    

 ****************************************************************  

                                                                    

 /free                                                             

                                                                    

  monitor;                                                         

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

              :(DtaQName + DtaQLib));                              

  on-error;                                                        

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

    *inlr = *on;                                         

    return;                                             

  endmon;                                               

                                                         

  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';                

                                                                   

     other;                                                       

          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';                        

       else;

           QMHType = 'L';                        

       endif;

        QMHNTBR = 0;                                              

        RtvDQMsg(DQMsgHdr :%size(DQMsgHdr) :'RDQM0100'            

                 :(QMHDQN + QMHDQLib)                        

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

                 :ErrCde);                                   

                                                              

     else;                                                   

        // 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'      

                 :ErrCde);                                   

     endif;                                                  

                                                              

     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');                                         

       else;

          dsply 'Message has been processed';               

        endif;                                         

     else;                                                          

        dsply ('Error accessing msg: ' + ErrCde.Hdr.QUSEI;   

     endif;                                                          

  endif;                                                            

                                                                     

  *inlr = *on;                                                       

  return;                                                           

                                                                     

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

                                            

  begsr *inzsr;                            

                                            

    QUSBPrv = 0;                           

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

                                            

  endsr;                                   

                                            

 /end-free                                 

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..

Bruce Vining

Bruce Vining is president and co-founder of Bruce Vining Services, LLC, a firm providing contract programming and consulting services to the System i community. He began his career in 1979 as an IBM Systems Engineer in St. Louis, Missouri, and then transferred to Rochester, Minnesota, in 1985, where he continues to reside. From 1992 until leaving IBM in 2007, Bruce was a member of the System Design Control Group responsible for OS/400 and i5/OS areas such as System APIs, Globalization, and Software Serviceability. He is also the designer of Control Language for Files (CLF).A frequent speaker and writer, Bruce can be reached at This email address is being protected from spambots. You need JavaScript enabled to view it.. 


MC Press books written by Bruce Vining available now on the MC Press Bookstore.

IBM System i APIs at Work IBM System i APIs at Work
Leverage the power of APIs with this definitive resource.
List Price $89.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: