| Code: 
							
								
								
								| 
    
    
    
    
    
    
    
    
    
    
    
    package com.vengoal.as400.list;
 import java.io.IOException;
import java.util.TreeMap;
 import com.ibm.as400.access.AS400;
import com.ibm.as400.access.AS400Exception;
import com.ibm.as400.access.AS400SecurityException;
import com.ibm.as400.access.AS400Text;
import com.ibm.as400.access.BinaryConverter;
import com.ibm.as400.access.CharConverter;
import com.ibm.as400.access.ErrorCodeParameter;
import com.ibm.as400.access.ErrorCompletingRequestException;
import com.ibm.as400.access.Job;
import com.ibm.as400.access.ObjectDoesNotExistException;
import com.ibm.as400.access.ProgramCall;
import com.ibm.as400.access.ProgramParameter;
import com.ibm.as400.access.Trace;
import com.ibm.as400.access.list.OpenList;
  * Represents a list of subsystem jobs on the system with Open List of Jobs (QGYOLJOB) API. 
 * By default, following keys retrieved:
 *             keys_[0] = 305;
 *            keys_[1] = 601;
 *            keys_[2] = 602;
 *            keys_[3] = 1307;
 *            keys_[4] = 1308;
 *            keys_[5] = 1309;
 *            keys_[6] = 1906;
 * 
 * List of Keys Supported for Format OLJB0300 reference:
 * http://www-01.ibm.com/support/knowledgecenter/ssw_ibm_i_72/apis/qgyoljob.htm?lang=en
 *
 */
public class SubsystemJobOpenList extends OpenList {
        
    private String subsystem_;
    private Job[] subsystemJobs_;
    
        private int currentSortKey_ = 1;
        
        private int numKeysReturned_;
    private int[] keyFieldsReturned_;
    private char[] keyTypesReturned_;
    private int[] keyLengthsReturned_;
    private int[] keyOffsetsReturned_;
        
        private int currentKey_ = 7;
    private int[] keys_ = new int[currentKey_];
    
    public SubsystemJobOpenList(AS400 system, String subsystem) {
         super(system);
         this.subsystem_ = subsystem;
                        keys_[0] = 305;
            keys_[1] = 601;
            keys_[2] = 602;
            keys_[3] = 1307;
            keys_[4] = 1308;
            keys_[5] = 1309;
            keys_[6] = 1906;        
    }
    
    public void addJobAttributeToRetrieve(int attribute){
        if (currentKey_ >= keys_.length){
                        int[] temp = keys_;
            keys_ = new int[temp.length * 2];
            System.arraycopy(temp, 0, keys_, 0, temp.length);
        }
        keys_[currentKey_++] = attribute;
    }
    
