The API Corner: Moving Jobs Within and Across Job Queues

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

Let's look at what we can do with the Move Job API.

 

Earlier this month, over on Midrange-L, Becky B. wrote in with this:

During month-end we have a large number of user-submitted jobs that automatically go to the QBATCH job queue and hold up other jobs. Is there a way to automatically redirect jobs to a different subsystem once they hit the job queue?

Included was a follow-on note explaining that management was hoping for a silver bullet so that they wouldn't have to change the programs submitting month-end jobs.

One response, from Roger H., was this:

There is a job notification exit point that may be helpful to you QIBM_QWT_JOBNOTIFY. Here's an article by (who else?) Bruce Vining on using it http://www.mcpressonline.com/apis/the-api-corner-monitoring-a-job-queue.html

This month, we'll look at how the QIBM_QWT_JOBNOTIFY exit point, in conjunction with the Move Job (QSPMOVJB) API, might be used. Note, however, that this article does not provide a really good solution to Becky's problem. But it does provide an opportunity to introduce an API (QSPMOVJB) that we have not previously used (and that many are not aware of). Next month, taking a different approach, we'll look at a more-complete solution. Some of the problems with the approach shown this month will be discussed after we review the sample program and API.

The emphasis of this article is on the QSPMOVJB API, so please review the previous articles Monitoring a Job Queue and Monitoring a Job Queue from an RPG Application Program. These articles discuss setting up the environment in which this month's program will run. The one additional step for this month is creating a second *JOBQ, BVINING/JOBQ2. This is the job queue that will be used when we detect a month-end-related job.

The source for the updated SBMDJOBS program follows.

h dftactgrp(*no)                                                   

                                                                    

d MovJob          pr                  extpgm('QSPMOVJB')           

d  FncCtlBlk                  4096a   const options(*varsize)      

d  LenFncCtlBlk                 10i 0 const                         

d  FmtFntCtlBlk                  8a   const                        

d  ErrCde                             likeds(QUSEC)                

                                                                    

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 PrvQJN          ds                                                    

d  PrvJobName                   10a                                     

d  PrvJobUsr                    10a                                     

d  PrvJobNbr                     6a                                     

                                                                         

d ErrCde          ds                  qualified                         

d  Hdr                                likeds(QUSEC)                      

d  MsgDta                      256a                                     

                                                                         

d Key             s              4a   inz('0004')                       

d LenMsg          s              5p 0                                   

d NotUsedChr      s              1a                                     

                                                                         

 /copy qsysinc/qrpglesrc,ejobntfy                                        

 /copy qsysinc/qrpglesrc,qspmovjb                                       

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

          (%subst(EJOQJN00 :1 :6) = 'MTHEND'));        

                                                        

         if PrvJobName <> *blanks;                     

            QSPSJN00 = %subst(EJOQJN00 :1 :10);        

            QSPSJUN00 = %subst(EJOQJN00 :11 :10);      

            QSPSJNbr00 = %subst(EJOQJN00 :21 :6);      

            QSPSIJID00 = *blanks;                      

            QSPTJN = PrvJobName;                       

            QSPTJUN = PrvJobUsr;                       

            QSPTJNbr = PrvJobNbr;                                     

            QSPTIJID = *blanks;                                       

                                                                       

            MovJob(QSPB0200 :%size(QSPB0200) :'MJOB0200' :ErrCde);    

         endif;                                                       

                                                                       

         if (((ErrCde.Hdr.QUSBAvl > 0) and                             

              (ErrCde.Hdr.QUSEI = 'CPF33C1')) or                      

             (PrvJobName = *blanks));                                 

            QSPSJN = %subst(EJOQJN00 :1 :10);                         

            QSPSJUN = %subst(EJOQJN00 :11 :10);                       

            QSPSJNbr = %subst(EJOQJN00 :21 :6);                       

            QSPSIJID = *blanks;                                       

            QSPTJQN = 'JOBQ2';                                         

            QSPTJQL = 'BVINING';                                      

                                                                       

            MovJob(QSPB0100 :%size(QSPB0100) :'MJOB0100' :ErrCde);    

         endif;                                                        

                                                                       

         if ErrCde.Hdr.QUSBAvl = 0;

           PrvQJN = EJOQJN00; 

         endif;                                          

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

    ErrCde.Hdr.QUSBAvl = 0;                                 

                                                             

  endsr;                                                    

                                                             

 /end-free                                                 

 

