HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"RMI: Pure Java Distributed Computing"
Volume: 4 Issue: 7, p. 58

	

Listing 1: 

/** ServerInterface 
* 
* This interface should be implemented by any "server" objects 
*   in your application.  By defining your own ServerInterface 
*   (or whatever you name the interface which contains all the 
*   remote methods), you can define the distributed behavior of 
*   a server object. 
* 
* Note : These are the servers "promised" methods. 
*/ 

import java.rmi.*; 

public interface ServerInterface extends Remote 
{ 
 // Returns a word of the day to client. 
 public String getWord () throws RemoteException; 
} //ServerInterface 

Listing 2: 

/** Server 
* 
*       In this example our server class is small and only 
*   does one thing: return a "word of the day". 
* 
*       Note : We must keep the server running in order to 
*   ensure any references are valid.  To this end, I have 
*   made the server Runnable and used an infinite loop. 
*/ 

import java.rmi.*; 
import java.rmi.server.*; 
import java.net.*; 

// JDK 1.2 : import java.security.*; 

public class Server extends UnicastRemoteObject implements 
serverInterface, Runnable 
{ 
 // public constants 
 public static final int WORDCOUNT = 5; 

 // private object variables 
 private String [] wordTable; 

 /** main 
 * 
 *       Program entry point - instatiates an instance of the 
 *   server and wraps it in a thread. 
 */ 
 public static void main (String args[]) 
 { 
  try 
  { 
   Thread serverthread = new Thread (new Server()); 
   serverthread.start(); 
  } //try 
  catch (Exception x) 
  { 
   x.printStackTrace(); 
   System.exit (1); 
  } //catch 
 } //main 

 /** Server 
 * 
 *       Default constructor will attempt to bind itself to 
 *   the registry and build a table of words to send 
 *   back to a requesting client. 
 */ 
 public Server () throws RemoteException 
 { 
  try 
  { 
   // Set the system security manager to a new 
   // RMISecurityManager! 

   // If using JDK 1.1.x, uncomment the following line 
   System.setSecurityManager (new RMISecurityManager()); 

   // If using JDK 1.2.x, uncomment the following line: 
   //System.setSecurityManager (new MySecurityManager()); 

   // Bind this object to the registry under 
   // the name "server". 
   Naming.rebind ("server", this); 
   System.out.println ("Server has bound itself to the 
registry."); 
   // Build the word table. 
   buildTable (); 
   System.out.println ("Words have been created."); 
  } //try 
  catch (MalformedURLException x) 
  { 
   System.out.println (x); 
   x.printStackTrace(); 
  } //catch 
 } //Server 

 /** buildTable 
 * 
 *       This method will allocate space for the word table and 
 *       assign some sample "words of the day". 
 */ 
 public void buildTable () 
 { 
  wordTable = new String[WORDCOUNT]; 
  wordTable[0] = new String("Programming"); 
  wordTable[1] = new String("Analysis"); 
  wordTable[2] = new String("Llama"); 
  wordTable[3] = new String("Vortex"); 
  wordTable[4] = new String("Code"); 
 } //buildTable 

 /** getWord 
 * 
 *       Returns a random word to the client.  This word is extracted 
 *       from the wordTable array.  This is not the best way to get 
 *   a random value, but thats another story <g>. 
 * 
 *       Note: This method was "promised" by the ServerInterface 
 *   interface, and so it must be implemented here in order to 
 *   Server concrete. 
 */ 
 public String getWord () throws RemoteException 
 { 
  System.out.println ("Returning a word of the day."); 
  return wordTable[(int)(Math.random() * 100 % WORDCOUNT)]; 
 } //insertSpaces 

 /** run 
 * 
 *       Just loops indefinately, until the process is killed.  The 
 *   yield is put in for good measure. 
 */ 
 public void run () 
 { 
  System.out.println ("Server online."); 
  while (true) 
  { 
   Thread.yield(); 
  } //while 
 } //run 
} //Server 

// 
// THIS CLASS ALLOWS EVERYTHING AND IS USED ONLY TO ALLOW Server.java 
// TO COMPILE UNDER JDK1.2.x as well as JDK1.1.x -- PLEASE NEVER USE 
// IT UNLESS YOU KNOW WHAT YOUR DOING! :) 
// 

// If using JDK 1.2.x, uncomment the following class definition 
//class MySecurityManager extends SecurityManager 
//{ 
// public void checkPermission (Permission p) 
// { 
//  return ; 
// } //checkPermission 
//} //MySecurityManager 

Listing 3: 

import java.awt.*; 
import java.rmi.*; 
import java.io.*; 

public class Client 
{ 
 public static void main (String args[]) 
 { 
  // check if the user specified a server or not. 
  if (args.length != 1) 
  { 
   System.out.println ("Execution Failure : Bad Number 
of Arguments!\n"); 
   System.out.println ("Usage: java Client <servername>"); 
   System.exit (1); 
  } //if 

  // Attempt to lookup the server by using the Naming.lookup 
function.  If 
  // successful we will have an object of the servers class, 
at which point 
  // get can get our word of the day! 
  try 
  { 
   // Get the server instance from the registry. 
   ServerInterface si = (ServerInterface)Naming.lookup 
(args[0]); 

   // Get the word of the day and display it. 
   System.out.println ("The word for today is: " + 
si.getWord()); 
  } //try 
  catch (Exception x) 
  { 
   System.out.println (x); 
   System.exit (1); 
  } //catch 
 } //main 
} //Client 
  

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.