Lab4

advertisement
Middleware Lab 4
Student Name: Joey Tawadrous
Questions
1. What is RMI?
RMI (remote method invocation) allows applications to call objects and methods
located remotely, sharing resources and processing load across systems. Unlike
other remote execution systems, that require only simple data types or defined
structures be passed to and from methods, RMI allows any Java object to be used.
One of the main advantages of RMI, is that it allows both client and server to
dynamically load new object types as required.
2. If the Server needs to support clients running on pre-5.0 VMs what extra step is
required in the compilation process? What does it generate? Give an example of its
usage?
 If your application needs to support such clients, you will need to generate
stub classes for the remote objects used in the application and deploy those
stub classes for clients to download.
 Clients running on pre-5.0 VMs generate stubs, skeletons, ties for remote
objects using either the JRMP or IIOP protocols. Also generates OMG IDL.
3. What is the default port on which the rmiregistry runs? Provide an example of how
to start the rmiregistry running on a different port.
 By default the application listens to port 1099.
 rmiregistry [port #].
 In addition the server is configured to talk to a particular rmiregistry port with
the jvm definition. –Dprotege.rmi.registry.port=[port #].
4. What is the purpose of the rmiregistry? What exactly does it do?
 The rmiregistry command creates and starts a remote object registry on a
specified port which is located on the current host. If the port is left out, the
registry is started on port 1099. However the rmiregistry command produces
no output and is typically run in the background. For example, remiregistry &
 A remote object registry is a bootstrap naming service that is used by RMI
servers on the same host and this is used to bind remote objects to names.
Also clients on local and remote hosts can then look up remote objects and
make remote method invocations.
5. Provide the interface definition for the calculator.
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Calculator extends Remote {
String sayHello() throws RemoteException;
int add(int Num1, int Num2) throws RemoteException;
int minus(int Num1, int Num2) throws RemoteException;
int divide(int Num1, int Num2) throws RemoteException;
int multiply(int Num1, int Num2) throws RemoteException;
double pi() throws RemoteException;
}
6. Provide the Servers implementation for the calculator’s methods.
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server implements Calculator {
public Server() {}
public String sayHello() {
return "Hello, world!";
}
public int add(int Num1, int Num2) {
System.out.println(Num1 + Num2);
return (Num1 + Num2);
}
public int minus(int Num1, int Num2) {
System.out.println(Num1 - Num2);
return (Num1 - Num2);
}
public int divide(int Num1, int Num2) {
System.out.println(Num1 / Num2);
return (Num1 / Num2);
}
public int multiply(int Num1, int Num2) {
System.out.println(Num1 * Num2);
return (Num1 * Num2);
}
public double pi() {
System.out.println(3.14);
return (3.14);
}
public static void main(String args[]) {
try {
Server obj = new Server();
Calculator stub = (Calculator) UnicastRemoteObject.exportObject(obj, 0);
// Bind the remote object's stub in the registry
Registry registry = LocateRegistry.getRegistry();
registry.bind("Calculator", stub);
System.err.println("Server ready");
}
catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
Download