Sample: Call an iSeries service program procedure from a Java application

This example illustrates how to use Java beans generated by the Program Call wizard to call an iSeries service program procedure from a Java application.

The Program Call wizard generates two types of Java beans. One type is a regular Java bean used by Java applications. This includes any standalone Java application or Java components of Web applications such as servlets and JSP pages. The other type can be used by the Web Services wizard to create a Web service. This example uses the generated Java beans to call an iSeries program procedure. You can also use the same service program and PCML from this example to create a Web service. See Sample: Create and test an iSeries Web service.

Time required

Allow 15 minutes to run the example with the supplied data and to review the output.

Before you begin

To run the examples, you need to restore the WHOLESALE library on your iSeries host from the savefile wholesale.savf. This savefile is in x:\WDSC\wdscsampl, where x is the drive on which you have installed the product. See the Related reference section Installing the sample libraries at the bottom of this page.

This example shows you how to call a service program procedure from a Java application. The service program used in this example is CWWSSRV, and the procedure is QryProdCost, which takes a product item number as an input argument and returns the product price as an output argument.

  1. Open a Java Perspective.
  2. Create a new Java project with the name JavaProject.
  3. Select JavaProject and click the Create Program Call Bean icon Create Program Call bean icon (picture of server) in the workbench menu bar. This launches the Program Call wizard.
  4. Click Add Program.
  5. Enter the following values in the Add Program page. You can click Browse to look up the program that you want to call from your iSeries server.
    Field Value
    Java bean name QueryProductCost
    Program object CWWSSRV
    Library WHOLESALE
    Program type *SRVPGM
    Entry point QryProdCost
    Return type void
    Thread safe false
  6. Click OK. This defines the program or procedure that you want to call.
  7. Enter the following values in the Add Parameter page:
    Field Value
    Parameter name item
    Data type packed decimal
    Length 5
    Precision 0
    Usage input
  8. Click OK. This defines the input parameter item, which is a packed decimal with length 5 and precision 0 decimal places.
  9. Define another parameter by entering the following values in the Add Parameter page:
    Field Value
    Parameter name price
    Data type packed decimal
    Length 7
    Precision 2
    Usage output
  10. Click OK. This defines output parameter price, which is packed decimal with length 7 and precision 2 decimal places.
  11. Click Next to proceed to the next page of the wizard.
  12. Enter query.product in the Package field to create the files in that package.
  13. Select Java application, and clear the Services check box.
  14. Click Next to proceed to the third page of the wizard and enter configuration information to be saved in the runtime configuration file.
  15. Under the Authentication tab, the Generate configuration file check box is selected by default. This saves your settings in a runtime configuration file. You can keep the default filename. Select Specify signon values and fill in your host name, user ID and password. Enable password encoding is selected by default, so your password is encoded in the runtime configuration file.
  16. Under the Library list tab, in the Library field, enter WHOLESALE and click Add. In the Current Library field, enter *USRPRF.
  17. Click Finish to exit the wizard.

The following files are created in JavaProject:

QueryProductCost.java is the Java bean that can be used in your Java application to call the iSeries program remotely. The Java bean generated for the Java application normally consists of these methods:

To create the Java class with the sample code:

  1. Right-click the query.product package and select New > Class.
  2. In the New Java Class wizard, enter QueryProductCostMain in the Name field, select the check box for public static void main(String[] args) under the Which method stubs would you like to create? area, and click Finish to create the file and open it in the editor view.
  3. Place the cursor at the end of
       public static void main(String[] args) {
    and press Enter to create a blank line.
  4. Start in the new blank line and add the following code prior to the closing braces:
           
    // Instantiate a QueryProductCost object. 
    QueryProductCost query = new QueryProductCost();
    
    // The QryProdCost procedure takes item as an input, 
    // and the equivalent Java data type for a packed 
    // decimal is java.math.BigDecimal. 
    // The item number of the product to query is 1.  
    java.math.BigDecimal item = new java.math.BigDecimal(1); 
    query.setItem(item);
    
     // Invoke the QryProdCost procedure on the iSeries server. 
    query.invoke();
    
     // After invocation, QryProdCost returns the price of  
    // item number 1 as a packed decimal, which is  
    // equivalent to the Java data type java.math.BigDecimal.
     java.math.BigDecimal price = query.getPrice();  
    
    // Print the price.  
    System.out.println("Price: "+price.toString());  
    
    // Disconnect the host connection.
    query.disconnect();
    System.exit(0);
  5. Right-click anywhere in the editor pane and select Save to save your changes.
  6. In the Package Explorer view, select QueryProductCostMain.java under query.product.
  7. In the menu bar, click the down arrow next to the Run icon Run icon  (circle containing triangle) and click Run As > Java Application.
  8. In the signon dialog, enter host name, your userid and password. The price of item 1 is displayed in the console.

You have successfully called a service program procedure from a Java application.

Related tasks
Calling an iSeries program from your Java application
Configuring your iSeries Java run time
Creating a Web service from an iSeries program or service program procedure
Developing Web services
Creating a Web service from a Java bean using the IBM WebSphere runtime environment
Creating a Web service from a Java bean using the IBM SOAP runtime environment
Related reference
Installing the sample libraries