Lesson 3 Remote Method Invocation RMI Mixing RMI

  • Slides: 17
Download presentation
Lesson 3 Remote Method Invocation (RMI) Mixing RMI and sockets

Lesson 3 Remote Method Invocation (RMI) Mixing RMI and sockets

Distributed Objects ¨ Simple idea – objects existing on one machine (server) may be

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 –

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 • Get user input • Decode user

Socket flow of events -- synchronous Client • 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 Server • Await client message • … • Receive client message • Decode client message • Perform action • Create client message • Send to client Method call on standalone object

Socket flow - asynchronous Client 1 • User input (UI) • Decode UI •

Socket flow - asynchronous Client 1 • User input (UI) • Decode UI • Create srvr msg • Send srvr msg • Await srvr reply • … • … • Receive server message • Decode reply • Output to user Server • Wait • … • Receive client msg • Decode client msg • Perform action • Create client message • Send to client 1 • Send to client 2 Client 2 • User input (UI) • Decode UI • Create srvr msg • Send srvr msg • Await srvr reply • … • … • Receive server message • Decode reply • Output to user

RMI flow of events -- synchronous Client Server • Instantiate object(s) • Bind to

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 Cartoon 1

RMI Cartoon 1

RMI Cartoon 2

RMI Cartoon 2

Steps for RMI Application ¨ Implement both client and server on single machine to

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

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 (Foo. Interface. java) – A server class, which creates one or more implementation objects and posts them to the registry (Foo. Server. java)

Creating the interface ¨ Interface file (e. g. Store. Interface. java) – Store. Interface

Creating the interface ¨ Interface file (e. g. Store. Interface. java) – Store. Interface must extend java. rmi. Remote – All methods in interface must throw java. rmi. Remote. Exception ¨ Implementation file (e. g Store. java) – Store must extend java. rmi. server. Unicast. Remote. Object – Store must implement Store. Interface – Program implementations. Be sure to throw Remote. Exception in remote methods – Explicitly include a call to super() as first line in constructor

Creating the server class ¨ Server class (e. g. Store. Server. java) – Create

Creating the server class ¨ Server class (e. g. Store. Server. 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 Store. Client. java and

Steps for RMI, cont. ¨ Create the client – Create Store. Client. java and import: java. rmi. Naming; java. rmi. Remote. Exception; java. net. Malformed. URLException; java. rmi. Not. Bound. Exception;

Steps for rmi, cont. – Call Naming. lookup() to get remote object reference (be

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

Deploying the Application ¨ Start the rmiregistry – rmiregistry & (Unix) – start rmiregistry (Windows) ¨ Start the Store. Server class – java Store. Server & (Unix) ¨ Run the client ¨ That’s it!

Additional Issues – covered next time ¨ Objects which are not remote are copied

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

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.