Previous Topic: Output ParametersNext Topic: Resource for Running Invoke Java Operator Example


Java Example

The following example is located in the Examples section of the Required Main Method field.

/*
The Main Method is used to invoke objects and methods defined in the Java SDK or in external Jars.
The Main Method consists of normal Java statements and expressions.
You can also:
- Define your own methods and use them inside the Main Method
- Pass input parameters to the Main Method
- Save output variables in the Operator's dataset at the end of execution of the Main Method
- Use a logger object in the Main Method

Typically, you would complete all class definitions in external Jars and list them in the External Jar Paths of the operator, then initialize and use these objects in the Main Method.
*/

/*
Below is an example on how to initialize and use a MyAccount object, which is defined in an external Jar file.
Operator Configuration:
1. Specify the path: 
“Invoke_Java_Op_Example_Jars/MyAccount.jar” in the operator's list of External Jar Paths. MyAccount.jar contains the ca.tech.pam.MyAccount class, which is used in the code below. During the installation of CA Process Automation, MyAccount.jar is uploaded as a User Resource.
2. Specify a dataset variable of type Date as the first object in the list of Input Parameters of the Operator. 
    This Parameter can be accessed in the Main Method as args[0].
3. Specify a dataset variable of type Integer and value 100 as the second object in the list of Input Parameters of the Operator. 
    This Parameter can be accessed in the Main Method as args[1].
4. Specify the variable name acct (without quotes) as the first object in the list of Output Variable Names of the Operator.
    acct is created in the Main Method as a MyAccount object, hence at the end of execution of the operator, acct will be saved in the operator's dataset as a variable of type JavaObject.
5. Configure the operator to use a logger, set 'Log File Path' to a local file path, 'Log Level' to Info, 'Append to Log File?' to false, and 'Log Data Without Logging Info?' to true.
*/

// Import the classes that you want to use
import ca.tech.pam.MyAccount;

// Note: no need to import StringBuffer and Date (used below) because they are part of the
// automatically imported packages (full list of these packages is provided in the documentation)
// import java.lang.StringBuffer;
// import java.util.Date;

// Initialize MyAccount object
// Note that the MyAccount constructor is defined in the external jar as:
// public MyAccount(Date date, int balance)
MyAccount acct = new MyAccount(args[0], args[1]);

// Use the public methods of the MyAccount object
// Note that addFunds is defined in the external jar as:
// public int addFunds (int amnt)
acct.addFunds(34);

// Note that subFunds is defined in the external jar as: 
// public int subFunds (int amnt)
acct.subFunds(10);

// Define your own method
String getStatement(MyAccount acc) {
    StringBuffer strBuff = new StringBuffer("Account Balance: " + acc.getBalance());
    Date dt = new Date(System.currentTimeMillis());
    strBuff.append(" on date: " + dt);
    return strBuff.toString();
}

// Use the method you defined and also print the statement using the 'logger' object that you
// setup in the 'Logger' page of the operator
logger.info(getStatement(acct));

// At the end of execution of the operator:
// acct will be saved in the operator's dataset as a variable of type JavaObject.
// The logger's log file will contain the message:
// 	Account Balance: 124 on date: Thu Aug 22 11:27:29 EDT 2013
// 	(The message includes the correct date and time of execution).