For activation, how do I get my objects to run in multiple VMs and not the VM of rmid.exe ??

Sameer Tyagi

The deal with spawning multiple VMs is the following bottom line. Refer to page 56 of the RMI specs ...

"All objects with the same groupID are activated in the same Java VM".


To start multiple VMs, the object must have a differnt ActivationGroupID.
and you must use one of the 2 constructors ActivationDesc(ActivationGroupID groupID, String className, String location, MarshalledObject data)
ActivationDesc(ActivationGroupID groupID, String className, String location, MarshalledObject data, boolean restart)

I have written an example for you that registers 2 Activable objects with the registry. The 2 objects have different ActivationGroupID's as you can see in the source.
Since the objects are lazily activated, you can see that 2 VMs are spawned when the client is run.
I have enclosed with this the complete example. The example contains 1. The remote interface
2. The remote interface implementation
3. The program to register the server.
4. The client program
5. The batch files to run both client and server.

package com.sameer;

import java.net.InetAddress;
import java.rmi.*;
import java.rmi.activation.*;

public class HelloServer  extends Activatable implements Hello {

   private static int counter=0;
   public HelloServer(ActivationID id, MarshalledObject data)
      throws RemoteException {
      super( id, 0 );
      System.out.println("Hello Server Constructor invoked" +counter);


   public String sayHello(){
	   String hostname=null;
			  hostname= InetAddress.getLocalHost().getHostName();
	          }catch (java.net.UnknownHostException who){}
      return  "Hello World from " + hostname;


package com.sameer;

public interface Hello extends java.rmi.Remote
   String sayHello() throws java.rmi.RemoteException;

package com.sameer;

import java.rmi.*;
import java.rmi.activation.*;
import java.util.Properties;
public class RegisterHelloServer {

      public static void main(String[] args) throws Exception {

      Properties policyFileLocation = new Properties();

      ActivationGroupDesc exampleGroup = new ActivationGroupDesc(policyFileLocation, null);
      ActivationSystem localActivationSystem = ActivationGroup.getSystem();
      ActivationGroupID agi = localActivationSystem.registerGroup(exampleGroup);

      //The activation group is what creates the activatible object in a JVM
      // Sets the activation group for the current JVM
      ActivationGroup.createGroup(agi, exampleGroup, 0);

      // Since the ActivationGroupID is not given in the constructor of desc, the
      // current JVMs activation group is used
      // create more ActivationGroupIDs since objects with the same ActivationGroupID are activated in the same VM
      //ActivationDesc(ActivationGroupID groupID, String className, Stringl ocation, MarshalledObject data, boolean restart)

        This creates in the current VM and the current identifier for the ActivationGroupID. If the ActivationGroupID does not
        exist a new one is created.
      	ActivationDesc desc = new ActivationDesc("com.sameer.HelloServer", "file:/export/home/whitney/java/classes/", null, true);

	  ActivationDesc desc = new ActivationDesc(agi, "com.sameer.HelloServer", "file:/export/home/whitney/java/classes/", null, true);
	  System.out.println("Gourp ID =" +desc.getGroupID());
	  Hello stub = (Hello)Activatable.register(desc);
	  Naming.rebind("HelloServer", stub);

	  // create another one for a new VM
	  ActivationGroupID agi_2 = localActivationSystem.registerGroup(exampleGroup);
	  ActivationDesc desc_2 = new ActivationDesc(agi_2, "com.sameer.HelloServer", "file:/export/home/whitney/java/classes/", null, true);
	  System.out.println("Gourp ID =" +desc_2.getGroupID());
	  Hello stub_2 = (Hello)Activatable.register(desc_2);
	  Naming.rebind("HelloServer2", stub_2);


package com.sameer;
import java.rmi.*;
public class HelloClient {

    private static String message = "";

    public static void main(String args[]) {

       try {
            Hello obj = (Hello) Naming.lookup("/HelloServer");

 // Spawn the second VM !!
            Hello obj_2 = (Hello) Naming.lookup("/HelloServer2");
        } catch (Exception e) {
	    System.out.println("HelloClient exception: " +  e.getMessage());


grant {
	// Allow everything for now
	permission java.security.AllPermission;

0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact