Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
11/11/10 Remote Method Invocation Remote Method Invocation Internet Computing Workshop Lecture 13 So far we have look at sending messages across a network. Remote Method Invocation (RMI) gives you access to objects that are implemented at a remote location. It's distributed programming in a fully objectoriented Java style. Advantages of RMI You can program in a purely object-orientated style. Disadvantages of RMI Needs additional configurations: Object registry Complied with rmic Once it's set up you don't need to worry about handling remote access. No need to design a protocol for your system. Remote Objects In RMI the client has access to an object that is implemented remotely. Fields of the object are stored remotely Remote data storage. Hard to tell which objects are local and which are remote. Communication Model With sockets: client and server communicate directly, exchanging messages. Server Messages Methods of the object are executed remotely Remote processing. Client 1 11/11/10 Communication Model The client asks the registry for the object. Bind Object X Server X This connects the object X to a stub. Registry X? Communication Model The client use the stub as if it was the object. Stub for X Stub Client Give me X The client treats the stub like the real object. All methods called on the stub are forward to the server. You must start the a registry running. Interact with registry using the java.rmi.Naming class - Adds the object “obj” to the registry under the name “name”. lookup (String name) - Returns a stub for the object called “name” unbind(String name) - Removes the object “name” from the registry list(String name) - Returns a list of all objects in the registry rebind (String name, Remote obj) - Method call Stub Client Both the client and the server must have access to an interface for the remote object. This interface must extend the java.rmi.Remote class. The methods may throw java.rmi.RemoteException java.rmi.Naming bind(String name, Remote obj) Registry Interface for the Object e.g. type “rmiregistry” from the command line Usually runs on port 1099, but can run on any port and even on the same machine as the server. Result The result is then returned to the client. The Registry Server X Registry URLs Services in registries can be addressed directly using a URL of the form: rmi://<host_name>[:<name_service_port>]/<service_name> rmi://localhost/printObject or rmi://68.45.12.7:30099/ process These are used with the Naming.lookup method: Naming.lookup(rmi://68.45.12.7:30099/process) which returns the remote object called “process” = unbind(name);bind(name,obj) 2 11/11/10 java.rmi.registry.LocateRegistry Communication Model Binds You can also refer to a Registry my creating a Registry object using the LocateRegistry package. Calc Server LocateRegistry.getRegistry(h,p) return an reference to the registry runing on host h and on port p Registry registry = LocateRegistry.getRegistry(host); Calc Inpl Generate using rmic Registry Implements Calc Interface Ro o = (Ro)registry.lookup("service"); Is the same as: Ro o = (Ro)Naming.lookup("rmi://host/serv"); Client Gets Object Distributing the Code Both the client and the server need the interface class. Server binds object Client does need the stub, but can fetch this dynamically, using the java.rmi.server.codebase Client gets object To keep things easy you should sending primitive data types as arguments and return values. You can send Objects that are “serializable” Make your own object serializable by extending the java.io.serializable class. The remote object is a new object: a copy of the one you sent. Stub forwards Calls to remote stub Server Calc Server Calc Impl Server places stub on public host Stub and impl communicate Public Host Client Calc Client Calc Interface Calc Interface Calc stub Calc stub CORBA vs RMI Method Arguments and RMI Client uses Stub Client does not need the implementation java Calc -Djava.rmi.server.codebase=URLofStub Calc stub Communication Model Registry Calc Client Common Object Request Broker Architecture (CORBA) is like RMI but not just in Java. Only primitive data types can be passed in CORBA. CORBA is very useful when new Java code is combined with legacy systems. 3 11/11/10 CORBA vs RMI CORBA uses a “Object Request Broker” (ORB), rather than a rmi registory. CORBA vs RMI Interfaces in CORBA are written in Interface Definition Language (IDL). You then generate the Java Interface using “ildj”. Use CORBA for inter-language working. Or for legacy Systems Otherwise use RMI. For More Information Summary RMI gives you access to objects that are implemented at a remote location. Use the “org.omg.CORBA.*” to distribution and access the object. “ildj” generates all the files you need. For more on RMI see: http://java.sun.com/docs/books/tutorial/rmi/ It's distributed programming in a fully objectoriented Java style. It needs addition configurations (registry,rmic) For more on CORBA see: http://java.sun.com/developer/onlineTraining/corba Hard to tell which objects are local and which remote. Next Week: AJAX: Asynchronous JavaScript and XML Makes really fast, responsive webpages. E.g. gmail 4