Professional Documents
Culture Documents
Chapter 7
• Consider the following program organization:
method call
SomeClass AnotherClass
returned object
computer 1 computer 2
My Machine
My Object
Today’s World...
Network and Distributed Objects
My Machine Remote
Machine
Local
Remote
Objects
Objects
13
The Stub and Skeleton
call
skeleton
Stub
RMI Client RMI Server
return
• C:>/rmic TestImpl
• C:>/start rmiregistry
Step 6: Create the Remote Server &
Start the server
– Create the object of remote class.
– static method bind/rebind (class Naming)
• Binds object to rmiregistry
• Object named to be used by client
• rebind replaces any previous objects with
same name
– Method bind does not
Step 6: Create the Remote Server &
Start the server
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.NotBoundException;
Lookup remote object in
import java.rmi.RemoteException; registry. Returns
import java.rmi.registry.LocateRegistry; Remote reference, cast
to proper type.
import java.rmi.registry.Registry;
public class RemoteClient1 {
public static void main(String[] args) throws RemoteException,
NotBoundException {
Registry registry=LocateRegistry.getRegistry("localhost", Const.RMI_PORT);
TestInterface remote=(TestInterface)registry.lookup(Const.RMI_ID);
System.out.println(remote.isLoginValid("test"));
System.out.println(remote.isLoginValid(“dick"));
}
} Call like regular
method.
For running this RMI example,
1) compile all the java files
javac *.java
2)create stub object by rmic tool
rmic TestImpl
3)start rmi registry in one command prompt
start rmiregistry
4)start the server in another command prompt
java RMIServer1
5)start the client application in another command prompt
java RemoteClient1
Processes
– For RMI, you need to be running three processes
• The Client
• The Server
• The Object Registry, rmiregistry, which is like a DNS
service for objects
– You also need TCP/IP active
31
The General RMI Architecture
Remote Machine
– The server must first bind its
bind
name to the registry RMI Server
Registry
– The client lookup the server skeleton
32
What is needed for RMI
– To send a message to a remote “server object,”
• The “client object” has to find the object
– Do this by looking it up in a registry
• The client object then has to marshal the parameters (prepare
them for transmission)
– The server object has to unmarshal its parameters, do its
computation, and marshal its response
• The client object has to unmarshal the response
– Much of this is done for you by special software
33
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
34