Using Java Activities to Publish Information

Using several Java activities, you can invoke a method on a Java object and translate the resulting data into an XML document. The data can then be made available to other activities.

Procedure

  1. In the samples directory of the File Explorer view, expand palette > java > InvokeMethod and double-click tibco.bw.sample.palette.java.InvokeMethod to expand it in the Project Explorer. For more information, see Accessing Samples.
  2. In Project Explorer expand the tibco.bw.sample.palette.java.InvokeMethod project.
  3. Set the default ApplicationProfile to match the OS you are running on. For more information, see Setting the Default Application Profile.
  4. Fully expand the Processes directory and double-click PublishAllBalances.bwp to open it in the Process Editor pane.
  5. Click Run > Debug Configurations.
  6. At the left hand tree of Debug Configuration wizard, expand BusinessWorks Application and select BWApplication.
  7. Click the Applications tab and then click the Deselect All button if you have multiple applications. Select the check box next to tibco.bw.sample.palette.java.InvokeMethod.Application.
  8. Click Debug.
    This runs the sample in Debug mode.
  9. Click the Terminate icon to stop the process.
    A log is displayed in the Console view.

Result

Three log files are created in c:\tmp\InvokeMethod:

  • PublishAllBalances.log for the PublishAllBalances process: has customer account information, such as customerLastName, accountId, and checkingBalance.
  • PublishBalance.log for the PublishBalance process: has customer account details, such as checking and savings balances.
  • PublishJavaInfo.log for the PublishJavaInfo process: has system information such as, javaHome, javaVersion, and javaVendor.

Understanding the Configuration

The following three Java activities are used in this sample.

  • The Java Invoke activity invokes a method on a Java object.
  • The Java To XML activity translates data from a Java object into an XML document that can be later used in other activities.
  • The XML To Java activity supplies data to an XML schema to construct an instance of the desired Java object.

The source code for the Java objects, LocalBank, Account, Balance, and SystemProperties is in the src directory in the project.

This project has the following three processes:
  • PublishBalance
  • PublishAllBalances
  • PublishJavaInfo
The PublishBalance process executes as follows:
  1. The CreateLocalBank activity is a Java Invoke activity that constructs an instance of the LocalBank object. This object contains the getBalance(...) method that the process later invokes to retrieve the balance.
  2. The CreateAccount activity is an XML To Java activity that creates an instance of the Account object and sets the object's data using an XML schema.
  3. The getBalance activity is a Java Invoke activity that invokes the getBalance(...) method on the LocalBank object created by the CreateLocalBank activity. The Account object created by the CreateAccount activity is also passed as a parameter to the getBalance(...) method. The method returns an instance of the Balance object that contains the balances for the specified account.
  4. If the getBalance activity returns an exception, the error is published by the PublishException (if the exception is InvalidAccount) or PublishError activity (in the event of any other exceptions).
  5. If the getBalance activity executes without any exception, the Balance activity is a Java To XML activity that takes the Balance object produced by the getBalance activity and converts the object's data to an XML document.
  6. The PublishBalance activity takes the data produced by the Balance activity and publishes the account and balance information by writing it to a file in a tmp directory.
The PublishAllBalances process executes as follows:
  1. The getAllBalance activity is a Java Invoke activity that constructs an instance of the LocalBank object and invokes the getAllBalance() method on that object. The output of the method is an instance of the java.util.ArrayList class that contains a collection of all balances.
  2. The CollectionToArray activity invokes the toArray method of java.util.ArrayList to convert the collection to an array of objects.
  3. The group then iterates over each object in the array. The Balance activity takes the current object in the array and converts its data to an XML document. The PublishBalance activity publishes the data from the output of the Balance activity.
The PublishJavaInfo process allows a Java To XML activity to make the well-known Java system properties available as well as the host name and address. This process executes as follows:
  1. The SystemProperties activity is a Java To XML activity that constructs an instance of the SystemProperties object and provides the data in the class as an XML schema.
  2. The PublishJavaInfo activity uses the data produced by the SystemProperties activity and publishes the Java information by writing it to a file in a tmp directory.

The filenames used in this sample are specified in the Module Descriptors > Module Properties.

Troubleshooting

  • If the Output tab of Java To XML activity is not populated, then press the Reload button next to the Class Name in Java To XML activity.
  • If any problem markers are visible in the project, close and re-open the project or import the project in a clean new workspace.