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.

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$