SDSU CS 696 Emerging Technologies: Distributed Objects
Spring Semester, 1998
Some Useful RMI Tools

To Lecture Notes Index
© 1998, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 21-Apr-98

Contents of Doc 8, Some Useful RMI Tools

  1. References
  2. Random Port for RMIRegistry
    1. A New Improved Version
  3. Shell program to kill all Your Java Processes

References


The UNIX Programming Environment, Kernighan & Pike, 1984

>http://www.eli.sdsu.edu/java-SDSU/sdsu.rmi.registry.UniVMRegistry.html

Doc 8, Some Useful RMI Tools Slide # 2

Random Port for RMIRegistry


The following program will run the rmiregistry on an open port and print to the screen which port the registry is running
import  java.net.ServerSocket;
import  sdsu.util.ProgramProperties;
import  java.io.IOException;

/**
 * This class starts the rmi registry on a random
 * open port. User can suggest a port using the -p flag
 */
public class StartRMIRegistry
   {
   public static void main( String[] args )
      {
      try
         {
         ProgramProperties flags = new ProgramProperties( args );
         int suggestedPort = flags.getInt( "p", 0);

         int port = getPort( suggestedPort );

         String randomPortRegistry = "rmiregistry " + port;

         Runtime.getRuntime().exec( randomPortRegistry );
         System.out.println( "rmiregistry running on port " + port );
         System.exit( 0 );
         }
      catch (IOException error )
         {
         System.out.println( "Had trouble trying to find a port\n " +
            error);
         }
      }
`
   /**
    * Return an open port on current machine. Try the 
    * suggested port first.
    * If suggestedPort is zero, just select a random port
    */
   private static int getPort( int suggestedPort ) throws IOException
      {
      ServerSocket openSocket;
      try
         {
         openSocket = new ServerSocket( suggestedPort );
         }
      catch (java.net.BindException portbusy)
         {
         // find random open port
         openSocket = new ServerSocket( 0 );
         }
      
      int port = openSocket.getLocalPort();
      openSocket.close();
      
      return port;
      } 
   }

Doc 8, Some Useful RMI Tools Slide # 3

A New Improved Version


I use UniVMRegistry in my later examples

This version of StartRMIRegistry will
start RMI Registry on an open port
you can suggest a port number to try first
start the UniVMRegistry
server is given the -Djava.rmi.server.hostName property

Flags:
-p for suggested port number
-d for domain other than sdsu.edu
-l (lower case L ) to turn on server logging



Doc 8, Some Useful RMI Tools Slide # 4

import  java.net.ServerSocket;
import  sdsu.util.ProgramProperties;
import  java.io.*;

/**
 * This class starts the rmi registry on a random
 * open port. User can suggest a port using the -p flag
 */
public class StartRMIRegistry
   {
   static final String DEFAULT_DOMAIN = "sdsu.edu";
   
   static final String PORT_KEY = "p";
   static final String DOMAIN_KEY = "d";
   static final String LOG_KEY = "l";
   
   public static void main( String[] args )
      {
      try
         {
         ProgramProperties flags = new ProgramProperties( args );
         int suggestedPort = flags.getInt( PORT_KEY, 0);
         
         int port = startRMI( suggestedPort );
         System.out.println( "rmiregistry running on port " + port );
         Thread.sleep( 1000);  //wait one second to let rmi start
         
         String domain = flags.getString( DOMAIN_KEY, 
                                    DEFAULT_DOMAIN );
         String host = getUnixHostName();
         String fullHostName = host + "." + domain;
         
         boolean logServer;
         if (flags.containsKey( LOG_KEY ) )
            logServer = true;
         else
            logServer = false;
         
         startUniVMRegistry( fullHostName, port, logServer );
         System.out.println( "UniVMRegistry running" );
         System.exit( 0 ); //don't let subprocesses keep us alive
         }
      catch (InterruptedException error )
         {
         System.out.println( "Process interrupted " + error);
         System.exit( 0 );   
         }
      catch (Exception error )
         {
         System.exit( 0 );
         }
      }

   private static void startUniVMRegistry( String host, 
                                          int  port, 
                                          boolean loggingOn ) throws IOException
      {
      try
         {
         String server = " -Djava.rmi.server.hostname=" + 
                           host.trim();
         
         String logging = " "; //default value is false
         if  (loggingOn)
            logging = " -Djava.rmi.server.logCalls=true";
            
         String portFlag = " -p=" + port;
         String javaClass = " sdsu.rmi.registry.UniVMRegistry";
         String command = "java" + server + logging + 
                              javaClass + portFlag + " &";
            
         Runtime.getRuntime().exec( command );
         }
      catch (IOException error )
         {
         System.out.println( "Had trouble starting UniVMRegistry" 
                              + error);
         throw error;
         }
      }

   private static int startRMI( int suggestedPort ) 
      throws IOException
      {
      try
         {
         int port = getPort( suggestedPort );

         String randomPortRegistry = "rmiregistry " + port;

         Runtime.getRuntime().exec( randomPortRegistry );
         return port;
         }
      catch (IOException error )
         {
         System.out.println( "Had trouble trying to find a port\n " +
            error);
         throw error;
         }
      }
      
   /**
    * Return an open port on current machine. Try the 
    * suggested port first.
    * If suggestedPort is zero, just select a random port
    */
   private static int getPort( int suggestedPort ) 
      throws IOException
      {
      ServerSocket openSocket;
      try
         {
         openSocket = new ServerSocket( suggestedPort );
         }
      catch (java.net.BindException portbusy)
         {
         // find random open port
         openSocket = new ServerSocket( 0 );
         }
      
      int port = openSocket.getLocalPort();
      openSocket.close();
      
      return port;
      } 

   private static String getUnixHostName() throws Exception
      {
      try
         {
         Process  hostName;
         BufferedReader  answer;

         hostName = Runtime.getRuntime().exec( "hostname" );
         answer = new BufferedReader( 
                        new InputStreamReader( 
                           hostName.getInputStream()) );

         hostName.waitFor();
         return answer.readLine().trim();
         }
      catch (Exception noName)
         {
         System.out.println( "Trouble accessing host name");
         throw noName;
         }
      }
   }



Doc 8, Some Useful RMI Tools Slide # 5

Shell program to kill all Your Java Processes


Put the following in a file, make it executable

Running the file will then kill all your Java processes

kill `/usr/bin/ps -o pid,comm -u$USER | egrep java | awk '{print $1}'`


visitors since 10-Feb-98