Java Activation Model Example
Capability: Remote activation of an object.
Involves the Registry and the Activator.
// Imports
import
import
import
import
import
import
import
import
import
import
[Link];
[Link];
[Link];
[Link];
[Link];
[Link].*;
[Link];
[Link];
[Link];
[Link];
// A class of object that can be activated remotely. The class
// must extend the Activatable class
public class RemoteActivatableObject
extends Activatable
implements RemoteInterface
{
// The constructor must have this signature
public RemoteActivatableObject(
ActivationID id, MarshalledObject data)
throws RemoteException
{
// Register the object with the activation system
super(id, 0);
}
// Test method
public String message(String message) throws RemoteException
{
String returnString = "Thanks for your message:"+message;
[Link]("Returning:"+returnString);
return "Thanks for your message:"+message;
}
1
// Main method
public static void main(String[] args) throws Exception
{
[Link](new RMISecurityManager());
// Define the location were the object can be found
[Link] location =
new [Link]("file:/src/se4/");
// Create a CodeSource object. 2nd param. is a PK, ignored here
CodeSource source = new CodeSource(location, null);
// Get the properties of the system
Properties props =
(Properties)[Link]().clone();
// Create an activation group descriptor and a group ID
ActivationGroupID agid =
[Link]().registerGroup(
new ActivationGroupDesc(props));
// Marshall the parameters
MarshalledObject data = null;
// Create an activation descriptor
ActivationDesc desc =
new ActivationDesc (
agid, "RemoteActivatableObject", source, data);
// Enter descriptor into registry
RemoteInterface remoteInterface =
(RemoteInterface) [Link](desc);
// Publish the interface in the naming service
[Link]("ServerTest", remoteInterface);
}
}
Compilation and execution
// compile the server
javac [Link]
// generates stubs
rmic RemoteActivatableObject
// start the registry
start rmiregistry
// start the RMI daemon,
// instantiates objects when required
rmid
// initialize the server
java RemoteActivatableObject
// start a client
java RemoteClient
Source
[Link], Using Java 2 Platform, Que, 1998.