rmi tutorial 1

4
School of Engineering Distributed Systems and Internetworking Dr. Perry XIAO Copyright © 2002, South Bank University 1 DSI Lab: Distributed Computing With Java RMI Remote Method Invocation (RMI) is a distributed systems technology that allows one Java Virtual Machine (JVM) to invoke object methods that will run on another JVM located elsewhere on a network. This technology is extremely important for the development of large-scale systems, as it makes it possible to distribute resources and processing load across more than one machine. 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; public long sub(long a, long b) throws java.rmi.RemoteException; public long mul(long a, long b) throws java.rmi.RemoteException; public long div(long a, long b) throws java.rmi.RemoteException; } __________________________________________________________ The second step is to implement the interface. Create the following Java program and save it as “CalculatorImpl.java”. ___________________________________________________________ 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)

Upload: arka-ray

Post on 07-Apr-2015

141 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: RMI Tutorial 1

School of Engineering Distributed Systems and Internetworking

Dr. Perry XIAO Copyright © 2002, South Bank University 1

DSI Lab: Distributed Computing With Java RMI Remote Method Invocation (RMI) is a distributed systems technology that allows one Java Virtual Machine (JVM) to invoke object methods that will run on another JVM located elsewhere on a network. This technology is extremely important for the development of large-scale systems, as it makes it possible to distribute resources and processing load across more than one machine. 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; public long sub(long a, long b) throws java.rmi.RemoteException; public long mul(long a, long b) throws java.rmi.RemoteException; public long div(long a, long b) throws java.rmi.RemoteException; } __________________________________________________________

• The second step is to implement the interface. Create the following Java program and save it as “CalculatorImpl.java”. ___________________________________________________________ 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)

Page 2: RMI Tutorial 1

School of Engineering Distributed Systems and Internetworking

Dr. Perry XIAO Copyright © 2002, South Bank University 2

throws java.rmi.RemoteException { return a + b; } public long sub(long a, long b) throws java.rmi.RemoteException { return a - b; } public long mul(long a, long b) throws java.rmi.RemoteException { return a * b; } public long div(long a, long b) throws java.rmi.RemoteException { return a / b; } } __________________________________________________________

• Now you can compile the interface and its implementation. To do this, type “rmic CalculatorImpl”. It should produce following 4 new files:

CalculatorImpl.class Calculator.class CalculatorImpl_Stub.class CalculatorImpl_Skel.class

• Next, create a RMI server. Create the following Java program and save it as “CalculatorServer.java”. __________________________________________________________ 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(); } } __________________________________________________________

• Finally, create a RMI client. Type in the following Java program and save it as “CalculatorClient.java”. __________________________________________________________ import java.rmi.Naming; import java.rmi.RemoteException; import java.net.MalformedURLException; import java.rmi.NotBoundException;

Page 3: RMI Tutorial 1

School of Engineering Distributed Systems and Internetworking

Dr. Perry XIAO Copyright © 2002, South Bank University 3

public class CalculatorClient { public static void main(String[] args) { long num1 = Integer.parseInt(args[0]); long num2 = Integer.parseInt(args[1]); 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.

Page 4: RMI Tutorial 1

School of Engineering Distributed Systems and Internetworking

Dr. Perry XIAO Copyright © 2002, South Bank University 4

• 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:

1. What is Java RMI? 2. What are Stub and Skeleton programs in Java RMI? 3. How does Java RMI work? 4. What level does RMI fit into the TCP/IP 4 layer model? 5. Which protocol do RMI use, TCP or UDP? 6. Which port number does RMI use? 7. What is the difference between RMI and RPC (Remote Procedure Calls)?

Reference Web Sites:

• http://java.sun.com/docs/books/tutorial/rmi/index.html • http://java.sun.com/j2se/1.4/docs/api/overview-summary.html