Try OpenEdge Now
skip to main content
Java Open Clients
Using the Open Client Java OpenAPI to Directly Access the AppServer : Sample Java OpenAPI code : Single-run/singleton procedure example
 

Single-run/singleton procedure example

A procedure called CustomerInfo.p contains a user-defined function with the following definition.

User-defined function defined by CustomerInfo.p

FUNCTION GetTotalOrdersByNumber RETURNS INTEGER (CustNum AS INTEGER, threshold AS DECIMAL):
...
END.
The version of CustomerInfo.p in the following code example (Persistent procedure ABL for the OpenAPI)cannot be run as a single-run (or singleton) because it has a parameter in the main block. Creating another internal procedure to set CustNum would also not work, because a single-run procedure is deleted from the AppServer after each call. Context must be established for each call. So in the previous code example (User-defined function defined by CustomerInfo.p), CustNum is passed as a parameter to the user-defined function GetTotalOrdersByNumber instead.
The OpenAPI code, in the following code example, runs CustomerInfo.p as a single-run procedure and then calls its user-defined function. The code also shows how to access the procedure type using the getProcedureType() method.

OpenAPI code to run the single-run procedure

import com.progress.open4gl.javaproxy.*;
import com.progress.open4gl.Parameter;
import com.progress.open4gl.ProcedureType;

public class sampleClient
{  private static final java.lang.String tName = new String( "KM_client" );
 
 public static void singleRunProcedure()
   {
    try  // To catch all exceptions
    {
      // Connect to the AppServer
      Connection myConn = new Connection("","","");
      OpenAppObject dynAO = new OpenAppObject(myConn, "asbroker2");

      // Run procedure as single-run
      OpenProcObject dynPO = dynAO.createPO("CustomerInfo.p", 
ProcedureType.SINGLE_RUN);

      // Display procedure type using getProcedureType method
      System.out.println("Procedure object procedure type: "
                         + dynPO.getProcedureType());
      // Call UDF
      // First set up parameters
      int custNum = 3;
      java.math.BigDecimal threshold = new java.math.BigDecimal(1000);
      Integer retVal;

      // Set up input parameters
      ParamArray parms = new ParamArray(2);
      parms.addInteger(0, custNum, ParamArrayMode.INPUT);
      parms.addDecimal(1, threshold, ParamArrayMode.INPUT);

      // Set up return type
      parms.setReturnType(Parameter.PRO_INTEGER);

      // Run UDF
      dynPO.runProc("GetTotalOrdersByNumber", parms);

      // Get return value
      retVal = (Integer)(parms.getReturnValue());

      // Even though the single-run procedure has been deleted on the
      // AppServer, use _release() to clean up the resources on the client.
      dynPO._release();
      dynAO._release();
    } // try to catch all unexpected exceptions
    catch ( Exception e )
    {
        System.out.println("Exception in sample2()");
        System.out.println("Exception Message: " + e.getMessage());
        e.printStackTrace();
    }
  }
} // class
A singleton procedure can be run in the same way, with ProcedureType.SINGLETON substituted into the createPO method. Unlike a single-run procedure, a singleton procedure remains instantiated on the AppServer after execution, but as with a single-run procedure, you need to use _release() to clean up resources on the client side.