midrange.com code scratchpad
Name:
AS400CommandOutput.java -- AS400 command output spooled file to TEXT file within QTEMP
Scriptlanguage:
Plain Text
Tabwidth:
4
Date:
05/12/2014 02:12:52 am
IP:
Logged
Description:
Many CLP to get command spooled output to QTEMP outfile in CLP, but when the CLP call by Java command server, could not get the QTEMP file. The AS400CommandOutput will run your command output to spooled and use CPYSPLF command to copy spooled to QTEMP outfile and read all record to TEXT file within QTEMP use AS400File.runCommand() method.
Code:
  1. package com.free400.vengoal;
  2.  
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.PrintWriter;
  7. import java.util.Enumeration;
  8.  
  9. import com.ibm.as400.access.AS400;
  10. import com.ibm.as400.access.AS400Exception;
  11. import com.ibm.as400.access.AS400File;
  12. import com.ibm.as400.access.AS400FileRecordDescription;
  13. import com.ibm.as400.access.AS400Message;
  14. import com.ibm.as400.access.CommandCall;
  15. import com.ibm.as400.access.Job;
  16. import com.ibm.as400.access.QSYSObjectPathName;
  17. import com.ibm.as400.access.Record;
  18. import com.ibm.as400.access.RecordFormat;
  19. import com.ibm.as400.access.SequentialFile;
  20. import com.ibm.as400.access.SpooledFile;
  21. import com.ibm.as400.access.SpooledFileList;
  22.  
  23. public class AS400CommandOutput {
  24.     
  25.     private static final String FILE_SEPARATOR_PROP = "file.separator";
  26.     public static java.text.SimpleDateFormat datetimeFmt = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  27.     private AS400 as400;
  28.     private CommandCall commandCall;
  29.     private SequentialFile seqfile;
  30.     private String lastSplfName=null, lastSplfJob=null, lastSplfUsr=null, lastSplfJobNbr=null;
  31.     private int lastSplfNbr=0;
  32.     private String file;
  33.     private String commandJobNbr;
  34.     private String ddmJobNbr;
  35.     private File toFile = null;
  36.     private PrintWriter writer_;
  37.     private FileOutputStream os;
  38.     private boolean fileOpened = false;
  39.  
  40.     public AS400CommandOutput(AS400 as400){
  41.         this.as400 = as400;
  42.         this.commandCall = new CommandCall(this.as400);
  43.     }
  44.     
  45.     public AS400CommandOutput(AS400 as400, File toFile){
  46.         this.as400 = as400;
  47.         this.toFile = toFile;
  48.         this.commandCall = new CommandCall(this.as400);
  49.     }
  50.     
  51.     public AS400 getSystem(){
  52.         return as400;
  53.     }    
  54.  
  55.     public String getCommandJobNbr(){
  56.         String cmdJobNbr = null;
  57.         try {
  58.             as400.connectService(AS400.COMMAND);
  59.             Job[] as400Jobs = as400.getJobs(AS400.COMMAND);
  60.             for(int i=0; i < as400Jobs.length; i++){
  61.                 cmdJobNbr = as400Jobs[i].getNumber();
  62.                 System.out.println("Linking to AS400 job: " + as400Jobs[i].getNumber() + "/" + as400Jobs[i].getUser() + "/" + as400Jobs[i].getName());
  63.             }
  64.         } catch (Exception e) {
  65.             e.printStackTrace();
  66.         }        
  67.         return cmdJobNbr;
  68.     }
  69.     
  70.     public String getDDMJobNbr(){
  71.         String ddmJobNbr = null;
  72.         try {
  73.             as400.connectService(AS400.RECORDACCESS);
  74.             Job[] as400Jobs = as400.getJobs(AS400.RECORDACCESS);
  75.             for(int i=0; i < as400Jobs.length; i++){
  76.                 ddmJobNbr = as400Jobs[i].getNumber();
  77.                 System.out.println("Linking to AS400 job: " + as400Jobs[i].getNumber() + "/" + as400Jobs[i].getUser() + "/" + as400Jobs[i].getName());
  78.             }
  79.         } catch (Exception e) {
  80.             e.printStackTrace();
  81.         }        
  82.         return ddmJobNbr;
  83.     }
  84.     
  85.     public void getLastSplfInfo(String selectSplfName, boolean deleteSplf) throws Exception{
  86.         String splfName, splfJob, splfUsr, splfJobNbr, splfDate,splfTime;
  87.         int splfNbr; 
  88.         String lastSplfDateTime=" ";
  89.         String splfDateTime;     
  90.     
  91.         SpooledFileList splfList = new SpooledFileList( as400 );
  92.         // set filters, all users, on all queues
  93.         splfList.setUserFilter(as400.getUserId());
  94.         splfList.setQueueFilter("/QSYS.LIB/%ALL%.LIB/%ALL%.OUTQ");
  95.         // open list, openSynchronously() returns when the list is completed.
  96.         splfList.openSynchronously();
  97.         Enumeration enumer = splfList.getObjects();            
  98.         while( enumer.hasMoreElements() )
  99.         {
  100.             SpooledFile splf = (SpooledFile)enumer.nextElement();
  101.             if ( splf != null )
  102.             {
  103.                 // output this spooled file's name
  104.                 splfName = splf.getStringAttribute(SpooledFile.ATTR_SPOOLFILE);
  105.                 splfJob = splf.getStringAttribute(SpooledFile.ATTR_JOBNAME);
  106.                 splfUsr = splf.getStringAttribute(SpooledFile.ATTR_JOBUSER);
  107.                 splfJobNbr =  splf.getStringAttribute(SpooledFile.ATTR_JOBNUMBER);
  108.                 splfDate =  splf.getStringAttribute(SpooledFile.ATTR_DATE);
  109.                 splfTime =  splf.getStringAttribute(SpooledFile.ATTR_TIME);
  110.                 splfNbr = splf.getIntegerAttribute(SpooledFile.ATTR_SPLFNUM);
  111.                 splfDateTime = splfDate + splfTime;
  112.                 //System.out.println("splfDate:" + splfDate + " splfTime:" + splfTime + " job:" + splfJobNbr + "/"+ splfUsr + "/" + splfJob +  " spooled file = " + splfName + " splfNbr=" + splfNbr);
  113.                 if(splfName.equalsIgnoreCase(selectSplfName) && splfJob.equalsIgnoreCase("QPRTJOB")){
  114.                     if (deleteSplf){
  115.                         splf.delete();
  116.                     } else if (splfDateTime.compareTo(lastSplfDateTime) > 0){
  117.                         lastSplfDateTime = splfDateTime;
  118.                         lastSplfName = splfName;
  119.                         lastSplfJob  = splfJob;
  120.                         lastSplfUsr  = splfUsr;                         
  121.                         lastSplfJobNbr=splfJobNbr;
  122.                         lastSplfNbr = splfNbr;
  123.                     }
  124.                 }
  125.             }
  126.             //System.out.println("last SPLFInfo: job " + lastSplfJobNbr + "/"+ lastSplfUsr + "/" + lastSplfJob + " lastSplfName:" + lastSplfName + " lastSplfNbr:" + lastSplfNbr);
  127.         }
  128.         // clean up after we are done with the list
  129.         splfList.close();
  130.     }
  131.     
  132.     public void cpysplfWithQtemp(String splfName, boolean deleteTempFile) throws Exception{
  133.         if(seqfile == null){
  134.             seqfile = new SequentialFile();
  135.             seqfile.setSystem(as400);
  136.             seqfile.setPath("/QSYS.LIB/QGPL.LIB/QDDSSRC.FILE"); // for run following command use;
  137.         }
  138.  
  139.         if(ddmJobNbr == null)
  140.             ddmJobNbr = getDDMJobNbr();        
  141.  
  142.         file = splfName.substring(0, 4) + ddmJobNbr;        
  143.         
  144.         getLastSplfInfo(splfName, false);
  145.         CPYSPLF(seqfile, lastSplfName, lastSplfJob, lastSplfUsr, lastSplfJobNbr, lastSplfNbr, true, "QTEMP", file, "M000000000", 201);
  146.         seqfile.setPath(new QSYSObjectPathName("QTEMP", file, "M000000000", "MBR").getPath());
  147.         setRecordFormat();
  148.         if(toFile == null)
  149.             readAll();
  150.         else
  151.             readAll(toFile);
  152.         getLastSplfInfo(splfName, true);
  153.     }
  154.     
  155.     public void readAll(){
  156.         readAll(null);
  157.     }
  158.     
  159.     public void readAll(File toFile){
  160.         try {
  161.             seqfile.open(AS400File.READ_ONLY, 100, AS400File.COMMIT_LOCK_LEVEL_NONE);
  162.             Record dataRcd = seqfile.readNext();
  163.             while (dataRcd != null) {
  164.                 if(toFile == null)
  165.                     onRecord(dataRcd);
  166.                 else
  167.                     onRecord(toFile, dataRcd);
  168.                 dataRcd = seqfile.readNext();
  169.             }
  170.         } catch (Exception e) {
  171.             e.printStackTrace();
  172.         } finally {
  173.             try {
  174.                 seqfile.close();
  175.                 if(fileOpened){
  176.                     writer_.close();
  177.                     os.close();
  178.                     fileOpened = false;
  179.                 }
  180.             } catch (Exception e) {
  181.                 e.printStackTrace();
  182.             }
  183.         }
  184.     }
  185.     
  186.     public void onRecord(Record record) {
  187.         System.out.println(getClass() + ":" + record);        
  188.     }
  189.     
  190.     public void onRecord(File file, Record record) throws IOException{
  191.         if(!fileOpened){
  192.             os = new FileOutputStream(file, file.exists());
  193.             writer_ = new PrintWriter(os, true);
  194.             fileOpened = true;
  195.         }
  196.          writer_.println(record);
  197.          writer_.flush();    
  198.     }
  199.     
  200.     public void setRecordFormat() throws Exception{
  201.         AS400FileRecordDescription recordDescription = new AS400FileRecordDescription(as400, seqfile.getPath());
  202.         RecordFormat[] formats = recordDescription.retrieveRecordFormat();
  203.         RecordFormat recordFormat = formats[0];
  204.         seqfile.setRecordFormat(recordFormat);
  205.     }
  206.     
  207.     public void CPYSPLF(SequentialFile seqFile, String splfName, String splfJob, String splfUsr, String splfJobNbr, int splfNbr , boolean includeIGCData, String toLibrary, String toFile, String toMbr, int toFileRecordLength){
  208.  
  209.         String cmdCRTPF = "CRTPF FILE(" 
  210.                 + toLibrary.toUpperCase().trim()  
  211.                 + "/" 
  212.                 + toFile.toUpperCase().trim()  
  213.                 + ") RCDLEN(" 
  214.                 + toFileRecordLength 
  215.                 + ") MBR(" 
  216.                 + toMbr.toUpperCase().trim() 
  217.                 + ") MAXMBRS(*NOMAX) SIZE(*NOMAX)";
  218.         
  219.         String cmdCPYSPLF = "CPYSPLF FILE(" 
  220.                 + splfName
  221.                 + ") TOFILE(" 
  222.                 + toLibrary.toUpperCase().trim() 
  223.                 + "/" 
  224.                 + toFile.toUpperCase().trim() 
  225.                 + ") JOB("
  226.                 + splfJobNbr 
  227.                 + "/" 
  228.                 + splfUsr 
  229.                 + "/" 
  230.                 + splfJob
  231.                 + ") SPLNBR(" 
  232.                 + splfNbr 
  233.                 + ") MBROPT(*REPLACE)";
  234.         try {
  235.             if(!chkObjExist(seqFile, toLibrary, toFile, "*FILE"))
  236.                 seqFile.runCommand(cmdCRTPF);    
  237.  
  238.             AS400Message[] messagelist = seqFile.runCommand(cmdCPYSPLF);
  239.             for (int i = 0; i < messagelist.length; i++) {
  240.                 if (messagelist[i].getID() != null){                    
  241.                     if(messagelist[i].getID().equalsIgnoreCase("CPF3485")){
  242.                         System.out.println(cmdCPYSPLF +" Command successful"); 
  243.                     } else
  244.                         System.out.println(messagelist[i].getID() + " " + messagelist[i].getText());
  245.                 }
  246.             }
  247.         } catch (Exception e) {
  248.             e.printStackTrace();
  249.         }
  250.     }
  251.     
  252.     public boolean chkObjExist(SequentialFile seqFile, String objLib, String objName, String objType) throws Exception {
  253.         boolean objectExist = true;
  254.         String cmdChkObj;
  255.         cmdChkObj = "CHKOBJ OBJ(" + objLib + "/" + objName + ") OBJTYPE(" + objType + ")";
  256.         AS400Message[] messagelist = seqFile.runCommand(cmdChkObj);
  257.         for (int i = 0; i < messagelist.length; i++) {
  258.             if (messagelist[i].getID() != null){
  259.                 System.out.println(messagelist[i].getID() + " " + messagelist[i].getText());
  260.                 if (messagelist[i].getID().equalsIgnoreCase("CPF9801")){
  261.                     objectExist = false;
  262.                 }
  263.             }
  264.         }
  265.         return objectExist;
  266.     }
  267.     
  268.     public boolean runCmd(String commandString) {        
  269.         boolean success = false;
  270.         try {
  271.             // Run the command.
  272.             if (success = commandCall.run(commandString)){
  273.                 //System.out.println(commandString + " Command successful");
  274.             }else{
  275.                 System.out.println(commandString + " Command failed");
  276.                 throw new AS400Exception(commandCall.getMessageList());
  277.             }
  278.         } catch (Exception e) {
  279.             System.out.println("Command " + commandCall.getCommand() + " did not run");
  280.             e.printStackTrace();
  281.         }
  282.         return success;
  283.     }
  284.     
  285.     public static void main(String[] args) {
  286.         try {
  287.             AS400 as400 = new AS400("as400ip", "user", "userpass");
  288.             AS400CommandOutput get400ACTJOB = new AS400CommandOutput(as400, new File("d:\\temp\\WRKACTJOB.TXT"));
  289.             get400ACTJOB.runCmd("WRKACTJOB OUTPUT(*PRINT) RESET(*YES) SEQ(*CPUPCT)");
  290.             Thread.sleep(5000);
  291.             get400ACTJOB.runCmd("WRKACTJOB OUTPUT(*PRINT) SEQ(*CPUPCT)");
  292.             get400ACTJOB.cpysplfWithQtemp("QPDSPAJB", true);
  293.  
  294.             get400ACTJOB.runCmd("WRKSYSSTS OUTPUT(*PRINT)");
  295.             get400ACTJOB.cpysplfWithQtemp("QPDSPSTS", true);
  296.  
  297.         } catch (Exception e) {
  298.             e.printStackTrace();
  299.         }
  300.     }    
  301. }
  302.  
© 2004-2019 by midrange.com generated in 0.009s valid xhtml & css