Does anyone tell me the difference between the following two programs

There are two programs which use activation mechanism of RMI to instantiate remote objects.

The interface is

import java.rmi.Remote;

import java.rmi.RemoteException;

interface Product // shared by client and server

extends Remote

{

String getDescription() throws RemoteException;

}

The remote object is

import java.rmi.MarshalledObject;

import java.rmi.RemoteException;

import java.rmi.activation.Activatable;

import java.rmi.activation.ActivationID;

import java.rmi.server.*;

/**

This is the implementation class for the remote product

objects.

*/

public class ProductImpl extends UnicastRemoteObject implements Product {

private static final long serialVersionUID = 6613571299445532659L;

public ProductImpl(ActivationID id , MarshalledObject object)throws RemoteException{

//Activatable.exportObject(this , id , 0);

System.out.println("marshalledobject");

}

public ProductImpl(String n) throws RemoteException {

name = n;

System.out.println("no marshalledobject");

}

public String getDescription() throws RemoteException {

return "I am a " + name + ". Buy me!";

}

private String name;

}

ProductActivator class is

import java.io.File;

import java.rmi.MarshalledObject;

import java.rmi.RMISecurityManager;

import java.rmi.activation.Activatable;

import java.rmi.activation.ActivationDesc;

import java.rmi.activation.ActivationGroup;

import java.rmi.activation.ActivationGroupDesc;

import java.rmi.activation.ActivationGroupID;

import java.util.Properties;

/**

This server program activates two remote objects and

registers them with the naming service.

*/

public class ProductActivator {

public static void main(String args[]) {

try {

System.out.println("Constructing activation descriptors...");

Properties props = new Properties();

// use the server.policy file in the current directory

//props.put("java.security.policy", new File("server.policy").getCanonicalPath());

System.setSecurityManager(new RMISecurityManager());

ActivationGroupDesc group = new ActivationGroupDesc(props, null);

ActivationGroupID id = ActivationGroup.getSystem().registerGroup(

group);

MarshalledObject p1param = new MarshalledObject("Blackwell Toaster");

MarshalledObject p2param = new MarshalledObject("ZapXpress Microwave Oven");

String classDir = ".";

// turn the class directory into a file URL

// for this demo we assume that the classes are in the current dir

// we use toURI so that spaces and other special characters in file names are escaped

String classURL = new File(classDir).getCanonicalFile().toURI().toString();

//String classURL=System.getProperty("java.rmi.server.codebase");

ActivationDesc desc1 = new ActivationDesc(id, "ProductImpl",classURL, p1param);

ActivationDesc desc2 = new ActivationDesc(id, "ProductImpl",classURL, p2param);

Product p1 = (Product) Activatable.register(desc1);

Product p2 = (Product) Activatable.register(desc2);

System.out.println("Binding activable implementations to registry...");

} catch (Exception e) {

e.printStackTrace();

}

}

}

NewProductActivator class is

import java.io.IOException;

import java.rmi.MarshalledObject;

import java.rmi.Remote;

import java.rmi.activation.ActivationDesc;

import java.rmi.activation.ActivationException;

import java.rmi.activation.ActivationGroup;

import java.rmi.activation.ActivationGroupDesc;

import java.rmi.activation.ActivationGroupID;

import java.rmi.activation.ActivationID;

import java.rmi.activation.ActivationGroupDesc.CommandEnvironment;

import java.util.Properties;

public class NewProductActivator {

public static void main(String args[]) throws ActivationException,

IOException {

ActivationGroupID gid = null;

ActivationID aid = null;

Object proxy = null;

Properties properties = new Properties();

properties.put("java.security.policy", System

.getProperty("java.security.policy"));

properties.put("java.rmi.server.codebase", System

.getProperty("java.rmi.server.codebase"));

String[] options = new String[] { "-cp",

System.getProperty("java.class.path") };

CommandEnvironment cmdToExecute = new CommandEnvironment(null, options);

gid = ActivationGroup.getSystem().registerGroup(

new ActivationGroupDesc(properties, cmdToExecute));

MarshalledObject params = new MarshalledObject(null);

ActivationDesc desc = new ActivationDesc(gid, "ProductImpl", null,

params, true);

/* Register the desired service with the activation system */

aid = ActivationGroup.getSystem().registerObject(desc);

Remote serverStub = aid.activate(false);

}

}

The methods executed and the classes created in the two main() are the same except the last methods invoked.

Does anyone know the difference that the two programs do?

Thank you.

[5292 byte] By [Zheng-Daa] at [2007-10-2 21:14:59]
# 1

I didn't look through all the code and I won't unless you format it properly, but the second one has 'restart' = true when constructing the ActivationDesc. The first one doesn't (it omits the parameter which is false by default).

See the constructor of ActivationDesc for what this means.

ejpa at 2007-7-14 0:22:27 > top of Java-index,Core,Core APIs...
# 2

Sorry, I will try to format it.

The interface is

import java.rmi.Remote;

import java.rmi.RemoteException;

interface Product // shared by client and server

extends Remote

{

String getDescription() throws RemoteException;

}

Message was edited by:

Zheng-Da

Zheng-Daa at 2007-7-14 0:22:27 > top of Java-index,Core,Core APIs...
# 3

The remote object is

import java.rmi.MarshalledObject;

import java.rmi.RemoteException;

import java.rmi.activation.ActivationID;

import java.rmi.server.*;

/**

This is the implementation class for the remote product

objects.

*/

