The K Zone, Home

About Me

K View

Projects

Papers

Gallery

Email

 

 

 
 

The Significance of the Java Language in Distributed Computing - Page 4

 

Back to Contents Page

 
 

 

Java RMI (Remote Method Invocation)

Implementation
RMI implementation involves the following steps:

1. Write and compile Java code for interfaces
2. Write and compile Java code for implementation classes
3. Generate Stub and Skeleton class files from the implementation classes
4. Write Java code for a remote service host program
5. Develop Java code for RMI client program
6. Install and run RMI system

Using a basic calculator as an example, the code implementation of these steps is as follows:

1. Interfaces

public interface Calculator
extends java.rmi.Remote {
public long add(long a, long b)
throws java.rmi.RemoteException;

public long sub(long a, long b)
throws java.rmi.RemoteException;
}

 

2. Implementation

public class CalculatorImpl
extends
java.rmi.server.UnicastRemoteObject
implements Calculator {

// Implementations must have an
//explicit constructor
// in order to declare the
//RemoteException exception
public CalculatorImpl()
throws java.rmi.RemoteException {
super();
}

public long add(long a, long b)
throws java.rmi.RemoteException {
return a + b;
}

public long sub(long a, long b)
throws java.rmi.RemoteException {
return a - b;
}
}

 

3. Stubs and Skeletons
Using the rmic compiler, generate the stub and skeleton files.

>rmic CalculatorImpl

 

4. Host Server
Remote RMI services must be hosted in a server process:

import java.rmi.Naming;

public class CalculatorServer {

public CalculatorServer() {
try {
Calculator c = new CalculatorImpl();
Naming.rebind("rmi://localhost:1099/CalculatorService", c);
} catch (Exception e) {
System.out.println("Trouble: " + e);
}
}

public static void main(String args[]) {
new CalculatorServer();
}
}

 

5. Client
Here is the source code for the client:

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;

public class CalculatorClient {

public static void main(String[] args) {
try {
Calculator c = (Calculator)
Naming.lookup(
"rmi://localhost
/CalculatorService");
System.out.println( c.sub(4, 3) );
System.out.println( c.add(4, 5) );
}
catch (MalformedURLException murle) {
System.out.println();
System.out.println(
"MalformedURLException");
System.out.println(murle);
}
catch (RemoteException re) {
System.out.println();
System.out.println(
"RemoteException");
System.out.println(re);
}
catch (NotBoundException nbe) {
System.out.println();
System.out.println(
"NotBoundException");
System.out.println(nbe);
}
catch (
java.lang.ArithmeticException
ae) {
System.out.println();
System.out.println(
"java.lang.ArithmeticException");
System.out.println(ae);
}
}
}

To run the system you need to run three consoles, one for the rmiregistry:

>rmiregistry

one for the server:

>javac CalculatorServer

and one for the client:

>javac CalculatorClient

If successful, the system should output the results - 1 and 9.


Overview
RMI offers a number of benefits, key amongst them is its straightforward implementation and support for peer-to-peer communication. Peer-to-peer is when client objects can invoke methods on the server object and the server object can invoke methods on the client object. RMI achieve implements peer-to-peer using stubs. The client object invokes a method on the server object and passes a stub pointing back to the client thus rendering the client a server.
Equally beneficial is RMI's use of the Secure Socket Layer (SSL) for transaction security.
RMI does possess some disadvantages that limit its range outside of Java applications. In particular the fact that RMI's architecture only supports Java-to-Java communication and is restricted to using only the RMI propriety transport protocol. As with Java IDL, RMI suffers from a limited number of services.

 

Next Page

 

 

Home | About Me | K View | Projects | Papers | Gallery | Email