Effective SOA solutions take advantage of all the services available across the systems, which include the legacy systems in your organization. There are several ways you can integrate the existing services running on your legacy systems. This series of posts is about using the AS/400 services directly in your Java applications. I will be covering how to call RPG programs, AS/400 Commands and AS/400 DataQueues in your Java applications.
Fortunately IBM has provided a very nice easy to use library for communicating with the AS/400 server from Java. The IBM Toolbox for Java is a library of Java classes that give Java programs easy access to IBM iSeries data and resources. JT Open is the open source version of Toolbox for Java. You can go to JT Open link to download the full set of java libraries and some more details of how that can be used to easily communicate with the AS/400 server. There are several ways you can access the services on AS/400 server, most common are as follows.
- Executing a DB2 Stored Procedure on AS/400.
- Executing the AS/400 RPG programs from Java.
- Sending a message via AS/400 DataQueues to communicate with the Server.
- Using CommandCall class of JT Open library to execute the AS/400 Commands.
- Calling AS/400 Commands.
In this article, we shell see how the CommandCall class work to execute AS/400 Commands from Java. First thing we need are the details of an AS/400 Command that we will be executing from our Java client. For the purpose of this exercise we created a test AS/400 Command that executes a batch job on the AS/400 System. Here are the details of the program:
AS/400 Command: MYLIB/RUNMYJOB SOME(PARAM)
NOTE: You can call any iSeries server CL command.
package as400; import java.util.Date; import com.ibm.as400.access.AS400; import com.ibm.as400.access.AS400Message; import com.ibm.as400.access.CommandCall; /** * Test program to test the AS/400 Command from Java. */ public class AS400CommandCallTest { public static void main(String[] args) { String server="yourserver.company.com"; String user = "AS400USER"; String pass = "AS400PWRD"; String commandStr = "MYLIB/RUNMYJOB SOME(PARAM)"; AS400 as400 = null; try { // Create an AS400 object as400 = new AS400(server, user, pass); // Create a Command object CommandCall command = new CommandCall(as400); // Run the command. System.out.println("Executing: " + commandStr); boolean success = command.run(commandStr); if (success) { System.out.println("Command Executed Successfully."); }else{ System.out.println("Command Failed!"); } // Get the command results AS400Message[] messageList = command.getMessageList(); for (AS400Message message : messageList){ System.out.println(message.getText()); } } catch (Exception e) { e.printStackTrace(); }finally{ try{ // Make sure to disconnect as400.disconnectAllServices(); }catch(Exception e){} } System.exit(0); } }
And here is the out put of this program when you run it.
Executing: MYLIB/RUNMYJOB SOME(PARAM) Command Executed Successfully.
All the command parameters are passed as a space saparated list with the command. We check the value returned by command.run(commandStr); to see if the command was successful and display the appropriate message. Calling the command.getMessageList(); will always return a list of messages if gnerated by the AS/400 Command. They can be either failure or success messages depending on if the command succeded or failed.
Note that the command.run(commandStr); will only return data if a completion message was generated by the command you are executing. Some commands do not generate a completion message if they run successfully. If you want the CommandCall to return a message regardless of its success or failure, you could do the following:
Create a CL program that will run the necessary command(s). Include the SNDPGMMSG command as follows: SNDPGMMSG MSGID(CPF9898) MSGF(QCPFMSG) MSGDTA('put msg text here') MSGTYPE(*COMP) Note: The MSGTYPE parameter must be set to *COMP. Now create a CL command that will call your CL program.
What you have done here is actually wrapped the AS/400 Command inside your custom command. The new CL command will allow you to receive a meaningful completion message in your JAVA program.
These are the very basics of how to use the IBM Toolbox for Java to communicate with programs in AS/400. For more details and advanced topics you can consult the IBM programmers guide. To view or download the PDF version of this document, select IBM® Toolbox for Java™ (about 3100 KB).
No comments:
Post a Comment