Download Java-RMI

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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. Use the notepad
program, create the following Java program and save it as “Calculator.java” .
___________________________________________________________
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)
SKR5302/SEM2 2010-2011/JAVA RMI/LAB
Page 1
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;
SKR5302/SEM2 2010-2011/JAVA RMI/LAB
Page 2
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 THE ANSWER.
SKR5302/SEM2 2010-2011/JAVA RMI/LAB
Page 3
• 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.
.
Exercise 2:
• Base on above exercises, design Any type of application such that 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)?
8. Consider an application that implements a remote dictionary. The dictionary offers
four operations: count to return the word count, insert to insert a word and its content,
delete to delete a word, and lookup to search for a word and return the content of the
word.
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
SKR5302/SEM2 2010-2011/JAVA RMI/LAB
Page 4