CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina
4/6/20042 Remote Method Invocation Provide a framework for Java objects to communicate via their methods, regardless of their location Networking details disappear under the guise of standard method calls Define a remote interface that declares methods that can be called remotely Use stub and skeleton classes to translate parameters and return values Register remote object with a naming service Client connects to naming service to get a remote reference
4/6/20043 Stub and Skeleton Classes Generated by rmic tool in JDK Stub class automatically translates remote method calls into network communication setup and parameter passing Skeleton class accepts network connections and translate them into actual method calls on actual object
4/6/20044 RMI Architecture Remote method() Stub JVM 1 RemoteObject method() Skeleton JVM 2 ClientServer Internet
4/6/20045 Object Transmission To transmit an object over network, need to make the object serializable Three stages of object transmission Marshaling: pack up data into a form that can be sent over some communication channel Delivery: over the network Unmarshaling: reconstruct object into original form at the other end object bytes object Internet MarshalingUnmarshaling Delivery
4/6/20046 Object Transmission in RMI Marchaling performed by a customized ObjectOutputStream Delivery performed over TCP/IP socket connection Unmarshaling performed by a customized ObjectInputStream Autogenerated stub and skeleton classes contain the code so is transparent to programmers
4/6/20047 RemoteException Superclass of all exceptions that can occur in RMI run time Thrown whenever a remote method invocation fails All method in a remote interface can throw RemoteException
4/6/20048 Steps to Create RMI Application Define remote interface Implement remote interface Generate stub and skeleton classes using rmic Write a client that locates server in naming registry and then calls remote methods Start naming registry using rmiregistry Start server Run client
4/6/20049 An RMI Date Server Example A simple date server that allows clients to determine date and time at server using remote method calls Define remote interface DateServer Implement remote interface with class DateServerImpl Client class DateClient
4/6/ Interface DateServer /* Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN X * * * * Copyright (c) Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */ import java.rmi.Remote; import java.rmi.RemoteException; import java.util.Date; public interface DateServer extends Remote { public Date getDate () throws RemoteException; }
4/6/ Class DateServerImpl /* Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN X * * * * Copyright (c) Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */ import java.rmi.*; import java.rmi.server.*; import java.util.Date; public class DateServerImpl extends UnicastRemoteObject implements DateServer { public DateServerImpl () throws RemoteException { } public Date getDate () { return new Date (); } public static void main (String[] args) throws Exception { DateServerImpl dateServer = new DateServerImpl (); Naming.bind ("DateServer", dateServer); }
4/6/ Class DateClient /* Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN X * * * * Copyright (c) Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */ import java.rmi.Naming; import java.util.Date; public class DateClient { public static void main (String[] args) throws Exception { if (args.length != 1) throw new IllegalArgumentException ("Syntax: DateClient "); DateServer dateServer = (DateServer) Naming.lookup ("rmi://" + args[0] + "/DateServer"); Date when = dateServer.getDate (); System.out.println (when); }
4/6/ RMI-Related Packages Five main packages in RMI framework java.rmi: classes related to client side of RMI java.rmi.server: classes related to server side of RMI java.rmi.registry: classes related to RMI naming registry java.rmi.dgc: classes supporting distributed garbage collection java.rmi.activation: classes supporting JDK 1.2 activation mechanism
4/6/ Interface Remote Superinterface for all remote interfaces Serve to identify all remote interfaces, so declare no methods A remote interface that extends Remote describes the only methods that a remote object supports Clearly delineate API of a remote object, so separate remote object’s implementation from its publicly-exposed interface
4/6/ Remote Method Call Semantics Major difference between remote method call and direct method call is parameters are passed by value in RMI Remote method gets copy of field values of Object, so client will not see changes reflected in its local copy of Object To modify an Object inside a remote method, need to have remote method return modified Object as result of remote method call
4/6/ Class Naming Methods Remote lookup(String address) throws MalformedURLException, RemoteException, NotBoundException void bind(String address, Remote object) throws MalformedURLException, RemoteException, AlreadyBoundException void rebind(String address, Remote object) throws MalformedURLException, RemoteException void unbind(String address) throws MalformedURLException, RemoteException, NotBoundException String[] list(String address) throws MalformedURLException, RemoteException
4/6/ Class Naming Exceptions MalformedURLException RemoteException UnknownHostException NotBoundException AlreadyBoundException
4/6/ Examples of Using Naming String[] services = Naming.list (“//accounts.my.bank/”); for (int i = 0; i < services.length; ++i) System.out.println (services[i]); BankAccount firstAccount = (BankAccount) Naming.lookup (services[0]); Naming.bind (“First Account”, firstAccount); account = new PersonalBankAccountImpl (“jim”); Naming.rebind (“personal/jim”, account);
4/6/ Class LocateRegistry Static methods Registry getRegistry() throws RemoteException Registry getRegistry(int port) throws RemoteException Registry getRegistry(String host) throws RemoteException Registry getRegistry(String host, int port) throws RemoteException Registry getRegistry(String host, int port, RMIClientSocketFactory clients) throws RemoteException Registry createRegistry(int port) throws RemoteException Registry createRegistry(int port, RMIServerSocketFactory servers, RMIClientSocketFactory clients) throws RemoteException
4/6/ Class LocateRegistry Exceptions RemoteException UnknownHostException
4/6/ Interface Registry Static variable int REGISTRY_PORT Methods Remote lookup(String name) throws RemoteException, NotBoundException void bind(String name, Remote object) throws RemoteException, AlreadyBoundException void rebind(String name, Remote object) throws RemoteException void unbind(String name) throws RemoteException, NotBoundException String[] list() throws RemoteException
4/6/ Interface Registry Exceptions RemoteException NotBoundException AlreadyBoundException AccessException
4/6/ Examples of Using Registry Registry registry = LocateRegistry.getRegistry (“host”, 1234); registry.rebind (“Service”, service); Registry registry = LocateRegistry.createRegistry (1234); registry.bind (“Service”, service);
4/6/ Next Class Remote method invocation (RMI) Examples of RMI in practice Read JNP Ch. 23, 24