Presentation is loading. Please wait.

Presentation is loading. Please wait.

Remote Method Invocation (RMI) Mixing RMI and sockets

Similar presentations


Presentation on theme: "Remote Method Invocation (RMI) Mixing RMI and sockets"— Presentation transcript:

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

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

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 User input (UI) Decode UI Create srvr msg Send srvr msg Await srvr reply Receive server message Decode reply Output to user 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

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.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

12 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

13 Steps for RMI, cont. Create the client
Create StoreClient.java and import: java.rmi.Naming; java.rmi.RemoteException; java.net.MalformedURLException; java.rmi.NotBoundException;

14 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).

15 Deploying the Application
Start the rmiregistry rmiregistry & (Unix) start rmiregistry (Windows) Start the StoreServer class java StoreServer & (Unix) Run the client That’s it!

16 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!

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


Download ppt "Remote Method Invocation (RMI) Mixing RMI and sockets"

Similar presentations


Ads by Google