public class ProductImpl extends UnicastRemoteObject implements Product {

private static final long serialVersionUID = 6613571299445532659;

public ProductImpl(ActivationID id , MarshalledObject object)throws RemoteException{

//Activatable.exportObject(this , id , 0);

System.out.println("marshalledobject");

}

public ProductImpl(String n) throws RemoteException {

name = n;

System.out.println("no marshalledobject");

}

public String getDescription() throws RemoteException {

return "I am a " + name + ". Buy me!";

}

private String name;

}

Zheng-Daa at 2007-7-14 0:22:28 > top of Java-index,Core,Core APIs...
# 4

The ProductActivator , which is one program using activation mechanism of RMI to instantiate remote objects

import java.io.File;

import java.rmi.MarshalledObject;

import java.rmi.RMISecurityManager;

import java.rmi.activation.Activatable;

import java.rmi.activation.ActivationDesc;

import java.rmi.activation.ActivationGroup;

import java.rmi.activation.ActivationGroupDesc;

import java.rmi.activation.ActivationGroupID;

import java.util.Properties;

/**

This server program activates two remote objects and

registers them with the naming service.

*/

public class ProductActivator {

public static void main(String args[]) {

try {

System.out.println("Constructing activation descriptors...");

Properties props = new Properties();

// use the server.policy file in the current directory

//props.put("java.security.policy", new File("server.policy").getCanonicalPath());

System.setSecurityManager(new RMISecurityManager());

ActivationGroupDesc group = new ActivationGroupDesc(props, null);

ActivationGroupID id = ActivationGroup.getSystem().registerGroup(

group);

MarshalledObject p1param = new MarshalledObject("Blackwell Toaster");

MarshalledObject p2param = new MarshalledObject("ZapXpress Microwave Oven");

String classDir = ".";

// turn the class directory into a file URL

// for this demo we assume that the classes are in the current dir

// we use toURI so that spaces and other special characters in file names are escaped

String classURL = new File(classDir).getCanonicalFile().toURI().toString();

//String classURL=System.getProperty("java.rmi.server.codebase");

ActivationDesc desc1 = new ActivationDesc(id, "ProductImpl",classURL, p1param);

ActivationDesc desc2 = new ActivationDesc(id, "ProductImpl",classURL, p2param);

Product p1 = (Product) Activatable.register(desc1);

Product p2 = (Product) Activatable.register(desc2);

System.out.println("Binding activable implementations to registry...");

} catch (Exception e) {

e.printStackTrace();

}

}

}

Zheng-Daa at 2007-7-14 0:22:28 > top of Java-index,Core,Core APIs...
# 5

Another program, NewProductActivator

import java.io.IOException;

import java.rmi.MarshalledObject;

import java.rmi.Remote;

import java.rmi.activation.ActivationDesc;

import java.rmi.activation.ActivationException;

import java.rmi.activation.ActivationGroup;

import java.rmi.activation.ActivationGroupDesc;

import java.rmi.activation.ActivationGroupID;

import java.rmi.activation.ActivationID;

import java.rmi.activation.ActivationGroupDesc.CommandEnvironment;

import java.util.Properties;

public class NewProductActivator {

public static void main(String args[]) throws ActivationException,

IOException {

ActivationGroupID gid = null;

ActivationID aid = null;

Object proxy = null;

Properties properties = new Properties();

properties.put("java.security.policy", System

.getProperty("java.security.policy"));

properties.put("java.rmi.server.codebase", System

.getProperty("java.rmi.server.codebase"));

String[] options = new String[] { "-cp",

System.getProperty("java.class.path") };

CommandEnvironment cmdToExecute = new CommandEnvironment(null, options);

gid = ActivationGroup.getSystem().registerGroup(

new ActivationGroupDesc(properties, cmdToExecute));

MarshalledObject params = new MarshalledObject(null);

ActivationDesc desc = new ActivationDesc(gid, "ProductImpl", null,

params, true);

/* Register the desired service with the activation system */

aid = ActivationGroup.getSystem().registerObject(desc);

Remote serverStub = aid.activate(false);

}

}

Zheng-Daa at 2007-7-14 0:22:28 > top of Java-index,Core,Core APIs...
# 6

The ProductActivator uses

Product p2 = (Product) Activatable.register(desc2);

to register the remote object

But the NewProductActivator uses

aid = ActivationGroup.getSystem().registerObject(desc);

Remote serverStub = aid.activate(false);

to register the remote object

I wonder the difference between the two ways

Zheng-Daa at 2007-7-14 0:22:28 > top of Java-index,Core,Core APIs...
# 7

The ProductActivator uses

Product p2 = (Product) Activatable.register(desc2);

to register the remote object

But the NewProductActivator uses

aid = ActivationGroup.getSystem().registerObject(desc);

Remote serverStub = aid.activate(false);

to register the remote object

I want to know the difference between the two ways

Zheng-Daa at 2007-7-14 0:22:28 > top of Java-index,Core,Core APIs...
# 8

Well there is also the difference I pointed out before, but the difference here is that ProductActivator uses a simple API and NewProductActivator uses a non-simple API to register the activatable objects.

As a side benefit, NewProductActivator gets the ActivationID, but all it does with it is to activate the object immediately, which is probably not appropriate for a registration program which should be run only once. (The object will auto-activate anyway when it is used.) I suspect that it's only doing this to get the stub, but the stub is returned by Activatable.register() anyway.

Basically, apart from the auto-restart difference, the old code is preferable.

ejpa at 2007-7-14 0:22:28 > top of Java-index,Core,Core APIs...