    public Job[] getSubsystemJobs(){
        return subsystemJobs_;
    }
     @Override
    protected byte[] callOpenListAPI() throws AS400SecurityException,
            ErrorCompletingRequestException, InterruptedException, IOException,
            ObjectDoesNotExistException {
        if (Trace.isTraceOn()) Trace.log(Trace.DIAGNOSTIC, "Opening spooled file list.");
         int lengthOfReceiverVariableDefinitionInformation = 4 + 20 * currentKey_;
        byte[] keyOfFieldsToBeReturned = new byte[4 * currentKey_];
        for (int i = 0; i < currentKey_; ++i)
        {
            BinaryConverter.intToByteArray(keys_[i], keyOfFieldsToBeReturned, i * 4);
        }
        
                byte[] sortInformation = new byte[4 + currentSortKey_ * 12];
        BinaryConverter.intToByteArray(currentSortKey_, sortInformation, 0);
        int fieldStartingPosition = 1;
        int fieldLength = 10;
        short dataType = (short)4;
        BinaryConverter.intToByteArray(fieldStartingPosition, sortInformation, 4 );
        BinaryConverter.intToByteArray(fieldLength, sortInformation, 8);
        BinaryConverter.shortToByteArray(dataType, sortInformation, 12);
                sortInformation[14] = (byte)0xF1;
        
                byte[] jobSelectionInformation = new byte[206];
        
                CharConverter conv = new CharConverter(system_.getCcsid(), system_);
        
        for (int i = 0; i < 26; ++i) jobSelectionInformation[i] = 0x40;
        String selectionJobName_ = "*ALL";
        String selectionUserName_= "*ALL";
        String selectionJobNumber_= "*ALL";
        String selectionJobType_= "*";
        conv.stringToByteArray(selectionJobName_.toUpperCase(), jobSelectionInformation, 0);
        conv.stringToByteArray(selectionUserName_.toUpperCase(), jobSelectionInformation, 10);
        conv.stringToByteArray(selectionJobNumber_, jobSelectionInformation, 20);
        conv.stringToByteArray(selectionJobType_, jobSelectionInformation, 26);
        
        int offset = 195;
        int numberOfSubsystem = 1;
         BinaryConverter.intToByteArray(offset, jobSelectionInformation, 76);
        BinaryConverter.intToByteArray(numberOfSubsystem, jobSelectionInformation, 80);
                AS400Text subsystemText = new AS400Text(10, system_);
        byte[] subSystemBytes = subsystemText.toBytes(subsystem_);            
        System.arraycopy(subSystemBytes, 0, jobSelectionInformation, offset, 10);
        offset += 10;             
        
                ProgramParameter[] parameters = new ProgramParameter[]
        {
                        new ProgramParameter(0),
                        new ProgramParameter(new byte[] { 0x00, 0x00, 0x00, 0x00 } ),
                        new ProgramParameter(new byte[] { (byte)0xD6, (byte)0xD3, (byte)0xD1, (byte)0xC2, (byte)0xF0, (byte)0xF3, (byte)0xF0, (byte)0xF0 } ),
                        new ProgramParameter(lengthOfReceiverVariableDefinitionInformation),
                        new ProgramParameter(BinaryConverter.intToByteArray(lengthOfReceiverVariableDefinitionInformation)),
                        new ProgramParameter(80),
                                    new ProgramParameter(new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF } ),
                        new ProgramParameter(sortInformation),
                        new ProgramParameter(jobSelectionInformation),
                        new ProgramParameter(BinaryConverter.intToByteArray(jobSelectionInformation.length)),
                        new ProgramParameter(BinaryConverter.intToByteArray(currentKey_)),
                        new ProgramParameter(keyOfFieldsToBeReturned),
                        new ErrorCodeParameter(),
                        new ProgramParameter(new byte[] { (byte)0xD6, (byte)0xD3, (byte)0xD1, (byte)0xE2, (byte)0xF0, (byte)0xF2, (byte)0xF0, (byte)0xF0 } )
        };
        
                ProgramCall pc = new ProgramCall(system_, "/QSYS.LIB/QGY.LIB/QGYOLJOB.PGM", parameters);
        
        if (!pc.run())
        {
            throw new AS400Exception(pc.getMessageList());
        }
        
                byte[] defInfo = parameters[3].getOutputData();
        numKeysReturned_ = BinaryConverter.byteArrayToInt(defInfo, 0);
        keyFieldsReturned_ = new int[numKeysReturned_];
        keyTypesReturned_ = new char[numKeysReturned_];
        keyLengthsReturned_ = new int[numKeysReturned_];
        keyOffsetsReturned_ = new int[numKeysReturned_];
        
        offset = 4;
        for (int i = 0; i < numKeysReturned_; ++i)
        {
            keyFieldsReturned_[i] = BinaryConverter.byteArrayToInt(defInfo, offset + 4);
            keyTypesReturned_[i] = conv.byteArrayToString(defInfo, offset + 8, 1).charAt(0);             keyLengthsReturned_[i] = BinaryConverter.byteArrayToInt(defInfo, offset + 12);
            keyOffsetsReturned_[i] = BinaryConverter.byteArrayToInt(defInfo, offset + 16);
            offset += 20;
        }
        