As with the previous SBMDJOBS program, the program uses the Receive Data Queue (QRCVDTAQ) API to receive a job notification message sent by the job notification exit point and then falls into a DOW to process all messages until a *STOP message is sent by a user on the system. Within the DOW, a fourth check has been added to the initial IF statement. For simplicity, I'm assuming that all month-end jobs that are to be routed to another job queue start with a job name of MTHEND. The new check is to determine if the submitted jobs name has MTHEND as the initial six characters. If so, then SBMDJOBS processes the message.

The Move Job API has two capabilities. It can be used to move a given job to the top of a job queue or to move a given job to being after another job on a job queue. The target job queue of the move can be either the job queue the job to be moved is currently on or a different job queue. SBMDJOBS will make use of both the "move to the top" and "move after" capabilities.

Within the DOW, SBMDJOBS uses the data structure PrvQJN (Previous qualified job name) to keep track of the previous month-end job that was moved. When the program processes the first job notification message, the PrvQJN subfield PrvJobName will be blank and the imbedded "if PrvJobName <> *blanks" will be false, giving control to the second imbedded IF (which has the "or (PrvJobName = *blanks))" check). Within this IF, format MJOB0100 of QSPMOVJB is used. MJOB0100 is the format to move a job to the top of a job queue. This format defines six fields: the name of the job to move, the user of the job to move, the number of the job to move, the internal job ID of the job to move, the name of the job queue the job is to be moved to, and the library of the job queue. SBMDJOBS set the first four fields using the job identification information provided in the job notification message and the last two fields to the job queue name JOBQ2 in library BVINING. The QSPMOVJB API is called and the job moves to the top of JOBQ2. SBMDJOBS then saves the qualified name of the job moved to PrvQJN and waits for the next job notification message.

Processing of the next month-end-related job notification message will find the imbedded "if PrvJobName <> *blanks" test to be true. Within this IF, format MJOB0200 of QSPMOVJB is used. MJOB0200 is the format to move a job to right after another job on a job queue. This format defines eight fields: the name of the job to move, the user of the job to move, the number of the job to move, the internal job ID of the job to move, the name of the job to move after, the user of the job to move after, the number of the job to move after, and the internal job ID of the job to move after. SBMDJOBS sets the first four fields using the job identification information provided in the job notification message and the last four fields using the job identification information found in data structure PrvQJN. The QSPMOVJB API is then called to move the job.

This moving of the job may or may not complete successfully. One reason for it to fail would be if the job identified by PrvQJN was no longer on a job queue (it's currently running or has already completed). In this case, the MJOB0200 call to QSPMOVJB will return an error code structure indicating a failure. Specifically, ErrCde.Hdr.QUSBAvl will be non-zero and ErrCde.Hdr.QUSEI will be set to 'CPF33C1' (Job &3/&2/&1 not on job queue. Job not moved.). In this case, SBMDJOBS will fall back to using format MJOB0100 and, as was done for the initial month-end job, move the month-end job to the top of the job queue BVINING/JOBQ2.

Once again, SBMDJOBS then saves the qualified name of the job moved to PrvQJN and waits for the next job notification message.

It's a nice and easy way to move jobs within, or across, job queues.

I mentioned earlier, though, that this approach has some problems/exposures. One is that the Job Notification exit runs asynchronously to the job running the SBMJOB command. While this might be just what you want in some situations, here it introduces the possibility that a submitted month-end job could be placed on the MYJOBQ job queue and start running before the job notification message is received and processed by SBMDJOBS. Therefore, we could have a combination of month-end jobs where some run out of MYJOBQ while others run out of JOBQ2, with the potential that two month-end jobs could be running concurrently due to the two different job queues being used.

A second consideration, and one that is explained in detail in the QSPMOVJB API documentation, is that moving a job can cause a change in the job's priority on the job queue (JOBPTY) and/or a change in the HOLD status of the job. As an example, if job MTHENDX is held on a job queue and then moved after job MTHENDC with MTHENDC having HOLD(*NO), then MTHENDX will be changed to HOLD(*NO) in order for it to be immediately after MTHENDC. As with the asynchronous nature of the job notification exit, this side effect might be just what you want for some situations, but you would really need to know the characteristics and assumptions of the various month-end jobs that might be moved.

Next month, we'll look at another exit capability of the system, one that runs synchronously with the SBMJOB processing, providing a solution that I believe will be much more along the "silver bullet" line Becky's management was hoping for.

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

$

Book Reviews

Resource Center

  •  

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

  • 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

  • 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: