ClientServer Using RMI - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

ClientServer Using RMI

Description:

Compile the server class. Run the stub compiler -- rmic classname ... Compile the client code. Start the client. 9/5 ... Compile the '...Client.java' program ... – PowerPoint PPT presentation

Number of Views:86
Avg rating:3.0/5.0
Slides: 24
Provided by: joseph120
Category:

less

Transcript and Presenter's Notes

Title: ClientServer Using RMI


1
Client/Server Using RMI
  • Joe Komar

2
Remote 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

3
RMI 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

4
RMI 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

5
RMI 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

6
RMI 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
7
RMI 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
8
RMI 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()

9
RMI 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
10
RMI 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()
11
RMI 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()
)
12
RMI 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()
13
RMI 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)

14
RMI Example Output
On the Server Count RMI Server ready. On the
Client Setting sum to 0 Incrementing Avg Ping
14.83 msecs Sum 500
15
RMI 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

16
RMI 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)

17
RMI 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

18
RMI 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

19
RMI 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

20
RMI 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

21
RMI 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

22
RMI 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

23
RMI 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
Write a Comment
User Comments (0)
About PowerShow.com