How many jobs are in the jobq

Is there a command or code that will return the number of jobs there are in a particular jobq?   Some of our interactive applications have the user submit a batch job to a job queue that will sequentially process jobs, and we want to be able to send a message back to the user informing him of where his job is in the queue.

I'm just going to guess at how many points to assign to this, cause if you know the answer it's probably easy, if you don't it's difficult.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


If you've used APIs before, this is "easy". If not, then this is as good of an API to start with as any -- the Retrieve Job Queue Information (QSPRJOBQ) API, one of the Work Management APIs. V5R3 documentation for QSPRJOBQ should be at:

Some trivial CL that would return number of jobs would look like:

  dcl   &JobQ     *char   10   value( 'QBATCH' )
  dcl   &JobQLib  *char   10   value( 'QGPL' )
  dcl   &qJobQ    *char   20

  dcl   &RcvVar   *char  144

  dcl   &bJobs    *char    4
  dcl   &dJobs    *dec  (   5 0 )  value( 0 )

   chgvar   &qJobQ   ( &JobQ *cat &JobQLib )

   call  QSPRJOBQ  (  +
                               &RcvVar   +
                               x'00000090'  +
                               'JOBQ0100'  +
                               &qJobQ  +
                               x'0000000000000000' +

   chgvar   &bJobs   %sst( &RcvVar  49  4 )
   chgvar   &dJobs   %bin( &bJobs )

The code calls the API for qualified job queue QGPL/QBATCH, requesting information to be returned formatted as a JOBQ0100 structure. It provides a char(144) variable to hold the structure; that variable is &RcvVar. It passes in the length of &RcvVar in binary as a 4-byte hex value in the second parm -- decimal 144 is hex 90. The last parameter tells the API not to return any error structure; any errors will be signalled as *ESCAPE messages instead. This is so because the parameter is 8-bytes of all hex zeros, which corresponds to two 4-byte binary fields -- the first 4-bytes are most important since they tell the API how much space is allocated here for an error structure, i.e., none.

When the API completes, the code extracts the number of jobs from the structure in two forms. First, it simply grabs the 4-bytes as characters from the positions where the number of jobs is returned. The API documents that as being offset 48 bytes from the beginning of the structure; so I added one to get position 49 since CL doesn't work exactly with offsets. It's defined as a 4-byte binary field. The code then converts that binary value into a decimal variable that has five positions and no decimal positions. (Number of jobs better be a whole number.)

Coding was from the documentation rather than copied, so it _might_ have minor bugs. Those can be fixed if necessary.

My take on this question is somthing like this.
This is the easiest way.

a new command - when compiling it the program to proccess the command is the cl program

             CMD        PROMPT('Display jobq position')                    
             PARM       KWD(JOBQ) TYPE(Q0001) MIN(1) PROMPT('Jobq')        
             PARM       KWD(USER) TYPE(*NAME) DFT(*CURRENT) +              
                          SPCVAL((*CURRENT)) PROMPT('User')                
 Q0001:      QUAL       TYPE(*NAME)                                        
             QUAL       TYPE(*NAME) DFT(*LIBL) SPCVAL((*LIBL *LIBL) +      
                          (*CURLIB *CURLIB)) PROMPT('Library')            


The cl program is


pgm (&jobqLib &User)                                              
             DCL        VAR(&JOBQLIB) TYPE(*CHAR) LEN(20)        
             DCL        VAR(&JOBQ) TYPE(*CHAR) LEN(10)            
             DCL        VAR(&LIB) TYPE(*CHAR) LEN(10)            
             DCL        VAR(&USER) TYPE(*CHAR) LEN(10)            
             CHGVAR     VAR(&JOBQ) VALUE(&JOBQLIB)                
             CHGVAR     VAR(&LIB) VALUE(%SST(&JOBQLIB 11 10))    
             IF         COND(&USER = *CURRENT) THEN(RTVJOBA +    
             OVRPRTF    FILE(QPRTSPLQ) HOLD(*YES)                
             WRKJOBQ    JOBQ(&LIB/&JOBQ) OUTPUT(*PRINT)          
             DLTF       FILE(QTEMP/xxjobq)                        
             MONMSG     MSGID(CPF0000)                            
             CRTPF      FILE(QTEMP/xxjobq) RCDLEN(198)            
             CPYSPLF    FILE(QPRTSPLQ) TOFILE(QTEMP/xxjobq) +          
             DLTSPLF    FILE(QPRTSPLQ) SPLNBR(*LAST)                    
             CALL       PGM(EEJOBQ) PARM(&USER)                        


Program EEjobq is the rpg program that followes


feejobq    cf   e             Workstn sfile(f01sfl : rrn1)              
fQPRTSPLQ  if   f  198        disk    ExtFile('QTEMP/XXJOBQ')          
d Input           ds           198                                      
d   Job                         10    Overlay(Input : 2)                
d   Job1                         6    Overlay(Input : 2)                
d   User                        10    Overlay(Input : 15)              
d   Sts                         10    Overlay(Input : 49)              
d   Hrdtxt                      20    Overlay(Input : 37)              
d Hdrtxtc         c                   'Work With Job Queue'            
d Headingsc       c                   'Job Name'                        
d I               s             10i 0                                  
c     *entry        Plist                                              
c                   parm                    p#User           10        
c                   write     F01ctl                                    
c                   do        *hival                                    
c                   Read      QPRTSPLQ      Input            
c                   if        %eof                          
c                   Leave                                    
c                   endif                                    
c                   if        Job = HeadingsC or            
c                             Hrdtxt = HdrtxtC or            
c                             Job1 = *blanks                
c                   Iter                                    
c                   endif                                    
c                   eval      I = I + 1                      
c                   if        User = P#User                  
c                   eval      rrn1 = rrn1 + 1                
c                   eval      Pos = i                        
c                   write     f01sfl                        
c                   endif                                    
c                   enddo                              
c                   eval      *in31 = rrn1 <> 0        
c                   Eval      Nbrjob = i              
c                   write     f01ftr                  
c                   exfmt     f01ctl                  
c                   eval      *inlr = *on              


and the display file is that is defined as EEJOBQ


     A                                      DSPSIZ(24 80 *DS3)            
     A          R F01SFL                    SFL                            
     A            JOB           10   O  7  3                              
     A            STS           10   O  7 15                              
     A            POS            7Y 0O  7 28EDTCDE(Z)                      
     A          R F01CTL                    SFLCTL(F01SFL)                
     A  *DS3                                SFLLIN(0005)                  
     A                                      SFLSIZ(0030)                  
     A                                      SFLPAG(0029)                  
     A  31                                  SFLDSP                        
     A                                      SFLDSPCTL                      
     A                                      OVERLAY                        
     A N31                                  SFLCLR                        
     A  31                                  SFLEND(*MORE)                  
     A            RRN1           4S 0H      SFLRCDNBR                      
     A                                  1 27'Display Jobq Position'        
     A                                      DSPATR(HI)                    
     A                                  1 73DATE                          
     A                                      EDTCDE(Y)                      
     A                                  2 73TIME                                  
     A                                  4  3'Jobs in queue:'                      
     A            NBRJOB         7Y 0O  4 18EDTCDE(Z)                            
     A                                  6  3'Job         Sts         Position  -  
     A                                         Job         Sts         Position'  
     A                                      DSPATR(HI)                            
     A                                      DSPATR(UL)                            
     A                                      COLOR(BLU)                            
     A                                  4 40'User Id:'                            
     A            P#USER        10   O  4 49                                      
     A          R F01FTR                                                          
     A                                 23  3'                                  -  
     A                                                                         -  
     A                                              '                            
     A                                      DSPATR(UL)                            
     A                                      COLOR(BLU)                            
     A                                 24  3'Enter to continue'                  
     A                                      DSPATR(HI)                            


It basically used the wrkjobq command to list the jobs - the shows them is a subfile.

there will be an API that does a better job but this is quick and easy.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
And note that regardless of how it's done, the count might be off.

For example -- The jobq is empty when the job is submitted and the subsystem has an available slot. That will put the job active pretty much immediately. And if the jobq is empty but there are no slots available at the moment in the subsystem, then the submitted job will be first in the queue. (Except when someone else, or multiple someones, is submitting jobs during the times you're counting, etc.)

Unless you (1) temporarily hold the queue, (2) submit the job, (3) count the jobs and (4) release the queue, there's potential inaccuracy. Further, that process must be serialized across _all_ jobs that submit to the same queue so that one job isn't releasing the queue just after another is holding it. But I suspect that's a level of accuracy beyond requirement, eh?

I'd guess you'd be fine simply calling the API, then immediately submitting the job, and then reporting the value from the API plus one.

Dave's example provides a way to get a list of what the system sees as the sequence of jobs in the queue. That could vary by priority, by some jobs being held/released, etc. I've never actually tried to run an analysis to that level of detail, so the spooled file _might_ be the reasonable way to do it. If that analysis was needed in your situation, I'd _probably_ try the Open List of Jobs (QGYOLJOB) API first; but that's not so easy as APIs go and that helps support Dave's suggestion. As always with spooled files based on system output, be aware that printed formats can change during upgrades in the future.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
IBM System i

From novice to tech pro — start learning today.