Introduction


What is RMI

The Distributed Calculator:
A Simple Example

Scaling Up: Enterprise Concerns

Scaling the Calculator

RMI Future

Distributed Calculator

by Yanping Tan

Client (download code)
addServerImpl (download code)
addServerInterface (download code)
subtractServerImpl (download code)
subtractServerInterface (download code)
timeServerImpl (download code)
timeServerInterface (download code)
divideServerImpl (download code)
divideServerInterface (download code)

 

Client

//In this program, just simply demonstrate how remote methods
//are called. there are four methods add, subtract, time and divide
//that are resided in different server respectly. Client can invoke
//all of them.
import java.lang.*;
import java.io.*;
import java.rmi.*;
public class calClient {
public static void main(String arg[]){
int a, b, aa, bb, cc, dd;
String newLine = System.getProperty("line.separator");
try{
addInterface ai=(addInterface)Naming.lookup("addserver");
subtractInterface si=(subtractInterface)Naming.lookup("subtractserver");
timeInterface ti=(timeInterface)Naming.lookup("timeserver");
divideInterface di=(divideInterface)Naming.lookup("divideserver");
while(true){
System.out.println("Enter equation to calculate or quit to exit");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=(br.readLine()).trim();
if(s.trim().equals("quit")){
br.close();
break;
}

else{
int i=0;
while(i<s.length() && Character.isDigit(s.charAt(i)))
i=i+1;
String s1=s.substring(0, (i));
String s2=s.substring((i+1), s.length());
a=Integer.parseInt(s1);
b=Integer.parseInt(s2);
if (s.charAt(i)=='+'){
aa=ai.add(a,b); //call remote method
System.out.println("The add method is invoked in add server"+
newLine+a+"+"+b+" = "+aa+newLine+newLine);
}
if (s.charAt(i)=='-'){
bb=si.subtract(a,b); //call remote method
System.out.println("The subtract method is invoked in subtract server"+
newLine+a+"-"+b+" = "+bb+newLine+newLine);
}
if (s.charAt(i)=='*'){
cc=ti.time(a,b); //call remote method
System.out.println("The time method is invoked in time server"+
newLine+a+"*"+b+" = "+cc+newLine+newLine);
}
if (s.charAt(i)=='/'){
dd=di.divide(a,b); //call remote method
System.out.println("The divide method is invoked in divide server"+
newLine+a+"/"+b+" = "+dd+newLine+newLine);
}
}
}
} catch (NotBoundException e){
System.out.println("Catch NotBoundException error.");
System.exit(0);
} catch (RemoteException e) {
System.out.println("Add Error:" +e);
System.exit(0);
} catch (java.net.MalformedURLException e){
System.out.println("URL error:"+e);
System.exit(0);
}catch (IOException e){
System.out.println("Catch IOException error.");
}
}
}

 


addServerImpl

import java.net.*;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class addServerImpl extends UnicastRemoteObject implements addInterface{
public addServerImpl() throws RemoteException{
System.out.println("Initializing addserver");
}
public static void main(String arg[]){
System.setSecurityManager(new RMISecurityManager());
try{
addServerImpl ASI=new addServerImpl();
Naming.rebind("addserver", ASI);
System.out.println("registered with Registry");
} catch (RemoteException e){
System.out.println("I catch RemoteExceptiom Error: " + e);
} catch (java.net.MalformedURLException e){
System.out.println("URL Error:" + e);
}
}
public int add(int a, int b){
return (a+b);
}
}

 


addServerInterface

public interface addInterface extends java.rmi.Remote{
public int add(int a, int b) throws java.rmi.RemoteException;
}

 


subtractServerImpl

import java.net.*;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class subtractServerImpl extends UnicastRemoteObject implements subtractInterface{
public subtractServerImpl() throws RemoteException{
System.out.println("Initializing subtractServer");
}
public static void main(String arg[]){
System.setSecurityManager(new RMISecurityManager());
try{
subtractServerImpl SSI=new subtractServerImpl();
Naming.rebind("subtractserver", SSI);
System.out.println("registered with Registry");
} catch (RemoteException e){
System.out.println("I catch RemoteExceptiom Error: " + e);
} catch (java.net.MalformedURLException e){
System.out.println("URL Error:" + e);
}
}
public int subtract(int a, int b){
return (a-b);
}
}

 


subtractServerInterface

public interface subtractInterface extends java.rmi.Remote{
public int subtract(int a, int b) throws java.rmi.RemoteException;
}

 


timeServerImpl

import java.net.*;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class timeServerImpl extends UnicastRemoteObject implements timeInterface{
public timeServerImpl() throws RemoteException{
System.out.println("Initializing timeserver");
}
public static void main(String arg[]){
System.setSecurityManager(new RMISecurityManager());
try{
timeServerImpl TSI=new timeServerImpl();
Naming.rebind("timeserver", TSI);
System.out.println("registered with Registry");
} catch (RemoteException e){
System.out.println("I catch RemoteExceptiom Error: " + e);
} catch (java.net.MalformedURLException e){
System.out.println("URL Error:" + e);
}
}
public int time(int a, int b){
return (a*b);
}
}

 


timeServerInterface

public interface timeInterface extends java.rmi.Remote{
public int time(int a, int b) throws java.rmi.RemoteException;
}

 


divideServerImpl

import java.net.*;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class divideServerImpl extends UnicastRemoteObject implements divideInterface{
public divideServerImpl() throws RemoteException{
System.out.println("Initializing divideserver");
}
public static void main(String arg[]){
System.setSecurityManager(new RMISecurityManager());
try{
divideServerImpl DSI=new divideServerImpl();
Naming.rebind("divideserver", DSI);
System.out.println("registered with Registry");
} catch (RemoteException e){
System.out.println("I catch RemoteExceptiom Error: " + e);
} catch (java.net.MalformedURLException e){
System.out.println("URL Error:" + e);
}
}
public int divide(int a, int b){
return (a/b);
}
}

 


divideServerInterface

public interface divideInterface extends java.rmi.Remote{
public int divide(int a, int b) throws java.rmi.RemoteException;
}