Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
14-Apr-12
AnotherClass
returned object
computer 2
If the network is the computer, we ought to be able to put the two classes on different computers RMI is one technology that makes this possible
2
CORBA (Common Object Request Broker Architecture) has long been king
CORBA supports object transmission between virtually any languages Objects have to be described in IDL (Interface Definition Language), which looks a lot like C++ data definitions CORBA is complex and flaky
Microsoft supported CORBA, then COM, now .NET RMI is purely Java-specific
Java to Java communications only As a result, RMI is much simpler than CORBA
3
Overview
RMI
JDBC
CORBA
java.net
TCP/IP
Network OS
RMI Layers
Java Virtual Machine Client Object Java Virtual Machine Remote Object
Stub
Skeleton
Transport Layer
TCP
Transport Layer
Java makes RMI (Remote Method Invocation) fairly easy, but there are some extra steps To send a message to a remote server object,
The client object then has to marshal the parameters (prepare them for transmission)
Java requires Serializable parameters The server object has to unmarshal its parameters, do its computation, and marshal its response
Terminology
A remote object is an object on another computer The client object is the object making the request (sending a message to the other object) The server object is the object receiving the request As usual, client and server can easily trade roles (each can make requests of the other) The rmiregistry is a special server that looks up objects by name
rmic is a special compiler for creating stub (client) and skeleton (server) classes
Processes
The Client The Server The Object Registry, rmiregistry, which is like a DNS service for objects
Stub
Skeleton Server
RMI Flow
1. Server Creates Remote Object Client Virtual Machine 2. Server Registers Remote Object Client Server Virtual Machine Remote Object
1
Stub
Skeleton Server
2
RMI Flow
Client Virtual Machine Client Server Virtual Machine Remote 3. Client requests object from Registry Object 4. Registry returns remote reference (and stub gets created) Stub
3 4
Skeleton Server
RMI Flow
Client Virtual Machine Client
5 6 7
Stub
Skeleton Server
5. Client invokes stub method 6. Stub talks to skeleton 7. Skeleton invokes remote object Fred method Registry Virtual Machine
Interfaces
In order to use a remote object, the client must know its behavior (interface), but does not need to know its implementation (class) In order to provide an object, the server must know both its interface (behavior) and its class (implementation) The interface must be available to both client and server The class should only be on the server
13
In short,
Classes
On the computer where it is defined, instances of this class can be accessed just like any other object On other computers, the remote object can be accessed via object handles
A Serializable class is one whose instances can be marshaled (turned into a linear sequence of bits)
It probably isnt a good idea for an object to be both remote and serializable
14
If an object is to be serialized:
The class must be declared as public The class must implement Serializable The class must have a no-argument constructor All fields of the class must be serializable: either primitive types or serializable objects
15
Must be public Must extend the interface java.rmi.Remote Every method in the interface must declare that it throws java.rmi.RemoteException (other exceptions may also be thrown) Must implement a Remote interface Should extend java.rmi.server.UnicastRemoteObject May have locally accessible methods that are not in its Remote interface
16
You can send messages to a Remote object and get responses back from the object All you need to know about the Remote object is its interface Remote objects dont pose much of a security issue
The receiving object needs to know how the object is implemented; it needs the class as well as the interface There is a way to transmit the class definition Accepting classes does pose a security issue
17
Security
It isnt safe for the client to use somebody elses code on some random server
Your client program should use a more conservative security manager than the default
System.setSecurityManager(new RMISecurityManager());
Most discussions of RMI assume you should do this on both the client and the server
Unless your server also acts as a client, it isnt really necessary on the server
18
The class that defines the server object should extend UnicastRemoteObject
This makes a connection with exactly one other computer If you must extend some other class, you can use exportObject() instead Sun does not provide a MulticastRemoteObject class String url = "rmi://" + host + ":" + port + "/" + objectName;
Naming.rebind(url, object);
Every remotely available method must throw a RemoteException (because connections can fail) Every remotely available method should be synchronized
19
Implementing RMI
Create Remote Interface Create a class that implements the remote interface Create stub and skeleton classes Copy Remote interface and stub to client Create and register remote objcet
Call the remote interface by using the lookup() method of the Naming class.
20
RMI flow - I
Client Virtual Machine
Client
Stub
Skeleton
Server
1. Server Creates Remote object. 2. Server Registers Remote object with RMI registry
21
RMI flow - II
Client Virtual Machine
Client
Stub
3 4 Registry Virtual Machine Remote Object Registered
Skeleton
Server
3. Client requests object from registry 4. Registry returns remote reference through interface
22
Stub
5. Client invokes stub method 6. Stub talks to skeleton 7. Skeleton invokes remote object method.
23
import java.rmi.*; public interface HelloInterface extends Remote { public String say() throws RemoteException; }
24
import java.rmi.*; import java.rmi.server.*; public class Hello extends UnicastRemoteObject implements HelloInterface { private String message; // Strings are serializable public Hello (String msg) throws RemoteException { message = msg; } public String say() throws RemoteException { return message; }
25
class HelloServer { public static void main (String[] argv) { try { Naming.rebind("rmi://localhost/HelloServer", new Hello("Hello, world!")); System.out.println("Hello Server is ready."); } catch (Exception e) { System.out.println("Hello Server failed: " + e); } } }
26
class HelloClient { public static void main (String[] args) { HelloInterface hello; String name = "rmi://localhost/HelloServer"; try { hello = (HelloInterface)Naming.lookup(name); System.out.println(hello.say()); } catch (Exception e) { System.out.println("HelloClient exception: " + e); } } }
27
rmic
The class that implements the remote object should be compiled as usual Then, it should be compiled with rmic:
rmic Hello
This will generate files Hello_Stub.class and Hello_Skel.class These classes do the actual communication
The Stub class must be copied to the client area The Skel was needed in SDK 1.1 but is no longer necessary
28
Trying RMI
If all goes well, you should get the Hello, World! message
29
Summary
1. 2.
The object server registers an object, with a name, with the registry server The client looks up the object in the registry server The request actually goes to the Stub class The Stub classes on client and server talk to each other The clients Stub class returns the result
30
3.
4.
import java.rmi.*; public interface AddServerIntf extends Remote { double add(double d1, double d2) throws RemoteException; }
31
import java.rmi.*; import java.rmi.server.*; public class AddServerImpl extends UnicastRemoteObject implements AddServerIntf { public AddServerImpl() throws RemoteException { } public double add(double d1, double d2) throws RemoteException { return d1 + d2; } }
32
import java.net.*; import java.rmi.*; public class AddServer { public static void main(String args[]) { try { AddServerImpl addServerImpl = new AddServerImpl(); Naming.rebind("AddServer", addServerImpl); } catch(Exception e) { System.out.println("Exception: " + e); } } }
33
import java.rmi.*; public class AddClient { public static void main(String args[]) { try { String addServerURL = "rmi://" + args[0] + "/AddServer"; AddServerIntf addServerIntf = (AddServerIntf)Naming.lookup(addServerURL); System.out.println("The first number is: " + args[1]); double d1 = Double.valueOf(args[1]).doubleValue(); System.out.println("The second number is: " + args[2]); double d2 = Double.valueOf(args[2]).doubleValue(); System.out.println("The sum is: " + addServerIntf.add(d1, d2)); } catch(Exception e) { System.out.println("Exception: " + e); }}}
34
a stub is a Java object that resides on the client machine. Its function is to present the same interfaces as the remote server. A skeleton is a Java object that resides on the server machine. It works with the other parts of the 1.1 RMI system to receive requests, perform deserialization, and invoke the appropriate code on the server.
rmic AddServerImpl
35
Copy AddClient.class, AddServerImpl_Stub.class, and AddServerIntf.class to a directory on the client machine Copy AddServerIntf.class, AddServerImpl.class, AddServerImpl_Skel.class, AddServerImpl_Stub.class, and AddServer.class to a directory on the server machine.
36
start rmiregistry
When this command returns, you should see that a new window has been created.
37
java AddServer
Recall that the AddServer code instantiates AddServerImpl and registers that object with the name AddServer.
38
The AddClient software requires three arguments: the name or IP address of the server machine and the two numbers that are to be summed together. java AddClient 127.0.0.1 8 9
Output:
The first number is: 8 The second number is: 9 The sum is: 17.0
39
The End
40