Presentation on theme: "Remote Method Invocation (RMI) Mixing RMI and sockets"— Presentation transcript:
1Remote Method Invocation (RMI) Mixing RMI and sockets Lesson 3Remote Method Invocation (RMI)Mixing RMI and sockets
2Distributed ObjectsSimple 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 socketsUnderlying socket code still exists, but is not programmed by user.
3RMI vs. CORBARMI is Java framework for creating distributed object applications – Remote Method InvocationCORBA is alternative technology based on open standard – Common Object Request Broker ArchitectureWeb Service/SOAP is a remote object framework based on http and XMLRMI is only for pure Java applications; CORBA is language independentJNI makes this distinction a little less rigid since it allows Java to interact with other languages
4Socket flow of events -- synchronous ClientServerGet user inputDecode user inputCreate server messageSend message to serverAwait server response…Receive server messageDecode replySend output to userAwait client message…Receive client messageDecode client messagePerform actionCreate client messageSend to clientMethod call on standalone object
5Socket flow - asynchronous Client1ServerClient2User input (UI)Decode UICreate srvr msgSend srvr msgAwait srvr reply…Receive server messageDecode replyOutput to userWait…Receive client msgDecode client msgPerform actionCreate client messageSend to client1Send to client2User input (UI)Decode UICreate srvr msgSend srvr msgAwait srvr reply…Receive server messageDecode replyOutput to user
6RMI flow of events -- synchronous ClientServerInstantiate object(s)Bind to registry…Get/cache remote obj refGet user inputDecode user inputRemote method callDecode return valueSend output to user
9Steps for RMI Application Implement both client and server on single machine to testCreate two directoriesclientserver
10RMI 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)
11Creating the interface Interface file (e.g. StoreInterface.java)StoreInterface must extend java.rmi.RemoteAll methods in interface must throw java.rmi.RemoteExceptionImplementation file (e.g Store.java)Store must extend java.rmi.server.UnicastRemoteObjectStore must implement StoreInterfaceProgram implementations. Be sure to throw RemoteException in remote methodsExplicitly include a call to super() as first line in constructor
12Creating the server class Server class (e.g. StoreServer.java)Create a new object instanceCall java.rmi.Naming.bind(…) to store the register the object with the naming service… contains String name associated with bound object
13Steps for RMI, cont. Create the client Create StoreClient.java and import:java.rmi.Naming; java.rmi.RemoteException; java.net.MalformedURLException; java.rmi.NotBoundException;
14Steps for rmi, cont.Call Naming.lookup() to get remote object reference (be sure to cast to interface type).Be sure to handle imported exceptionsOnce you get remote object reference, handle as regular object (there are some subtle differences that we’ll explore later).
15Deploying the Application Start the rmiregistryrmiregistry & (Unix)start rmiregistry (Windows)Start the StoreServer classjava StoreServer & (Unix)Run the clientThat’s it!
16Additional 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 RMISynchronizationRegistering multiple objectsBottom Line: Don’t be too fancy!
17Examples tic-tac-toe reorganized as standalone back-end object single-threaded test of TTT objectmultithreaded test of TTT object using pollingclient-server TTT using RMI and pollingclient-server TTT using RMI over socketsclient-server TTT using RMI callbacks.