Remote Method Invocation RMI - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Remote Method Invocation RMI

Description:

Simple idea objects existing on one machine (server) may be ... Call java.rmi.Naming.bind(...) to store the register the object with the naming service ... – PowerPoint PPT presentation

Number of Views:472
Avg rating:3.0/5.0
Slides: 19
Provided by: andrews76
Category:

less

Transcript and Presenter's Notes

Title: Remote Method Invocation RMI


1
Lesson 3
  • Remote Method Invocation (RMI)
  • Mixing RMI and sockets
  • Rethinking out tic-tac-toe game

2
Distributed Objects
  • Simple idea objects existing on one machine
    (server) may be accessed from another machine
    through regular method call.
  • Eliminates need to marshal and unmarshal data
    sent over sockets
  • Underlying socket code still exists, but is not
    programmed by user.

3
RMI vs. CORBA
  • RMI is Java framework for creating distributed
    object applications Remote Method Invocation
  • CORBA is alternative technology based on open
    standard Common Object Request Broker
    Architecture
  • RMI is only for pure Java applications CORBA is
    language independent
  • JNI makes this distinction a little less rigid
    since it allows Java to interact with other
    languages

4
Socket flow of events -- synchronous
Client
Server
  • Get user input
  • Decode user input
  • Create server message
  • Send message to server
  • Await server response
  • Receive server message
  • Decode reply
  • Send output to user
  • Await client message
  • Receive client message
  • Decode client message
  • Perform action
  • Create client message
  • Send to client

Method call on standalone object
5
Socket flow - asynchronous
Client1
Server
Client2
  • Wait
  • Receive client msg
  • Decode client msg
  • Perform action
  • Create client message
  • Send to client1
  • Send to client2
  • User input (UI)
  • Decode UI
  • Create srvr msg
  • Send srvr msg
  • Await srvr reply
  • Receive server message
  • Decode reply
  • Output to user
  • User input (UI)
  • Decode UI
  • Create srvr msg
  • Send srvr msg
  • Await srvr reply
  • Receive server message
  • Decode reply
  • Output to user

6
RMI flow of events -- synchronous
Client
Server
  • Instantiate object(s)
  • Bind to registry
  • Get/cache remote obj ref
  • Get user input
  • Decode user input
  • Remote method call
  • Decode return value
  • Send output to user

7
RMI Cartoon1
8
RMI Cartoon2
9
Steps for RMI Application
  • Implement both client and server on single
    machine to test
  • Create two directories
  • client
  • server

10
RMI steps, server side
  • Three files need to be created
  • The implementation class (Foo.java)
  • An interface listing the methods in the
    implementation class which you want to make
    remote (FooInterface.java)
  • A server class, which creates one or more
    implementation objects and posts them to the
    registry (FooServer.java)

11
Creating the interface
  • Interface file (e.g. StoreInterface.java)
  • StoreInterface must extend java.rmi.Remote
  • All methods in interface must throw
    java.rmi.RemoteException
  • Implementation file (e.g Store.java)
  • Store must extend java.rmi.server.UnicastRemoteObj
    ect
  • Store must implement StoreInterface
  • Program implementations. Be sure to throw
    RemoteException in remote methods
  • Explicitly include a null construct that calls
    super()

12
Creating Stubs
  • The stub or proxy code is generated automatically
    by using the rmic utility.
  • Once the interface and implementation classes are
    complete, generate the stubs as follows
  • rmic v1.2 Store
  • This creates Store_Stub.java the java
    networking layer that transparently handles the
    underlying message passing.

13
Creating the server class
  • Server class (e.g. StoreServer.java)
  • Create a new object instance
  • Call java.rmi.Naming.bind() to store the
    register the object with the naming service
  • contains String name associated with bound
    object

14
Steps for RMI, cont.
  • Create the client
  • Change to the client dir and copy Store.class and
    Store_Stub.class (or you can import them but
    remember that these will ultimately be on
    different machines).
  • Create StoreClient.java and import
  • java.rmi.Naming java.rmi.RemoteException
    java.net.MalformedURLException
    java.rmi.NotBoundException

15
Steps for rmi, cont.
  • Call Naming.lookup() to get remote object
    reference (be sure to cast to interface type).
  • Be sure to handle imported exceptions
  • Once you get remote object reference, handle as
    regular object (there are some subtle differences
    that well explore later).

16
Deploying the Application
  • Start the rmiregistry
  • rmiregistry (Unix)
  • start rmiregistry (Windows)
  • Start the StoreServer class
  • java StoreServer (Unix)
  • Run the client
  • Thats it!

17
Additional Issues covered next time
  • Objects which are not remote are copied (slow!)
  • Stub and interface codes can be downloaded by
    client (typical for real distributed systems)
  • Security issues in real system (see ch. 5 in Core
    Java 2)
  • Subtleties with Object methods (clone, etc)
  • Using callbacks with RMI
  • Synchronization
  • Registering multiple objects
  • Bottom Line Dont be too fancy!

18
Examples
  • tic-tac-toe reorganized as standalone back-end
    object
  • single-threaded test of TTT object
  • multithreaded test of TTT object using polling
  • client-server TTT using RMI and polling
  • client-server TTT using RMI over sockets
  • client-server TTT using RMI callbacks.
Write a Comment
User Comments (0)
About PowerShow.com