The TimeServer
Home ] Up ] The Remote Interfaces ] [ The TimeServer ] The TimeClient ] Running the Example ]

 

 

Here's the TimeServer:

package rmiServer;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.UnknownHostException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Date;

import rmi.Time;
import rmiClient.TimeMonitor;
    
/**
 * TimeServer acts as a server for the remote "Time" service.
 */
public class TimeServer 
    extends UnicastRemoteObject
    implements Time
{
  /**
   * Constructor
   */
  public TimeServer() throws RemoteException
  { }
  
  /**
   * Registers the specified TimeMonitor
   */
  public void registerTimeMonitor(TimeMonitor monitor)
  {
    System.out.println( "Client registering a TimeMonitor" );   
    new TimeTicker(monitor).start();
    System.out.println( "Timer Started" );
  }
  
  /**
   * Main entry point
   */
  public static void main(String[] args)
  {
    System.out.println("Setting java.security.policy ...");   
    System.setProperty("java.security.policy", "policies/server.policy");
    
    // Use a security manager
    System.err.println("Setting SecurityManager ...");
    System.setSecurityManager( new SecurityManager() );
       
    try
    {
      TimeServer server = new TimeServer();
      System.out.print("Creating registry...");
      LocateRegistry.createRegistry(PORT);
      System.out.println("created");
      System.out.println("[Re]binding to registry...");
      Naming.rebind("rmi://" + HOST + ":" + PORT + "/" + "TimeServer", server);
      System.out.println("Binding done" );    
      System.out.println("Waiting for Client requests");
    }
    catch (UnknownHostException uhe)
    {
      uhe.printStackTrace();
    }
    catch (RemoteException re)
    {
      re.printStackTrace();
    }
    catch (MalformedURLException mue)
    {
      mue.printStackTrace();
    }
  }
  
  /// Private data ////
  private static final String HOST = "localhost";
  private static final int PORT = 2002;
}

/**
 * A TimeTicker class, used to update the client
 * on a regular basis.
 */
class TimeTicker extends Thread
{
  /**
   * Constructor
   */
  TimeTicker(TimeMonitor monitor)
  {
    m_monitor = monitor;
  }
  
  /**
   * The run method for the thread.
   */
  public void run()
  {
    boolean done = false;
    while (!done)
    {
      try
      {
        sleep(2000); // 2 seconds
        m_monitor.tellMeTheTime( new Date() );
      }
      catch ( Exception e )
      {
        done = true;
      }
    }
  }
  
  //// Private data ////
  private TimeMonitor m_monitor;
}

Note that:

  • We're creating our own registry, at a specified port
  • When a client request to register a TimeMonitor comes in, a new instance of TimeTicker is created, and started up in a new thread.
  • Periodically, this thread calls the TimeMonitor back at its tellMeTheTime() method.
 
The page was last updated February 19, 2008