BEA Logo BEA WebLogic Application Integration 2.0

  BEA Home  |  Events  |  Solutions  |  Partners  |  Products  |  Services  |  Download  |  Developer Center  |  WebSUPPORT

 

   WebLogic Application Integration Documentation   |   WebLogic Application Integration User Guide   |   Previous Topic   |   Next Topic   |   Contents   |   Index

Using Application Views by Writing Custom Code

 

This section contains information on the following subjects:

 


Before You Begin

The following prerequisites must have been met before you write custom Java code to implement a business process:

 


Introduction to Using Application Views by Writing Custom Code

Although the primary way to use application views in business processes is to use WebLogic Process Integrator (see Using Application Views in WebLogic Process Integrator), an alternate way is to write custom Java code to represent the business process. If you are a developer who uses the custom coding method, this section uses a simple example to demonstrate how to custom code your enterprise's business process.

For a thorough comparison of the two ways to use application views, see Deciding Which of the Two Methods to Use.

 


Steps for Using Application Views by Writing Custom Code

This section uses a concrete example class called SyncCustomerInformation to explain how to write custom code. In general, you must do the following two steps to create custom code that uses an application view in a business process:

About this Example

In the simple example used throughout this section, the following business logic is implemented:

An enterprise has a customer relationship management (CRM) system and an order processing (OP) system. You want a business process that coordinates the synchronization of customer information between these two systems. That means that whenever a customer is created on the CRM system, it should trigger the creation of a corresponding customer record on the OP system. The attached Java class SyncCustomerInformation implements this business logic.

This is not a sophisticated example. It does not cover everything you can do using custom code. It only demonstrates the basic steps you will take when you implement your own organization's business processes.

Use this example code as a template for custom coding your own business processes.

Prerequisites for this Example

This example assumes the following prerequisites are already met:

Writing the Java Class

When writing custom code, there must exist a Java class to represent each application required for the business process. Create the necessary Java classes if they do not exist already. This example calls for one application class called SyncCustomerInformation. Of course, your own code will use different variable names. To create the SyncCustomerInformation Java class:

  1. See Example Code for SyncCustomerInformation for the complete source code for the Java application class.

    Note: For your own projects, use the SyncCustomerInformation code as a template or guide. The SyncCustomerInformation example code is thoroughly commented.

  2. Make sure the code does the following things:

    1. Create code to listen for East Coast.New Customer.

    2. Obtain a reference to the NamespaceManager (variable name m_namespaceMgr) and ApplicationViewManager (variable name m_appViewMgr) within WebLogic. Accomplish this using a JNDI lookup from the WebLogic server.

    3. Using the NamespaceManager, obtain a reference to the "root" namespace by calling nm.getRootNamespace(). This reference is stored in a variable called root.

    4. Using the root variable, obtain a reference to the East Coast namespace by calling root.getNamespaceObject("East Coast"). This reference is stored into a variable called eastCoast.

    5. Using the eastCoast variable, obtain a temporary reference to the Customer Management ApplicationView and store it into a variable called custMgmtHandle.

    6. This custMgmtHandle temporary reference will be used to obtain an actual reference to an ApplicationView instance for Customer Management. Do this by calling the ApplicationViewManager as avm.getApplicationViewInstance (custMgmtHandle.getQualifiedName()). Store the returned reference into a variable called custMgmt.

    7. Begin listening for New Customer events by calling custMgmt.addEventListener("New Customer", listener), where listener is an object that can respond to New Customer events (see the WebLogic API for a full discussion of event listeners and the EventListener interface).

    8. Implement the onEvent method of the listener class used in the step above.

      When a New Customer event is received, the onEvent method of the listener is called.

      The onEvent method should then call a method to respond to the event. In this example, the onEvent method provides the event object that contains the data associated with the event. The method is called handleNewCustomer.

    9. Implement the handleNewCustomer method that will respond to the New Customer event.

      The handleNewCustomer method transforms the XML document in the event to the form expected by the East Coast.Order Processing.Create Customer service. This transformation may be performed using XSLT or manually using custom transformation code. The end result of the transformation is an XML document that conforms to the schema for the request document of the East Coast.Order Processing.Create Customer service. Store this document in a variable called createCustomerRequest.

      handleNewCustomer will then obtain a reference to an instance of the East Coast.Order Processing ApplicationView in the same way described for the East Coast.Customer Management ApplicationView. This reference is stored into a variable called orderProc.

      handleNewCustomer will then invokes the Create Customer service on the East Coast.Order Processing ApplicationView by calling orderProc.invokeService("Create Customer", createCustomerRequest). Recall that createCustomerRequest is the variable holding the request document for the Create Customer service. The response document for this service is stored in a variable named createCustomerResponse.

      handleNewCustomer is finished and returns, leaving itself ready to handle the next incoming New Customer event.

      When you are finished, a new Java class exists called SyncCustomerInformation. This class implements the Sync Customer Information business logic. This SyncCustomerInformation class uses the WebLogic API to get events from the CRM system and to invoke services on the OP system.

