Creating a Java Application That Uses CRNP

The
following example illustrates how to develop a simple Java application named CrnpClient that uses the CRNP. The application registers for event
callbacks with the CRNP server on the cluster, listens for the event callbacks,
and processes the events by printing their contents. Before terminating, the
application unregisters its request for event callbacks.

Keep the following points in mind when reviewing this example.

The sample application performs XML generation and parsing
with the JAXP (Java API for XML Processing). This example does not teach you
how to use the JAXP. JAXP is described in more detail at http://java.sun.com/xml/jaxp/index.html.

Now that your environment is configured, you can develop your application.

Get Started

In this part of the example, you create a basic class called CrnpClient, with a main method that parses the command line arguments
and constructs a CrnpClient object. This object passes
the command line arguments to the class), waits for the user to terminate
the application, calls shutdown on the CrnpClient, and then exits.

The constructor of the CrnpClient class needs to
execute the following tasks:

Set up the XML processing objects.

Create a thread that listens for event callbacks.

Contact the CRNP server and register for event callbacks.

Create the Java code that implements the preceding logic.

The following example shows the skeleton code for the CrnpClient class. The implementations of the four helper methods that are
referenced in the constructor and shutdown methods are shown later. Note that
the code that imports all the packages you need is shown.

Parse the Command Line Arguments

Define the Event Reception Thread

In the code, you need to ensure that event reception is performed in
a separate thread so that your application can continue to do other work while
the event thread blocks and waits for event callbacks.

Note –

Setting up the XML is discussed later.

In your code, define a Thread subclass called EventReceptionThread that creates a ServerSocket
and waits for events to arrive on the socket.

In this part of the example code, events are neither read nor processed.
Reading and processing events are discussed later. The EventReceptionThread creates a ServerSocket on a wildcard internetworking
protocol address. EventReceptionThread also keeps a
reference to the CrnpClient object so that EventReceptionThread can send events to the CrnpClient object to process.

Register and Unregister Callbacks

Opening a basic TCP socket to the registration internetworking
protocol and port

Constructing the XML registration message

Sending the XML registration message on the socket

Reading the XML reply message off the socket

Closing the socket

Create the Java code that implements the preceding logic.

The following example shows the implementation of the registerCallbacks method of the CrnpClient class (which is
called by the CrnpClient constructor). The calls to createRegistrationString() and readRegistrationReply()
are described in more detail later.

regIp and regPort are object members
that are set up by the constructor.

Generate the XML

Now that you have set up the structure of the application and have written
all the networking code, you write the code that generates and parses the
XML. Start by writing the code that generates the SC_CALLBACK_REG XML registration message.

An SC_CALLBACK_REG message consists of a registration
type (ADD_CLIENT, REMOVE_CLIENT, ADD_EVENTS, or REMOVE_EVENTS), a callback port,
and a list of events of interest. Each event consists of a class and a subclass,
followed by a list of name and value pairs.

In this part of the example, you write a CallbackReg
class that stores the registration type, callback port, and list of registration
events. This class also can serialize itself to an SC_CALLBACK_REG XML message.

An interesting method of this class is the convertToXml
method, which creates an SC_CALLBACK_REG XML message string
from the class members. The JAXP documentation at http://java.sun.com/xml/jaxp/index.html describes the code
in this method in more detail.

The implementation of the Event class is shown below.
Note that the CallbackReg class uses an Event class that stores one event and can convert that event to
an XML Element.

Create the Registration and Unregistration Messages

Now that you have created the helper classes that generate the XML messages,
you can write the implementation of the createRegistrationString method. This method is called by the registerCallbacks method, which is described in Register and Unregister Callbacks.

createRegistrationString constructs a CallbackReg object and sets its registration type and port. Then createRegistrationString constructs various events, using the createAllEvent, createMembershipEvent, createRgEvent, and createREvent helper methods.
Each event is added to the CallbackReg object after this
object is created. Finally, createRegistrationString calls
the convertToXml method on the CallbackReg
object to retrieve the XML message in String form.

Note that the regs member variable stores the command
line arguments that a user provides to the application. The fifth and subsequent
arguments specify the events for which the application should register. The
fourth argument specifies the type of registration, but is ignored in this
example. The complete code in Appendix G, CrnpClient.java Application shows how to
use this fourth argument.

Set Up the XML Parser

You have now created the networking and XML generation code for the
application. The final step is to parse and process the registration reply
and event callbacks. The CrnpClient constructor calls
a setupXmlProcessing method. This method creates a DocumentBuilderFactory object and sets various parsing properties
on that object. The JAXP documentation at http://java.sun.com/xml/jaxp/index.html describes this
method in more detail.

Parse the Registration Reply

To parse the SC_REPLY XML message that the CRNP server
sends in response to a registration or unregistration message, you need a RegReply helper class. You can construct this class from an XML
document. This class provides accessors for the status code and status message.
To parse the XML stream from the server, you need to create a new XML document
and use that document's parse method (the JAXP documentation at http://java.sun.com/xml/jaxp/index.html describes this
method in more detail).

Create the Java code that implements the preceding logic.

Note that the readRegistrationReply method uses the
new RegReply class.

Note that the retrieveValues method walks the DOM
tree in the XML document and pulls out the status code and status message.
The JAXP documentation at http://java.sun.com/xml/jaxp/index.html contains more detail.

Parse the Callback Events

The final step is to parse and process the actual callback events. To
aid in this task, you modify the Event class that you
created in Generate the XML so that this class can construct
an Event from an XML document and create an XML Element. This change requires an additional constructor (that
takes an XML document), a retrieveValues method, the addition
of two member variables (vendor and publisher), accessor methods for all fields, and finally, a print method.