Download Remote Method Invocation (RMI)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lesson 3
Remote Method Invocation (RMI)
Mixing RMI and sockets
Rethinking out tic-tac-toe game
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
RMI Cartoon1
RMI Cartoon2
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.
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()
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
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;
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!)
 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!
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.