The ProgramCall class allows the Java™ program to call an iSeries™ program. You can use the ProgramParameter class to specify input, output, and input/output parameters. If the program runs, the output and input/output parameters contain the data that is returned by the iSeries program. If the iSeries program fails to run successfully, the Java program can retrieve any resulting iSeries messages as a list of AS400Message objects.
Required parameters are as follows:
The program name and parameter list can be set on the constructor, through the setProgram() method, or on the run() method The run() method calls the program.
Using the ProgramCall class causes the AS400 object to connect to the iSeries server. See managing connections for information about managing connections.
Example: Using ProgramCall
The following example shows how to use the ProgramCall class:
// Create an AS400 object. AS400 sys = new AS400("mySystem.myCompany.com"); // Create a program object. I choose // to set the program to run later. ProgramCall pgm = new ProgramCall(sys); // Set the name of the program. // Because the program does not take // any parameters, pass null for the // ProgramParameter[] argument. pgm.setProgram(QSYSObjectPathName.toPath("MYLIB", "MYPROG", "PGM")); // Run the program. My program has // no parms. If it fails to run, the failure // is returned as a set of messages // in the message list. if (pgm.run() != true) { // If you get here, the program // failed to run. Get the list of // messages to determine why the // program didn't run. AS400Message[] messageList = pgm.getMessageList(); // ... Process the message list. } // Disconnect since I am done // running programs sys.disconnectService(AS400.COMMAND);
The ProgramCall object requires the integrated file system path name of the program.
The default behavior is for iSeries programs to run in a separate server job, even when the Java program and the iSeries program are on the same server. You can override the default behavior and have the iSeries program run in the Java job using the setThreadSafe() method.
You can use the ProgramParameter objects to pass parameter data between the Java program and the iSeries program. Set the input data with the setInputData() method. After the program is run, retrieve the output data with the getOutputData() method. Each parameter is a byte array. The Java program must convert the byte array between Java and iSeries formats. The data conversion classes provide methods for converting data. Parameters are added to the ProgramCall object as a list.
Example: Using ProgramParameter
The following example shows how to use the ProgramParameter object to pass parameter data.
// Create an AS400 object AS400 sys = new AS400("mySystem.myCompany.com"); // My program has two parameters. // Create a list to hold these // parameters. ProgramParameter[] parmList = new ProgramParameter[2]; // First parameter is an input // parameter byte[] key = {1, 2, 3}; parmList[0] = new ProgramParameter(key); // Second parameter is an output // parameter. A four-byte number // is returned. parmList[1] = new ProgramParameter(4); // Create a program object // specifying the name of the // program and the parameter list. ProgramCall pgm = new ProgramCall(sys, "/QSYS.LIB/MYLIB.LIB/MYPROG.PGM", parmList); // Run the program. if (pgm.run() != true) { // If the iSeries cannot run the // program, look at the message list // to find out why it didn't run. AS400Message[] messageList = pgm.getMessageList(); } else { // Else the program ran. Process the // second parameter, which contains // the returned data. // Create a converter for this // iSeries data type AS400Bin4 bin4Converter = new AS400Bin4(); // Convert from iSeries type to Java // object. The number starts at the // beginning of the buffer. byte[] data = parmList[1].getOutputData(); int i = bin4Converter.toInt(data); } // Disconnect since I am done // running programs sys.disconnectService(AS400.COMMAND);