Example Code for SyncCustomerInformation

Below is the example code for the SyncCustomerInformation Java class. It implements the business logic for the scenario described in About this Example. Use this example code as a guide for writing your own custom code to implement your enterprise's business processes.

Listing 4-1 Full Class Source Code for SyncCustomerInformation.

package com.bea.wlai.test;

import java.util.Hashtable;
import javax.naming.*;
import java.rmi.RemoteException;
import com.bea.wlai.client.*;
import com.bea.wlai.common.*;
import com.bea.document.*;

/**
* This class implements the business logic for the 'Sync Customer Information'
* business process. It uses the WLAI API to listen to events from the CRM
* system, and to invoke services on the OP system. It assumes that there
* are two ApplicationViews defined and deployed in the 'East Coast'
* namespace. The application views and their required events and services
* are shown below.
*
* Customer Management
* events (New Customer)
* services (none)
*
* Order Processing
* events (none)
* services (Create Customer)
*/
public class SyncCustomerInformation
implements EventListener
{
/**
* Main method to start this application. No args are required.
*/
public static void
main(String[] args)
{
// Check that we have the information needed to connect to the server.

if (args.length != 3)
{
System.out.println("Usage: SyncCustomerInformation ");
System.out.println(" <server url> <user id> <password>");
return;
}

try
{
// Create an instance of SyncCustomerInformation to work with

SyncCustomerInformation syncCustInfo =
new SyncCustomerInformation(args[0], args[1], args[2]);

// Get a connection to WLAI

InitialContext initialContext =
syncCustInfo.getInitialContext();

// Get a reference to an instance of the 'East Coast.Customer Management'
// Application View

ApplicationView custMgmt =
syncCustInfo.getInstanceOfCustomerManagement();

// Add the listener for 'New Customer' events. In this case we have
// our application class implement EventListener so it can listen for
// events directly.

custMgmt.addEventListener("New Customer", syncCustInfo);

// Process up to 10 events and then quit.

syncCustInfo.setMaxEventCount(10);
syncCustInfo.processEvents();
}
catch (Exception e)
{
e.printStackTrace();
}

return;
}

/**
* EventListener method to respond to 'New Customer' events
*/
public void
onEvent(IEvent newCustomerEvent)
{
try
{
// Print the contents of the incoming 'New Customer' event.

System.out.println("Handling new customer: ");
System.out.println(newCustomerEvent.toXML());

// Handle it

IDocument response = handleNewCustomer(newCustomerEvent.getPayload());

// Print the response

System.out.println("Response: ");
System.out.println(response.toXML());

// If we have processed all the events we want to, quit.

m_eventCount++;
if (m_eventCount >= m_maxEventCount)
{
quit();
}
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Quitting...");
quit();
}
}

/**
* Handles any 'New Customer' event by invoking the 'Create Customer'
* service on the 'Order Processing' ApplicationView. The response
* document from the service is returned as the return value of this
* method.
*/
public IDocument
handleNewCustomer(IDocument newCustomerData)
throws Exception
{
// Get an instance of the 'Order Processing' ApplicationView.

ApplicationView orderPro

 

back to top previous page next page