                return parameters[5].getOutputData();
    }
     @Override
    protected Object[] formatOutputData(byte[] data, int recordsReturned, int recordLength)
            throws AS400SecurityException, ErrorCompletingRequestException,
            InterruptedException, IOException, ObjectDoesNotExistException {
        int number = recordsReturned;          
        CharConverter conv = new CharConverter(system_.getCcsid(), system_);
        
        SubsystemJobListItem[] listItems = new SubsystemJobListItem[number];
        subsystemJobs_ = new Job[number];
        String currentUser = null;
        String functionName = null;
        String functionType = null;
        String messageReply = null;
        byte[] messageKey = null;
        String qualMessageQueue = null;
        String qualSubsystem = null;
        TreeMap keyValues = new TreeMap();
        for (int i = 0, offset = 0; i < listItems.length; ++i, offset += recordLength)
        {
            String jobName = conv.byteArrayToString(data, offset, 10);
            String jobUser = conv.byteArrayToString(data, offset + 10, 10);
            String jobNumber = conv.byteArrayToString(data, offset + 20, 6);
            String status = conv.byteArrayToString(data, offset + 26, 4);
            String jobType = conv.byteArrayToString(data, offset + 30, 1);
            String jobSubtype = conv.byteArrayToString(data, offset + 31, 1);
            
            for (int j = 0; j < numKeysReturned_; ++j)
            {
                 int keyOffset = keyOffsetsReturned_[j];
                 if (keyTypesReturned_[j] == 'C')
                 {    
                     String value = conv.byteArrayToString(data, offset + keyOffset, keyLengthsReturned_[j]);
                     if(keyFieldsReturned_[j] == 305 ) currentUser = value;
                     if(keyFieldsReturned_[j] == 601 ) functionName = value;
                     if(keyFieldsReturned_[j] == 602 ) functionType = value;
                     if(keyFieldsReturned_[j] == 1307) messageReply = value;
                     if(keyFieldsReturned_[j] == 1309) qualMessageQueue = value;
                     if(keyFieldsReturned_[j] == 1906) qualSubsystem = value;
                     if(keyFieldsReturned_[j] == 1308)
                     {
                         byte[] msgKey = new byte[4];
                         System.arraycopy(data, offset + keyOffset, msgKey, 0, 4);
                         messageKey = msgKey;
                     }
                     if(j > 6){
                         if(keyFieldsReturned_[j] == 312 || keyFieldsReturned_[j] == 313 ||
                                 keyFieldsReturned_[j] == 315 || keyFieldsReturned_[j] == 317 ||
                                 keyFieldsReturned_[j] == 414 || keyFieldsReturned_[j] == 415 ||
                                 keyFieldsReturned_[j] == 416 || keyFieldsReturned_[j] == 417 ||
                                 keyFieldsReturned_[j] == 1609)
                             keyValues.put(keyFieldsReturned_[j], new Long(BinaryConverter.byteArrayToLong(data, offset + keyOffset))) ;
                         else
                             keyValues.put(keyFieldsReturned_[j], value) ;
                     }
                 }
                 else
                 {
                     if ((keyFieldsReturned_[j] == Job.TEMP_STORAGE_USED_LARGE))
                         keyValues.put(keyFieldsReturned_[j], new Long(BinaryConverter.byteArrayToUnsignedInt(data, offset + keyOffset))) ;
                     else
                         keyValues.put(keyFieldsReturned_[j], new Integer(BinaryConverter.byteArrayToInt(data, offset + keyOffset))) ;
                 }        
            }
            listItems[i] = new SubsystemJobListItem(jobName, jobUser, jobNumber,
                        status, jobType,jobSubtype, currentUser, functionName, functionType,
                        messageReply, messageKey, qualMessageQueue, qualSubsystem);
            subsystemJobs_[i] = new Job(this.getSystem(), jobName, jobUser, jobNumber);
            
            listItems[i].setKeyValues(keyValues);
        }
        return listItems;        
    }
     @Override
    protected int getBestGuessReceiverSize(int number) {        
        return 300 * number;
    }
    
}
============================================================================
package com.vengoal.as400.list;
 import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
 import com.ibm.as400.access.BinaryConverter;
 public class SubsystemJobListItem {
    public static final  int ACTIVE_JOB_STATUS_FOR_JOBS_ENDING     =  103;     public static final  int CURRENT_USER                          =  305;     public static final  int CPU_TIME_USED_LARGE                   =  312;     public static final  int CPU_TIME_USED_FOR_DATABASE            =  313;     public static final  int ELAPSED_CPU_PERCENT_USED              =  314;     public static final  int ELAPSED_CPU_TIME_USED                 =  315;     public static final  int ELAPSED_CPU_PERCENT_USED_FOR_DATABASE =  316;     public static final  int ELAPSED_CPU_TIME_USED_FOR_DATABASE    =  317;     public static final  int DATE_ENTERED_SYSTEM                   =  402;     public static final  int ELAPSED_DISK_IO                       =  414;     public static final  int DISK_IO                               =  415;     public static final  int ELAPSED_DISK_IO_ASYNCH                =  416;     public static final  int ELAPSED_DISK_IO_SYNCH                 =  417;     public static final  int CONTROLLED_END_REQUESTED              =  502;     public static final  int FUNCTION_NAME                         =  601;     public static final  int FUNCTION_TYPE                         =  602;     public static final  int INTERNAL_JOB_IDENTIFIER               =  902;     public static final  int ELAPSED_INTERACTIVE_RESPONSE_TIME     =  904;     public static final  int ELAPSED_INTERACTIVE_TRANSACTIONS      =  905;     public static final  int JOB_USER_IDENTITY                     = 1012;     public static final  int JOB_END_REASON                        = 1014;     public static final  int JOB_LOG_PENDING                       = 1015;     public static final  int JOB_TYPE_ENHANCED                     = 1016;     public static final  int MEMORY_POOL                           = 1306;     public static final  int MESSAGE_REPLY                         = 1307;     public static final  int MESSAGE_KEY                           = 1308;     public static final  int MESSAGE_QUEUE                         = 1309;     public static final  int MESSAGE_QUEUE_ASP                     = 1310;     public static final  int ELAPSED_PAGE_FAULTS                   = 1609;     public static final  int RUN_PRIORITY                          = 1802;     public static final  int SUBSYSTEM                             = 1906;     public static final  int SERVER_TYPE                           = 1911;     public static final  int SPOOLED_FILE_ACTION                   = 1982;     public static final  int THREAD_COUNT                          = 2008;     public static final  int TEMP_STORAGE_USED_LARGE               = 2009;     
    private String jobName;
    private String jobUser;
    private String jobNumber;
    private String status;
    private String jobType;
    private String jobSubtype;
    private String currentUser;          private String functionName;         private String functionType;         private String messageReply;         private byte[] messageKey;           private String qualMessageQueue;     private String qualSubsystem;        private TreeMap keyValues = new TreeMap();     
    public SubsystemJobListItem(String jobName, String jobUser, String jobNumber,
            String status, String jobType,String jobSubtype, String currentUser, String functionName,
            String functionType,
            String messageReply, byte[] messageKey, String qualMessageQueue, String qualSubsystem) {    
        this.jobName = jobName;
        this.jobUser = jobUser;
        this.jobNumber = jobNumber;
        this.status = status;
        this.jobType = jobType;
        this.jobSubtype = jobSubtype;
        this.currentUser = currentUser;
        this.functionName = functionName;
        this.functionType = functionType;
        this.messageReply = messageReply;
        this.messageKey = messageKey;
        this.qualMessageQueue = qualMessageQueue;
        this.qualSubsystem = qualSubsystem;        
    }
    
    public Object getObject(int key){
        return keyValues.get(key);
    }
    
    public void setKeyValues(TreeMap keyValues){
        this.keyValues = keyValues;
    }
     public String getJobName() {
        return jobName;
    }
     public String getJobUser() {
        return jobUser;
    }
     public String getJobNumber() {
        return jobNumber;
    }
     public String getStatus() {
        return status;
    }
     public String getJobType() {
        return jobType;
    }
     public String getJobSubtype() {
        return jobSubtype;
    }
     public String getCurrentUser() {
        return currentUser;
    }    
    
    public String getFunctionName() {
        return functionName;
    }    
     public String getFunctionType() {
        return functionType;
    }
     public String getMessageReply() {
        return messageReply;
    }
     public byte[] getMessageKey() {
        return messageKey;
    }
     public String getQualMessageQueue() {
        return qualMessageQueue;
    }
     public String getQualSubsystem() {
        return qualSubsystem;
    }
    
    public String toString(){
        StringBuffer strBuf = new StringBuffer();
        strBuf.append(jobName).append("/");
        strBuf.append(jobUser).append("/");
        strBuf.append(jobNumber).append(",");
        strBuf.append(status).append(",");
        strBuf.append(jobType).append(",");
        strBuf.append(jobSubtype).append(",");
        strBuf.append("305=" + currentUser).append(",");
        strBuf.append("601=" + functionName).append(",");
        strBuf.append("602=" + functionType).append(",");
        strBuf.append("1307=" + messageReply).append(",");
        strBuf.append("1308(MSGKEY 4 bytes hex string)=" + BinaryConverter.bytesToHexString(messageKey)).append(",");
        strBuf.append("1309=" + qualMessageQueue).append(",");
        strBuf.append("1906=" + qualSubsystem);
        if(keyValues.size() > 0){
             Set set = keyValues.entrySet();
             Iterator i = set.iterator();
             while(i.hasNext()) {                 
                 Map.Entry me = (Map.Entry)i.next();
                 strBuf.append("," + me.getKey() + "=" + me.getValue());
            }
        }
        return strBuf.toString();
    }
}
============================================================================
package com.vengoal.as400.list;
 import java.util.Enumeration;
 import com.ibm.as400.access.AS400;
