Using a MarshalledObject to create persistent data


This page should not be the first activation tutorial that you read. This tutorial assumes that you have already read one or more of the three introductory activation tutorials: In the case of a UnicastRemoteObject, it is easy to pass command-line arguments to the implementation class, because the server program that received those arguments is always running during the lifetime of the remote object implementation. For activatable objects, however, the setup class may exit immediately after registering the activation descriptor with the RMI daemon and registering the stub with the rmiregistry.

The MarshalledObject class provides a flexible mechanism for passing persistence or initialization data through the ActivationDesc, registered with rmid, rather than hard-coding values into the implementation's class file.


Note:  For the remainder of this tutorial, the terms "activatable object implementation", "activatable object," and "implementation" may be used interchangeably to refer to  the class, examples.activation.MyPersistentClass, which implements a remote interface and is activatable.

In this tutorial the setup class, examples.activation.Setup4, does two new things:

In this example, if the persistentObjectStore.ser file exists, the activatable object implementation is initialized with the persistent data from the file.  Otherwise, if the file does not exist,  the activatable object initializes itself as though this is the first time a client has tried to send data.

The client program, examples.activation.Client4,  passes a vector of transaction-like data to the activatable object, and that data is added to the implementation object's vector. Each time a client calls the implementation (to add more transaction data), the activatable implementation stores its state (writes the vector) out to the file specified by the MarshalledObject.

This tutorial is organized as follows:

The files needed for this tutorial are: You may notice that while the client code is included, it is not discussed in a step-by-step manner, like the implementation and setup classes. The reason for this omission, is that the client code for activatable objects is no different than the RMI client code for accessing non-activatable remote objects.  Activation is strictly a server-side implementation decision.

For all of the source code used in the activation tutorials, you may choose from these formats:


Creating the remote interface

Create an interface that describes each of the methods that you would like to call remotely. For this example, the remote interface will be examples.activation.YetAnotherRemoteInterface.  There are three steps to create a remote interface:

  1. Make the appropriate imports in the interface
  2. Extend java.rmi.Remote
  3. Declare each of the methods that may be called remotely

Creating the implementation class

For this example, the implementation class will be examples.activation.MyPersistentClass. There are five steps to create an activatable implementation class that uses a MarshalledObject:


Creating the "setup" class

The job of the "setup" class is to create all the information necessary for the activatable class, without necessarily creating an instance of the remote object. For this example, the setup class will be examples.activation.Setup4.

The setup class passes information about the activatable class to rmid, registers a remote reference (an instance of the activatable class's stub class) and an identifier (name) with the rmiregistry, and then the setup class may exit.  There are six steps to create a setup class:

  1. Make the appropriate imports
  2. Install a SecurityManager
  3. Create an ActivationDesc instance
  4. Declare an instance of your remote interface and register with rmid
  5. Bind the stub to a name in the rmiregistry
  6. Quit the setup application
  7. Step 1:
    Make the appropriate imports in the setup class


Compile and run the code

There are six steps to compile and run the code:

  1. Compile the remote interface, implementation, client and setup classes
  2. Run rmic on the implementation class
  3. Start the rmiregistry
  4. Start the activation daemon, rmid
  5. Run the setup program
  6. Run the client
  7. Step 1:
    Compile the remote interface, implementation, client and setup classes

    % javac -d . YetAnotherRemoteInterface.java
    % javac -d . MyPersistentClass.java
    % javac -d . Client4.java
    % javac -d . Setup4.java

    Step 2:
    Run rmic on the implementation class

    % rmic -d . examples.activation.MyPersistentClass

    Step 3:
    Start the rmiregistry


Copyright © 1998 Sun Microsystems, Inc. All Rights Reserved.