Title: ClientServer Using RMI
1Client/Server Using RMI
2Remote Method Invocation (RMI)
- Can invoke methods on a remote object
- Integrates a distributed object model
- ORB compliant
- Extends security allowing dynamic downloading of
stub classes - Passes local objects by value (serialization)
- Passes remote objects by reference
3RMI Process (ten steps to success)
- Define an interface -- extends java.rmi.Remote,
all methods throw java.rim.Remote.Exception - Implement the interface -- server class that
extends java.rmi.UnicastRemoteObject - Compile the server class
- Run the stub compiler -- rmic classname
- Generates stub for client and skeleton for server
4RMI Process (ten steps to success)
- Start the RMI registry (non-persistent naming
service) -- rmiregistry - Start the serve objects -- load server classes
and create instances of remote objects - Register your remote objects with the registry --
use java.rmi.Naming class methods
5RMI Process (ten steps to success)
- Write your client code -- use java.rmi.Naming
class to locate remote object - Compile the client code
- Start the client
6RMI Interface
// CountRmi Interface public interface CountRMI
extends java.rmi.Remote int sum() throws
java.rmi.RemoteException void sum(int _val)
throws java.rmi.RemoteException public int
increment() throws java.rmi.RemoteException
7RMI Interface Implementation
// CountRMIImpl.java, CountRMI implementation impo
rt java.rmi. import java.rmi.server.UnicastRemot
eObject public class CountRMIImpl extends
UnicastRemoteObject implements CountRMI
private int sum
8RMI Interface Implementation
public CountRMIImpl(String name) throws
RemoteException super() try
Naming.rebind(name, this) sum 0 catch
(Exception e) System.out.println("Exception
" e.getMessage()) e.printStackTrace()
9RMI Interface Implementation
public int sum() throws RemoteException
return sum public void sum(int val)
throws RemoteException sum val
public int increment() throws RemoteException
sum return sum
10RMI Server
// CountRMIServer.java import java.rmi. import
java.rmi.server. public class CountRMIServer
public static void main(String args)
System.setSecurityManager(new RMISecurityManager()
) try CountRMIImpl myCount new
CountRMIImpl("my CountRMI")
System.out.println("CountRMI Server ready.")
catch (Exception e) System.out.println("Exc
eption " e.getMessage())
e.printStackTrace()
11RMI Client
// CountRMIClient.java RMI Count client import
java.rmi. import java.rmi.registry. import
java.rmi.server. public class CountRMIClient
public static void main(String args) //
Create and install the security manager
System.setSecurityManager(new RMISecurityManager()
)
12RMI Client
try CountRMI myCount (CountRMI)Naming.lookup("
rmi//" args0 "/"
"my CountRMI") System.out.println("Setting Sum
to 0") myCount.sum(0) long startTime
System.currentTimeMillis() System.out.println("
Incrementing") int count new
Integer(args1).intValue() for (int i 0 i
lt count i ) myCount.increment()
13RMI Client
long stopTime System.currentTimeMillis()
System.out.println("Avg Ping "
((stopTime - startTime)/(float)count
) " msecs")
System.out.println("Sum " myCount.sum())
catch(Exception e) System.err.println("Sys
tem Exception" e) System.exit(0)
14RMI Example Output
On the Server Count RMI Server ready. On the
Client Setting sum to 0 Incrementing Avg Ping
14.83 msecs Sum 500
15RMI Hands On
- There are four files in \\Nt431\Public\komar\rmi
that you need to copy to your diskette - On one system find the IP name (Start/Run
WINIPCFG) - Copy the files to a temporary directory on that
server - Use this directory as your home directory for the
steps on the next page
16RMI Hands On
- Compile the ...Server java program which should
compile all the classes needed for the server - Run the RMI compiler using the Impl file
- rmic CountRMIImpl
- Copy the _Stub.class file to the diskette
- Run the RMI registry (start rmiregistry)
- Run the server (java CountRMIServer)
17RMI Hands On
- Take your diskette to a second machine and do
these steps. - Compile the Client.java program
- Run the client using the IP name as the first
parameter and an iteration integer as the second - java CountRMIClient s152.sci1.stthomas.edu 500
18RMI Related classes and interfaces
- RemoteException -- superclass for all RMI
exceptions. All remote methods must throw a
RemoteException - Remote -- interface that flags remote objects
(contains no methods) - RemoteObject -- remote version of the Java root
Object class - RemoteServer -- class to define methods to create
server objects and export them
19RMI Related classes and interfaces
- UnicastRemoteObject -- class implements a remote
server object - object only exists as long as the process that
created it - requires a TCP connection based transport
- client and server use a stream protocol to
communicate - RemoteStub -- class is superclass for all client
stubs
20RMI Related classes and interfaces
- Registry interface -- methods let you update
entries in a registry - LocateRegistry class -- overloaded getRegistry()
static methods to find a registry - Naming class -- retrieve and define remote
objects using URL syntax - rmi//hostport/name (port usually 1099)
- Client uses lookup() method, host uses rebind()
method
21RMI Related classes and interfaces
- RMISecurityManager class -- simple security
manager for RMI objects (Applets have their own
security classes) - RMIClassLoader class -- load stubs and skeletons
via a URL
22RMI and CORBA (Common Object Request Broker
Architecture)
- Moving toward one way in cooperation with OMG
(Object Management Group consortium) - RMI over IIOP (Internet InterORB Protocol) --
will be offered by JavaSoft - RMI/IDL (Interface Definition Language) -- use
Java syntax to specify CORBA interfaces
23RMI Assignment
- Create the Java code needed to do the following
using RMI - Perform addition, subtraction, multiplication, or
division by executing a remote objects method - That remote method will accept three parameters
(character operator, double val1, double val2)
and return the resulting double