File was not found on server.

Loading and Invoking Java Code Prior to Running any Services

Article ID: 1382
Last updated: 15 Sep, 2009
Article ID: 1382
Last updated: 15 Sep, 2009
Revision: 1
Views: 1790
Posted: 08 Dec, 2008
by --
Updated: 15 Sep, 2009



     Use Case


     Instructions for Running Example


Occasionally, Java services in the same project may need to access the same data, resource or logic. The Hydra Agent can load  and invoke Java code prior to running any of the project's services.

The use case we will consider here involves the use of a property file shared by two Java services in the same project. Although, each service could read the property file independently, this is time consuming if the file is large. We would prefer to read the file once prior to any of the services running. The overhead then occurs once when the agent starts rather than on each request to the service.

This example is similar to the PurchaseApproval Tutorial.  We have added a properties.xml file that contains information about the different vendors, including their name, credit limit and terms of payment.  We'll read this file into a static List of DataObjects before the agent loads the services.  As each request for a purchase comes in we need to validate the vendor and verify that the total of the purchase is within the credit limit for that vendor.
  The BPEL service, PO_VendorApproval, calls two Java services, GetVendorData and AmountOK. The service, GetVendorData, includes a static List and the public method, initService( ). initService( ) is executed as the agent is loading in projects. This method reads properties.xml into a DataObject. The vendor DataObjects are obtained from this root DataObject.

static final String INSTANCE_DOC = "properties.xml";
    static List list;
    public void initService() throws Exception {
            XMLDataAccessService das = XMLDataAccessServiceFactory.create();
            DataGraph dataGraph = das.load(new File(INSTANCE_DOC));
             DataObject root = dataGraph.getRootObject();
            DataObject vendors = root.getDataObject("vendors");
            list = vendors.getList("Vendor");

        catch (Exception e)
            System.out.println("Exception Thrown: " + e.getMessage());

The example also has a static getter method, getVendor( ) that returns the a DataObject for a vendor specified by name. Both the GetVendorDataOperation and the AmountOKOperation use the getVendor( ) method to obtain the data they need about the current vendor.


    static public DataObject getVendor(String byName)


        DataObject vendor;

        for (int index = 0; index < list.size(); ++index)


            vendor = (DataObject)list.get(index);

            if (byName.equalsIgnoreCase(vendor.getString("name.0/text.0")))


                return vendor;



        System.out.println("Vendor not found.");

        return null;



When a request comes in the first node in the flow is the GetVendorDataOperation. This service uses the vendor's name from the request to verify that the vendor is valid, i.e. that it is in the List of vendor DataObjects. The request is then passed on to the AmountOKOperation which totals the items in the purchase order. It accesses the List of vendor DataObject to determine the credit limit for the specified vendor. The total purchase must be less than the credit limit.


See the attachment below to download the example.


In this example the static data and the initService() method were included in the GetVendorData Service. Including them with the Service allowed us to use the generated ant build files without modifications. If you wanted to include an existing Java class, you could modify the build to include your class.

To build and deploy the process, execute the following commands

from a command window set with the Hydra environment:

1. ant

   This command runs the default target, which both builds and deploys

   the process. You can run these separately as 'ant build' and 'ant deploy'.

2. ant startServer

3. ant run

   This command tests the service by using the Hydra sendsoap utility to send

   three soap messages to the service. The messages are: AmazonNotOKMessage.xml,

   AmazonOKMessage.xml and eBayNotOKMessage.xml


See readme.txt  for the complete instructions and the expected output.


In Hydra Services Guide "Loading and Invoking Java Code Prior to Running any Services"


This article was:   Helpful | Not helpful
Report an issue
Article ID: 1382
Last updated: 15 Sep, 2009
Revision: 1
Views: 1790
Posted: 08 Dec, 2008 by --
Updated: 15 Sep, 2009 by
Attached files

Also listed in

Others in this category