Download presentation
Presentation is loading. Please wait.
Published byGarry Harmon Modified over 6 years ago
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 Eliminates need to “marshal” and “unmarshal” data sent over sockets Underneath same process, but is hidden from user CORBA is alternative technology RMI is only Java – to –Java, CORBA is language independent (as long as bindings have been defined) JNI makes this distinction a little less rigid
3
RMI Cartoon1
4
RMI Cartoon2
5
Steps for RMI Application
Implement on single machine first (as we do) Create two directories client server Three files need to be built: The implementation class An interface listing the methods in the implementation class which you want to make remote A server class, which creates one or more implementation objects and posts them to the registry.
6
Creating the interface
Interface file (e.g. Store.java) Extend java.rmi.Remote Publish your public methods and be sure they all throw java.rmi.RemoteException Implementation file (e.g StoreImpl.java) Import java.rmi.server.UnicastRemoteObject Implement Store Extend UnicastRemoteObject Program implementations Explicitly add a null construct that calls super()
7
Steps for RMI, cont. Run rmic –v1.2 on the StoreImpl class
This produces StoreImpl_Stub.class Server class (e.g. StoreServer.java) Import java.rmi.Naming; Create a new object instance Call Naming.bind(…) to store the register the object with the naming service
8
Steps for RMI, cont. Create the client
Change to the client dir and copy Store.classand StoreImpl_Stub.class (or you can import them but remember that these will ultimated by on different machines). Create StoreClient.java and import: java.rmi.Naming; java.rmi.RemoteException; java.net.MalformedURLException; java.rmi.NotBoundException;
9
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).
10
Deploying the Application
Start the rmiregistry rmiregistry & (Unix) start rmiregistry (Windows) Start the StoreServer class java StoreServer & (Unix) Run the client That’s it!
11
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: Don’t be too fancy!
12
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.