Lesson 3: Remote Method Invocation (RMI) Mixing RMI and Sockets

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 17

Lesson 3

Remote Method Invocation (RMI)


Mixing RMI and sockets
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.
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

 Web Service/SOAP is a remote object framework based on http


and XML

 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
Socket flow of events -- synchronous
Client Server
•Get user input •Await client message
•Decode user input •…
•Create server message •…
•Send message to server •Receive client message
•Await server response •Decode client message
•… •Perform action
•… •Create client message
•… •Send to client
•Receive server message
•Decode reply Method call on standalone object
•Send output to user
Socket flow - asynchronous
Client1 Server Client2
•User input (UI) •Wait •User input (UI)
•Decode UI •… •Decode UI
•Create srvr msg •… •Create srvr msg
•Send srvr msg •Receive client msg •Send srvr msg
•Await srvr reply •Decode client msg •Await srvr reply
•… •Perform action •…
•… •Create client message •…
•… •Send to client1 •…
•Receive server •Send to client2 •Receive server
message message
•Decode reply •Decode reply
•Output to user •Output to user
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
RMI Cartoon1
RMI Cartoon2
Steps for RMI Application
 Implement both client and server on single
machine to test

 Create two directories


– client
– server
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)
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.UnicastRemoteObject
– Store must implement StoreInterface
– Program implementations. Be sure to throw
RemoteException in remote methods
– Explicitly include a call to super() as first line in
constructor
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
Steps for RMI, cont.
 Create the client
– Create StoreClient.java and import:
java.rmi.Naming;
java.rmi.RemoteException;
java.net.MalformedURLException;
java.rmi.NotBoundException;
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 we’ll explore later).
Deploying the Application
 Start the rmiregistry
– rmiregistry & (Unix)
– start rmiregistry (Windows)
 Start the StoreServer class
– java StoreServer & (Unix)
 Run the client
 That’s it!
Additional Issues – covered next
time
 Objects which are not remote are copied (slow!)
 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: Don’t be too fancy!
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.

You might also like