29
Fri, Nov
0 New Articles

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

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