29
Fri, Nov
0 New Articles

The API Corner: Monitoring a Job Queue from an RPG Application Program

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

Today, we'll look at using the Job Notification Exit Point.

 

In last month's column, "Monitoring a Job Queue," we looked at a CL program that, via data queue messages sent by the system, enabled us to become aware of newly submitted jobs to a job queue and automate the release of those jobs. This month, we'll provide that level of automation, and a bit more, using an RPG program.

If you haven't recently reviewed last month's article, you may want to do so now as this article assumes that you're familiar with the content previously covered (for instance, the layout of the data queue message sent by the system and the definition of the subsystem being used).

Below is an RPG implementation of last month's CL program.

h dftactgrp(*no)                                                      

                                                                      

d RcvDtaQMsg     pr                 extpgm('QRCVDTAQ')              

d DtaQName                     10a   const                            

d DtaQLib                     10a   const                            

d LenDtaRcvd                   5p 0                                  

d RcvVar                       1a   options(*varsize)                

d WaitTime                     5p 0 const                            

d KeyOrder                     2a   const options(*nopass)          

d LenKey                       3p 0 const options(*nopass)          

d Key                       4096a   options(*varsize :*nopass)      

d LenSndrInfo                   3p 0 const options(*nopass)          

d SndrInfo                  4096a   options(*varsize :*nopass)      

d RmvMsg                       10a   const options(*nopass)          

d LenRcvVar                     5p 0 const options(*nopass)          

d ErrCde                             likeds(QUSEC) options(*nopass)  

                                                        

d RunCmd         pr                 extpgm('QCAPCMD')        

d Cmd                       4096a   const options(*varsize)          

d LenCmd                      10i 0 const                  

d CtlBlck                   4096a   const options(*varsize)

d LenCtlBlck                   10i 0 const                  

d FmtCtlBlck                   8a   const                  

d ChgdCmd                      1a   options(*varsize)      

d LenAvlChgdCmd               10i 0 const                  

d LenRtnChgdCmd               10i 0                        

d ErrCde                             likeds(QUSEC)          

                                                              

d ErrCde         ds                 qualified              

d Hdr                               likeds(QUSEC)          

d MsgDta                     256a                          

                                                              

d Cmd             s           4096a                          

d Key             s             4a   inz('0004')            

d LenMsg         s             5p 0                        

d NotUsedChr    s             1a                          

d NotUsedInt     s             10i 0                        

                                                              

/copy qsysinc/qrpglesrc,ejobntfy                            

/copy qsysinc/qrpglesrc,qcapcmd                          

/copy qsysinc/qrpglesrc,qusec                            

                                                          

/free                                                    

                                                          

RcvDtaQMsg('MYDTAQ' :'BVINING' :LenMsg :EJOQJQN :-1    

             :'EQ' :%size(Key) :Key :0 :NotUsedChr        

             :'*YES' :%size(EJOQJQN) :QUSEC);            

                                                          

dow EJOMI00 <> '*STOP';                                

     if ((EJOMI00 = '*JOBNOTIFY') and                    

         (EJOMF00 = '02') and                            

         (EJOQJQN00 = 'MYJOBQ   BVINING'));            

                                                         

         Cmd = 'RlsJob Job(' +                            

               %subst(EJOQJN00 :21 :6) + '/' +            

               %trimr(%subst(EJOQJN00 :11 :10)) + '/' +  

               %trimr(%subst(EJOQJN00 :1 :10)) + ')';    

                                                          

         RunCmd(Cmd :%len(%trimr(Cmd))                    

               :QCAP0100 :%size(QCAP0100) :'CPOP0100'    

               :NotUsedChr :0 :NotUsedInt :ErrCde);      

     endif;                                              

                                                          

     RcvDtaQMsg('MYDTAQ' :'BVINING' :LenMsg :EJOQJQN :-1

                 :'EQ' :%size(Key) :Key :0 :NotUsedChr    

                 :'*YES' :%size(EJOQJQN) :QUSEC);        

enddo;                                                  

                                                          

*inlr = *on;                                            

return;                                                

                                                          

begsr *inzsr;                                          

                                                          

   QUSBPrv = 0;                                          

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

                                                          

   QCAP0100 = *loval;                                    

   QCACMDPT = 0;                                          

   QCABCSDH = '0';                                      

   QCAPA = '0';      

   QCACMDSS = '0';  

                      

endsr;              

                      

/end-free            

The major changes from last month's CL version of the program, other than adding prototypes for the APIs Receive Data Queue (QRCVDTAQ) and Process Commands (QCAPCMD), are:

  • Using the IBM-provided definition of the data queue message sent by the Job Notification Exit Point rather than keying in our own definition (the structure &DtaQMsg in last month's column). This is accomplished by the statement "/copy qsysinc/qrpglesrc,ejobntfy" and does require changing the names of several variables associated with the data queue messages being received (relative to the CL variable names).

  • Using Optional Parameter Group 2 of the Receive Data Queue API to control the amount of information returned by the QRCVDTAQ API. The use of the Size of data receiver parameter (parameter 12 of the API) allows us to specify the allocated size of the Data parameter (parameter 4).

By default, the QRCVDTAQ API returns to the Data parameter the actual number of bytes defined by the message being received. This is fine as long as the message being received fits into the variable associated with the Data parameter, but it can introduce problems down the road. That is, if the actual message is 512 bytes in length and the Data parameter is using a variable with a length of 144 bytes, then the API will, by default, return the full 512 bytes associated with the message. The first 144 bytes of the message will go to the variable associated with the Data parameter (the allocated size of variable &DtaQMsg in the CL program), and the remaining 368 bytes will go "somewhere." This "somewhere" is why the API documentation includes the text "unexpected results can occur."

Last month's CL program could have made use of the Size of data receiver parameter by hard coding the length of the %DtaQMsg structure, but I elected not to for two reasons: first, I hate hard coding length values in multiple locations (in this case, the DCLed LEN of &DtaQMsg and the value passed for the Size of data receiver parameter) as I always seem to "miss one" when needing to later update the code, and second, the MAXLEN value used when creating the data queue ensured that an allocated length of 144 bytes would not be exceeded (unless of course someone in the future recreates the data queue with a larger MAXLEN). In the case of RPG, the %size built-in provides a very flexible way to specify the length of the variable associated with the Data parameter, so I'm using it. In the case of RPG, and the %size built-in, I don't have to worry about making multiple updates to the source in order to accommodate size allocation changes to a variable.

Note that when calling the QCAPCMD API, to run the RLSJOB command, the data structure ErrCde is being used for the error code parameter. The ErrCde data structure Bytes provided field, QUSBPrv, is set to a non-0 value in the *inzsr subroutine so any errors encountered when running the RLSJOB command will not cause an exception (escape message) to be sent. This use of ErrCde mimics my intent (though not some of the side effects) with the MONMSG MSGID(CPF0000) found in last month's CL version of SBMDJOBSnamely, that errors such as the job being submitted in a released state or the job not being found (possibly due to having already completed) do not stop our program.

This use of ErrCde can be contrasted with the use of QUSEC as the error code parameter value when calling the QRCVDTAQ API. The Bytes provided field of QUSEC is set to 0 in the *inzsr subroutine so that any error encountered when attempting to receive a data queue message will result in an escape message. This mimics the lack of an error code parameter being specified in last month's CL program and seems appropriate to me as an error in receiving the data queue message indicates a real problem (most likely of a configuration nature) that is outside the scope of this example program.

Assuming that the previous RPG source is in member SBMDJOBS (Submitted Jobs) of source file QRPGLESRC, you can create the program into library BVINING using this command:

CRTBNDRPG PGM(BVINING/SBMDJOBS)

We now have an RPG instance of SBMDJOBS functionally equivalent to last month's CL version. To test our new program, see the earlier article "Monitoring a Job Queue."

It was mentioned last month that the Job Notification Exit Point support is very much driven by active subsystems and that if the MYSBS subsystem is not active, then no messages will be sent to the MYDTAQ data queue. Jobs submitted to MYJOBQ, while the subsystem is not active, will simply sit forever on the job queue, in a held status, if we simply stop with the current program logic. What we need to do is determine if, when the subsystem starts, any jobs are currently held on the job queue BVINING/MYJOBQ.

We can accomplish this several ways, and the approach we'll take today is to call the Retrieve Job Queue Information(QSPRJOBQ) API. The QSPRJOBQ API currently supports two formats, and at first blush it may appear that format JOBQ0100 and the field Number of jobs meet our needs. The Number of jobs field does indeed provide the number of jobs in the job queue, but there are several reasons, besides a job being held, that non-0 value might be returned. Two that come to mind are:

  • The job queue itself, rather than the jobs on the job queue, might be held.
  • Individual jobs may have been already released when the subsystem was previously active but unable to start due to the MAXACT and/or MAXPTYx values associated with the job queue entry within MYSBS, prior to the previous ending of the subsystem.

What we really want is the number of individually held jobs on the job queue, and this information can be found in format JOBQ0200 of the API. The following changes (shown in bold) to SBMDJOBS enables us to determine whether there are any individually held jobs in job queue BVINING/MYJOBQ.

h dftactgrp(*no)                                                    

                                                                    

d RcvDtaQMsg     pr                 extpgm('QRCVDTAQ')            

d DtaQName                     10a   const                        

d DtaQLib                     10a   const                        

d LenDtaRcvd                   5p 0                              

d RcvVar                       1a   options(*varsize)            

d WaitTime                     5p 0 const                        

d KeyOrder                     2a   const options(*nopass)        

d LenKey                       3p 0 const options(*nopass)        

d Key                       4096a   options(*varsize :*nopass)    

d LenSndrInfo                   3p 0 const options(*nopass)        

d SndrInfo                   4096a   options(*varsize :*nopass)    

d RmvMsg                       10a   const options(*nopass)        

d LenRcvVar                     5p 0 const options(*nopass)        

d ErrCde                             likeds(QUSEC) options(*nopass)

                                                                    

d RtvJobQI       pr                 extpgm('QSPRJOBQ')            

d RcvVar                       1a   options(*varsize)            

d LenRcvVar                   10i 0 const                    

d FmtRcvVar                     8a   const                    

d QualJobQ                     20a   const                    

d ErrCde                             likeds(QUSEC)            

                                                              

d RunCmd         pr                 extpgm('QCAPCMD')        

d Cmd                       4096a   const options(*varsize)  

d LenCmd                       10i 0 const                    

d CtlBlck                   4096a   const options(*varsize)  

d LenCtlBlck                   10i 0 const                    

d FmtCtlBlck                    8a   const                    

d ChgdCmd                       1a   options(*varsize)        

d LenAvlChgdCmd               10i 0 const                    

d LenRtnChgdCmd               10i 0                          

d ErrCde                            likeds(QUSEC)            

                                                              

d ErrCde         ds                 qualified                

d Hdr                               likeds(QUSEC)            

d MsgDta                      256a                            

                                                              

d Cmd             s           4096a                    

d Key             s             4a   inz('0004')      

d LenMsg         s             5p 0                  

d NotUsedChr     s             1a                    

d NotUsedInt     s             10i 0                  

                                                      

/copy qsysinc/qrpglesrc,ejobntfy                      

/copy qsysinc/qrpglesrc,qcapcmd                      

/copy qsysinc/qrpglesrc,qsprjobq                      

/copy qsysinc/qrpglesrc,qusec                        

                                                      

/free                                                  

                                                      

RcvDtaQMsg('MYDTAQ' :'BVINING' :LenMsg :EJOQJQN :-1  

             :'EQ' :%size(Key) :Key :0 :NotUsedChr    

             :'*YES' :%size(EJOQJQN) :QUSEC);          

                                                      

dow EJOMI00 <> '*STOP';                              

     if ((EJOMI00 = '*JOBNOTIFY') and                

         (EJOMF00 = '02') and                        

         (EJOQJQN00 = 'MYJOBQ  BVINING'));              

                                                          

         Cmd = 'RlsJob Job(' +                            

               %subst(EJOQJN00 :21 :6) + '/' +            

               %trimr(%subst(EJOQJN00 :11 :10)) + '/' +    

               %trimr(%subst(EJOQJN00 :1 :10)) + ')';      

                                                          

         RunCmd(Cmd :%len(%trimr(Cmd))                    

               :QCAP0100 :%size(QCAP0100) :'CPOP0100'    

               :NotUsedChr :0 :NotUsedInt :ErrCde);      

     endif;                                              

                                                          

     RcvDtaQMsg('MYDTAQ' :'BVINING' :LenMsg :EJOQJQN :-1  

                :'EQ' :%size(Key) :Key :0 :NotUsedChr    

                 :'*YES' :%size(EJOQJQN) :QUSEC);          

enddo;                                                  

                                                          

*inlr = *on;                                              

return;                                                  

                                                          

begsr *inzsr;                                        

                                                        

   QUSBPrv = 0;                                        

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

                                                        

   QCAP0100 = *loval;                                  

   QCACMDPT = 0;                                      

   QCABCSDH = '0';                                    

   QCAPA = '0';                                        

   QCACMDSS = '0';                                    

                                                        

   RtvJobQI(QSPQ020000 :%size(QSPQ020000) :'JOBQ0200'  

             :'MYJOBQ   BVINING' :QUSEC);              

                                                        

   if ((QSPJOQP001 > 0) or                              

       (QSPJOQP101 > 0) or                            

       (QSPJOQP201 > 0) or                            

       (QSPJOQP301 > 0) or                            

       (QSPJOQP401 > 0) or                            

       (QSPJOQP501 > 0) or                            

       (QSPJOQP601 > 0) or                

       (QSPJOQP701 > 0) or                

       (QSPJOQP801 > 0) or                

       (QSPJOQP901 > 0));                  

       // We have jobs in held status      

                                            

   endif;                                  

                                            

endsr;                                    

                                            

/end-free                                  

As in the case of using an error code parameter of QUSEC when calling QRCVDTAQ, QUSEC is also being used when calling QSPRJOBQ as the inability to access the job queue is a problem beyond the scope of our example program.

Assuming that you're continuing to use the previous RPG source member of SBMDJOBS in source file QRPGLESRC, you can create the program into library BVINING using this command:

CRTBNDRPG PGM(BVINING/SBMDJOBS)

Having determined that there are indeed held jobs on the job queue, next month we'll look at another API, Open List of Jobs (QGYOLJOB), which can be used to find the specific jobs that are on hold in job queue BVINING/MYJOBQ. Related to this, we'll also be using several other APIs that support the processing of open lists.

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.. I'll see what I can do about answering your burning questions in future columns.

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

$

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: