Java Remote Method Invocation RMI
Idea If objects communicate with each other on one JVM why not do the same on several JVM’s? If objects communicate with each other on one JVM why not do the same on several JVM’s? Build on top of the Socket layer Build on top of the Socket layer Break down objects into a string of bytes and send these over the wire Break down objects into a string of bytes and send these over the wire Use Client/Server architecture Use Client/Server architecture
Client/Server Architecture Client requests data from server Client requests data from server Server sends the data to the client (response) Server sends the data to the client (response) Data is exchanged in standard format Data is exchanged in standard format RMI: Remote calls will go in both directions RMI: Remote calls will go in both directions Client Object: is the object one of whose methods issue a remote call Client Object: is the object one of whose methods issue a remote call Remote Object is the object on the server whose method is called Remote Object is the object on the server whose method is called
We need a Proxy Object The client calls a method on a special type of interface that is implemented by the remote object The client calls a method on a special type of interface that is implemented by the remote object Call this interface a stub Call this interface a stub We still have to deal with the parameters and return values, i.e. convert them into an appropriate format We still have to deal with the parameters and return values, i.e. convert them into an appropriate format Call this conversion marshalling Call this conversion marshalling
Marshalling Objects need to be disassembled to this format Objects need to be disassembled to this format Formatted object is sent over the wire Formatted object is sent over the wire Object needs to be reassembled on the other side Object needs to be reassembled on the other side Hence use character format: if the parameter of return value is a primitive type just copy it (as a sequence of bytes) Hence use character format: if the parameter of return value is a primitive type just copy it (as a sequence of bytes) If it is an object then disassemble to a string If it is an object then disassemble to a string Call this last process serialization Call this last process serialization
What is a Stub? It defines a remote interface It defines a remote interface Contains methods that will be eventually called remotely Contains methods that will be eventually called remotely It extends java.rmi.Remote It extends java.rmi.Remote All its methods must throw a java.rmi.RemoteException All its methods must throw a java.rmi.RemoteException
The Stub Produces An identifier of the remote object An identifier of the remote object A description of the method invoked A description of the method invoked Marshalled parameters Marshalled parameters
Server Side There is another interface for the call (now local) that has to unmarshall the parameters and make the actual call with these There is another interface for the call (now local) that has to unmarshall the parameters and make the actual call with these Called a skeleton (not needed by programmer) Called a skeleton (not needed by programmer) Possible return value has to be marshalled and sent back to the client Possible return value has to be marshalled and sent back to the client Note the dynamic nature of marshalled objects must be accomodated Note the dynamic nature of marshalled objects must be accomodated Skeleton resides on the server Skeleton resides on the server
Stubs must be downloaded! From server to client, since it is the remote object’s methods that must be called From server to client, since it is the remote object’s methods that must be called If the client is to know about the stub, then the latter must be registered somewhere If the client is to know about the stub, then the latter must be registered somewhere The client must look the stub up The client must look the stub up The server enters the stub into the registry with Naming.rebind() The server enters the stub into the registry with Naming.rebind() The client looks it up with Naming.lookup() The client looks it up with Naming.lookup()
Naming Lookup Stubs are normally downloaded when the client calls a remote method Stubs are normally downloaded when the client calls a remote method What do you do when there is no remote interface to facilitate downloading? What do you do when there is no remote interface to facilitate downloading? Use the bootstrap registry service the first time round Use the bootstrap registry service the first time round This associates a name (string) to an object on the server. This associates a name (string) to an object on the server.
Naming Lookup (ctd) The server Naming.rebind() accepts a (unique) name--a normal string, say xxx The server Naming.rebind() accepts a (unique) name--a normal string, say xxx The client accesses it by means of a so- called RMI URL, e.g. rmi://localhost:90/xxx The client accesses it by means of a so- called RMI URL, e.g. rmi://localhost:90/xxx This is the argument to Naming.lookup() to obtain the remote reference. This is the argument to Naming.lookup() to obtain the remote reference.
Defining the Remote Interface It extends java.rmi.Remote It extends java.rmi.Remote Must be public Must be public Contains declarations of methods to be called remotely Contains declarations of methods to be called remotely Is the way to pass remote objects Is the way to pass remote objects All methods must declare java.rmi.RemoteException in their throws clause All methods must declare java.rmi.RemoteException in their throws clause
How to Generate Stubs Stubs and skeletons are generated on the server from class files Stubs and skeletons are generated on the server from class files Use the RMI compiler rmic Use the RMI compiler rmic Format: Format: rmic rmic Generates both in same directory as the class file Generates both in same directory as the class file
Naming Conventions Assume the remote interface is called xxx Assume the remote interface is called xxx Then the stub is called xxx_Stub Then the stub is called xxx_Stub The skeleton is called xxx_Skel The skeleton is called xxx_Skel The original class file is called xxxImpl The original class file is called xxxImpl The client’s class is called xxxClient The client’s class is called xxxClient
Write the Implementation Implement the remote interface Implement the remote interface Extend java.rmi.server.UnicastRemoteObject Extend java.rmi.server.UnicastRemoteObject Implement the methods to be called remotely Implement the methods to be called remotely Other medthods may be available only locally Other medthods may be available only locally Constructor must throw a remote exception Constructor must throw a remote exception
UnicastRemoteObject Objects of this type are accessible by the TCP/IP protocol Objects of this type are accessible by the TCP/IP protocol It automatically connects to the RMI runtime system It automatically connects to the RMI runtime system Can perform equality checks with other remote objects Can perform equality checks with other remote objects You can also construct objects of this type with a specific port and,optionally, a SocketFactory on both server and client. You can also construct objects of this type with a specific port and,optionally, a SocketFactory on both server and client.
Set Up the Server Write a java main program Write a java main program Instantiate classes to become remote objects Instantiate classes to become remote objects Put in a Security Manager Put in a Security Manager Bind the remote objects to a name in the registry (more later) Bind the remote objects to a name in the registry (more later)
Set Up a Client Obtain references to remote objects from Naming.lookup() Obtain references to remote objects from Naming.lookup() Call methods on those remote objects Call methods on those remote objects Should set up a Security Manager Should set up a Security Manager
How to Proceed 1. Write the remote interface. This is shared between client and server 2. Write the remote object implementation 3. Write the client application that invokes methods of Compile all programs: javac xxx*.java -d classes javac xxx*.java -d classes 5.Write server program that creates the remote objects
Running Your Program 1. Start the RMI registry: start rmiregistry start rmiregistry 2. Locate classes to make stubs: run rmic 3. Start the server: java -Djava.security.policy = server.policy xxxServer java -Djava.security.policy = server.policy xxxServer 4. Run the client: java -Djava.security.policy = client.policy xxxClient java -Djava.security.policy = client.policy xxxClient