Home : Course Map : Chapter 18 :
RMI Example Server
JavaTech
Course Map
Chapter 18

Introduction
Server Demo
Client Demo
Demo Support
Resources
Exercises

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

The code for the server classes RMIExampleServer and RMIExampleImpl and the interface RMIExampleInterface described in the book is shown below. Also, the policy files and support files are includes.

Note that RMIExampleServer belongs to the package javatech.rmi18.server.impl and so must reside in the client subdirectory as shown by this path:

javatech
  |
  |__ rmi18
       |
       |__ server
            |
            |-- impl

RMIExampleServer.java

// RMIExampleServer.java

package javatech.rmi18.server.impl;

import java.net.MalformedURLException;
import java.rmi.*;

/** The server class that will get run to start up the remote
  * implementation class and bind it into the RMI registry. **/
public class RMIExampleServer
{
  public static void main (String[] args) {
    
    // Create and install a security manager
    if (System.getSecurityManager() == null) {
      System.setSecurityManager(new RMISecurityManager());
    }

    // Instantiate an RMIExampleImpl implementation class and bind
    // into the registry.
    try {
      // Instantiate.
      System.err.println ("Constructing an RMIExampleImpl");
      RMIExampleImpl impl = new RMIExampleImpl ();

      // Bind into the registry using the java.rmi.Naming API.
      System.err.println ("(re)binding it");
      Naming.rebind ("//localhost:3001/rmi-example-server", impl);
      System.err.println (
        "RMIExampleServer ready and waiting on clients"
      );
    }
    catch (RemoteException re) {
      System.err.println (re);
    }
    catch (MalformedURLException mue) {
      System.err.println (mue);
    }
  } // main
} // class RMIExampleServer

 

In the same directory as above:

RMIExampleImpl.java

// RMIExampleImpl.java

package javatech.rmi18.server.impl;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import javatech.rmi18.server.*; // This is where the remote interface lives

/** The implementation class that implements the remote methods declared
  * in the RMIExampleInterface interface. **/
public class RMIExampleImpl
  extends UnicastRemoteObject
  implements RMIExampleInterface
{
  // The default constructor will not work since UnicastRemoteObject,
  // which we extend, throws RemoteException, which the default
  // constructor provided by the compiler does not. Therefore, we must
  // implement a default constructor that throws RemoteException even
  // if the constructor does nothing at all. It will, of course,
  // automatically call its superclass constructor.

  /** Constructor. Must be declared to throw RemoteException. **/
  public RMIExampleImpl () throws RemoteException {}

  /** Echoes on the server side the input string provided by the
   * client. **/
  public void method1 (String s) {
    System.out.println ("RMIExampleImpl.method1: " + s);
  } // method1

  /** Adds the input parameters and returns the sum. **/
  public int add (int a, int b) {
    System.out.println ("RMIExampleImpl.add: computing sum of " +
              a + " and " + b);
    return a + b;
  } // add

  /** Does something locally. There is no way to call this method
    * remotely since it does not appear in the list of remote methods
    * in the RMIExampleInterface interface. **/
  public void doSomethingLocal (float x) {
    System.err.println ("RMIExampleImpl.doSomethingLocal: local only");
  } // doSomethingLocal
} // class RMIExampleImpl

 

The interface is in the following path:

javatech
  |
  |__ rmi18
       |
       |__ server

RMIExampleInterface.java

// RMIExampleInterface.java

package javatech.rmi18.server;

import java.rmi.Remote;
import java.rmi.RemoteException;

/** The remote interface for use with the RMI Example in chapter 18. **/
public interface RMIExampleInterface extends Remote {
  public void method1 (String s) throws RemoteException;
  public int add (int i, int j) throws RemoteException;
} // interface RMIExampleInterface

 

 

See the RMI example support files page for the policy file plus the build and run command files for the client.

Most recent update: Oct.12, 2005

  
  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.