RMI Tutorial 1
RMI Tutorial 1
RMI Tutorial 1
RMI applications are divided into two kinds of programs: servers and clients. RMI servers create
some remote objects, and register with a lookup service, to allow clients to find them. Clients use
a remote reference to one or more remote objects in the server and then invokes methods on them.
RMI provides the mechanism by which servers and clients communicate and pass information
back and forth. Such an application is sometimes referred to as a distributed object application.
The following lab work shows you how create a Java RMI application, which perform the
calculation of two numbers.
Exercise 1:
• To create a RMI application, the first step is to design an interface. Logon to Windows
2000 system, open a MS-Dos window, change directory to d:\Jwork. Use the notepad
program, create the following Java program and save it as “Calculator.java” in
d:\Jwork directory.
___________________________________________________________
public interface Calculator
extends java.rmi.Remote {
public long add(long a, long b)
throws java.rmi.RemoteException;
throws java.rmi.RemoteException {
return a + b;
}
public CalculatorServer() {
try {
Calculator c = new CalculatorImpl();
Naming.rebind("rmi://localhost:1099/CalculatorService", c);
} catch (Exception e) {
System.out.println("Trouble: " + e);
}
}
try {
Calculator c = (Calculator)
Naming.lookup("rmi://localhost/CalculatorService");
System.out.println( "The substraction of "+num1 +" and "+
num2 +" is: "+ c.sub(num1, num2) );
System.out.println( "The addition of "+num1 +" and "+ num2 +"
is: "+c.add(num1, num2) );
System.out.println( "The multiplication of "+num1 +" and "+
num2 +" is: "+c.mul(num1, num2) );
System.out.println( "The division of "+num1 +" and "+ num2 +"
is: "+c.div(num1, num2) );
}
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);
}
}
}
__________________________________________________________
• Type “javac CalculatorServer.java” and “javac CalculatorClient.java” to
compile the client and server programs.
• Congratulations! We are now ready to test the RMI applications. Type “rmiregistry” to
start the RMI registry so that objects can be registered (Note: The MS-Dos window will
hang in there, it is ok!)
• Open another MS-Dos window, change to the same directory, type “java
CalculatorServer” to run the server program (Note: Again, the MS-Dos window will
hang in there, it is ok!)
• Open the third MS-Dos window, change to the same directory, type “java
CalculatorClient 2 4” to run the client program, comment on the results.
• Explain the purpose of every single statement in all 4 above Java programs. Write them
down to your logbook.
• So far, the client and server programs are all running on the same computer, although they
are running through networking services. Next, is to run the client and server programs on
different computers connected by real networks.
• Use FTP program to upload “Calculator.java”, “CalculatorImpl.java”, and
“CalculatorServer.java” to your university Unix account (consult lab instructor, if
you don’t know how to do it).
• Open a Telnet window to connect to your university Unix account. Type in following
commands to compile the programs and start the registry.
• Open another Telnet window to connect to your university Unix account, type “java
CalculatorServer” to run the server program.
• Modify the “CalculatorClient.java” program so that it can connect server program
that runs both in local machine and remote machines (How?).
• Type “javac CalculatorClient.java” to compile the modified client program.
• Type “java CalculatorClient 5 9” to run the client program, comment on the
results.
• Now, upload the “CalculatorClient.java” program to your Unix account. Run the
server program on local Windows machine and client program on remote Unix machine.
Comment the results.
Exercise 2:
• Base on above exercises, design a RMI Lottery application. Each time you run the client
program -- “java LotteryClient n”, the server program “LotteryServer” will
generate n set of Lottery numbers. Here n is a positive integer, representing the money you
will spend on Lottery in sterling pounds. You should write this program in a proper
engineering manner, i.e. there should be specifications, design (flow chart, FD, or pseudo
code), coding, test/debug, and documentation.
Questions:
• http://java.sun.com/docs/books/tutorial/rmi/index.html
• http://java.sun.com/j2se/1.4/docs/api/overview-summary.html