import com.ibm.as400.access.CallStackEntry;
import com.ibm.as400.access.Job;
import com.vengoal.as400.common.MessageUtil;
 public class SubsystemJobOpenListTest {
     public static void main(String[] args) {
        AS400 as400 = new AS400("as400ip", "user", "pass");
        String subsystem = "QBATCH";
        SubsystemJobOpenList list = new SubsystemJobOpenList(as400, subsystem);
        list.addJobAttributeToRetrieve(SubsystemJobListItem.MEMORY_POOL);
        list.addJobAttributeToRetrieve(SubsystemJobListItem.RUN_PRIORITY);
        list.addJobAttributeToRetrieve(SubsystemJobListItem.DATE_ENTERED_SYSTEM);
        list.addJobAttributeToRetrieve(SubsystemJobListItem.JOB_LOG_PENDING);
        list.addJobAttributeToRetrieve(SubsystemJobListItem.JOB_TYPE_ENHANCED);
        list.addJobAttributeToRetrieve(SubsystemJobListItem.SPOOLED_FILE_ACTION);
        list.addJobAttributeToRetrieve(SubsystemJobListItem.THREAD_COUNT);
        list.addJobAttributeToRetrieve(SubsystemJobListItem.CPU_TIME_USED_LARGE);
        list.addJobAttributeToRetrieve(SubsystemJobListItem.ELAPSED_CPU_PERCENT_USED);
        list.addJobAttributeToRetrieve(SubsystemJobListItem.ELAPSED_CPU_TIME_USED);
        list.addJobAttributeToRetrieve(SubsystemJobListItem.ELAPSED_PAGE_FAULTS);
        list.addJobAttributeToRetrieve(SubsystemJobListItem.DISK_IO);
        list.addJobAttributeToRetrieve(SubsystemJobListItem.ELAPSED_DISK_IO_ASYNCH);
        list.addJobAttributeToRetrieve(SubsystemJobListItem.ELAPSED_DISK_IO_SYNCH);
        try {
            list.open();
            Enumeration items = list.getItems();
            while (items.hasMoreElements())
            {
                SubsystemJobListItem item = (SubsystemJobListItem)items.nextElement();
                System.out.println(item);
                if(item.getMessageReply().equalsIgnoreCase(Job.MESSAGE_REPLY_WAITING)){
                    Job msgwJob = new Job(as400, item.getJobName(), item.getJobUser(), item.getJobNumber());
                    CallStackEntry[] callstackEntry = msgwJob.getCallStack(Job.INITIAL_THREAD);
                    System.out.println("job call stack as following:");
                    for(int i = 0; i< callstackEntry.length; ++i){
                        System.out.println(callstackEntry[i].getProgramLibrary() + "/" + callstackEntry[i].getProgramName() + " " + callstackEntry[i].getProcedureName());
                    }
                }
                System.out.println("Spooled file action=" + item.getObject(SubsystemJobListItem.SPOOLED_FILE_ACTION));
                System.out.println("====================================");
            }
            
            Job[] subsystemJobs = list.getSubsystemJobs();            
            if(subsystemJobs != null){
                for(int i =0; i < subsystemJobs.length; ++i){
                                        System.out.println(subsystemJobs[i].getNumber() + "/" + subsystemJobs[i].getUser() + "/" + subsystemJobs[i].getName());
                }
            } else {
                System.out.println("Subsystem " + subsystem + " is inactive or not exist");
            }
            list.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  |  |