How can multiple VM's be spawned from within the same activation setup program?

Avi Kak

Sameer Tyagi has a wonderful example showing how multiple VM's can be spawned within the same activation setup program. Here is a minimalist version of that program, along the same lines as the minimalist "Hello" program for remote object activation.

The minimalist version shown below is instructive in the sense that it does not invoke security managers anywhere explicitly. That is, the RMISecurityManager is not set either in the server programs or in the client programs. Also, there is no specifying of the security policy system property for launching either the activation daemon rmid on the server side or the program HelloClient on the client side. Although they would be necessary for more general programs involving dynamic loading of classes, the essence of Sameer's example code can be illustrated without their invocation. However, note that you still need a policy file for the VM's you'd be launching in the activation setup program on the server. The security policy for the activated VM is needed only for the reason that the sayHello() method tries to find the name of the local host.

To make this code work on your machine, you'd need to make appropriate substitutions for the name rvl4.ecn.purdue.edu of my server machine and the name RMI13.d of my RMI directory on the server side.

////// server and client file: Hello.java   //////

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

//////  server file: HelloServer.java   //////

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 );

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

//////  server file: RegisterHelloServer.java  //////

import java.rmi.*;
import java.rmi.activation.*;
import java.util.Properties;

public class RegisterHelloServer {

  public static void main(String[] args) throws Exception
    Properties props = new Properties();
    props.put( "java.security.policy", "/home/rvl4/a/kak/RMI13.d/policy" );
    ActivationGroupID agi = ActivationGroup.getSystem().registerGroup(
                   new ActivationGroupDesc( props, null ) );
    ActivationDesc desc = new ActivationDesc( agi, "HelloServer", null, null, true );
    Hello stub = (Hello)Activatable.register(desc);
    Naming.rebind("HelloServer", stub);

   // create another one for a new VM:
    ActivationGroupID agi_2 = ActivationGroup.getSystem().registerGroup(
                   new ActivationGroupDesc( props, null ) );
    ActivationDesc desc_2 = new ActivationDesc( agi_2, "HelloServer", null, null, true );
    Hello stub_2 = (Hello)Activatable.register(desc_2);
    Naming.rebind("HelloServer2", stub_2);

//////        server file: policy       //////

grant codebase "file:/home/rvl4/a/kak/RMI13.d/" {
  permission java.security.AllPermission;

//////  server shellscript: activate.sh  //////

#! /bin/sh
sleep 1
java RegisterHelloServer

//////  client file: HelloClient.java  //////

import java.rmi.*;

public class HelloClient {
  public static void main(String args[]) 
    try {
      Hello obj = ( Hello ) Naming.lookup( "rmi://RVL4.ecn.purdue.edu/HelloServer" );
      // Spawn the second VM !!
      Hello obj_2 = ( Hello ) Naming.lookup( "rmi://RVL4.ecn.purdue.edu/HelloServer2" );
    } catch (Exception e) { e.printStackTrace(); }

//////   client bat file:  runclient.bat   //////

java HelloClient