520 likes | 746 Views
Java Remote Method Invocation (RMI). ). Distributed Systems. a collection of independent computers that appears to its users as a single coherent system. Models of Distribution. Message passing. Distributed objects. Event-based architectures Space-based paradigms.
E N D
DistributedSystems a collection of independent computers thatappearstoitsusersasasinglecoherentsystem
ModelsofDistribution Message passing Distributed objects Event-based architecturesSpace-based paradigms
DistributedObjectModel Views a distributed system as a series ofinteractingobjects Based on some underlying messagepassingprotocolinvisibletotheprogrammer Three main technologies: RMI, CORBAandDCOM
DistributedObjectComputing Enable any object in the local system to directly interact with an object on a remote host Goals: Let any object reside anywhere in the network, and allow an application to interact with these objects in the same way as they do with a local object. Provide the ability to construct an object on one host and transmit it to another host. Enable an agent on one host to create an object on another host.
WhatIsRMI? A mechanism that allows the invocation ofamethodthatexistsinanotheraddressspace Java-to-Java only Client-Server ProtocolHigh-level API TransparentLightweight
RelatedTechnologies RPC (“Remote Procedure Calls”) Developed by Sun Platform-specific CORBA (“Common Object Request Broker Architecture”) Developed by OMG Access to non-Java objects (as well as Java) DCOM (“Distributed Common Object Model”) Developed by Microsoft Access to Win32 objects LDAP (“Lightweight Directory Access Protocol”) Finding resources on a network
RMI Client –the process that invokes a method on aremoteobject Server –the process that owns the remoteobject Object Registry –a name server that relatesobjectswithnames Objects are registered with the Object Registry, undera unique name. The Object Registry is used to obtain access toremote objects, using their names.
RemoteObjects(Diagram) Java Virtual Machine Java Virtual Machine ClientObject Remote Object TCP Client Server
RMILayers Java Virtual Machine Client Java Virtual Machine Remote Object Object Stub Skeleton Remote Reference Layer Remote Reference Layer TCP Transport Layer Transport Layer 11/18/2003
RMIArchitectureintheOSImodel User Application Application Layer Presentation Layer Stub Skeleton Remote Reference Layer Session Layer TCP Transport Layer IP Network Layer Data-link layerPhysical Layer Hardware Interface Network
Java Virtual Machine Java Virtual Machine ClientObject RemoteObject RemoteObjects Stub Skeleton Remote Reference Layer Remote Reference Layer TCP Transport Layer Transport Layer Server Client Remote Objects Live on server Accessed as if they were local
Java Virtual Machine Java Virtual Machine ClientObject RemoteObject StubsandSkeletons Stub Skeleton Remote Reference Layer Remote Reference Layer TCP Transport Layer Transport Layer Server Client Stub lives on client pretends to be remote object -a proxy for the remote object Skeleton lives on server receives requests from stub, talks to the remote object and delivers response to stub Stubs and skeletons are not written by the programmer! They are generated by a special compiler “rmic”
Java Virtual Machine Java Virtual Machine ClientObject RemoteObject StubsandSkeletons Stub Skeleton Remote Reference Layer Remote Reference Layer TCP Transport Layer Transport Layer Server Client Stub –responsibilities Initiate remote calls Marshals arguments to be sent Informs the remote reference layer that a callshouldbeinvokedontheserver Unmarshals a return value (or exception) Informs the remote reference layer that thecalliscomplete
Java Virtual Machine Java Virtual Machine ClientObject RemoteObject StubsandSkeletons Stub Skeleton Remote Reference Layer Remote Reference Layer TCP Transport Layer Transport Layer Server Client Skeleton –responsibilities Unmarshals incoming arguments Calls the actual remote object implementationMarshals return values for transport to theclient Marshaling –definition The process of converting native programminglanguagedatatypestoaformatsuitablefortransmissionacrossanetwork
RemoteInterfacesandStubs Remote Interface implements implements Remote Object Client Stub Skeleton (Server) 11/18/2003
RemoteInterfaces Declare exposed methods –the methodsthatcanbecalledfromremotelocations Extend java.rmi.Remote The remote object implements thisinterface Act like a proxy for the remote object
Java Virtual Machine Java Virtual Machine ClientObject RemoteObject RemoteReferenceLayer Stub Skeleton Remote Reference Layer Remote Reference Layer TCP Transport Layer Transport Layer Server Client Sets up connections to remote addressspaces Manages connections Listens for incoming callsCommunicates via TCP/IP
ObjectRegistries Name and look up remote objects Remote objects register by name Clients obtain a remote reference to theremoteobject A registry is a running processon thesamehostastheRMIserver
HTTPTunneling Cool: if it can’t make the connection normally,itwilltunnelthroughport80 Allows clients behind firewall to makeremotecallstoserver Note: does not work server -> client
RMISystemArchitecture Client Virtual Machine Client Server Virtual Machine Remote Object Skeleton Server Stub Server Client “Fred” Registry Registry Virtual Machine
RMIFlow 1. Server Creates Remote Object Server Virtual Machine Remote 2. Server Registers Remote Object Client Object 1 Skeleton Server Stub 2 “Fred” Registry Virtual Machine
RMIFlow Client Virtual Machine Client Server Virtual Machine 3. Client requests object from Registry 4. Registry returns remote reference Skeleton Server Stub 3 4 “Fred” Registry Virtual Machine
RMIFlow Client Virtual Machine Client Server Virtual Machine Remote Object 5 7 6 Skeleton Server Stub 5. Client invokes stub method 6. Stub talks to skeleton 7. Skeleton invokes remote object method Registry Virtual Machine
PartII:RMIUsage 11/18/2003 and Distributed Programming
CreatingRemoteObjects Define a Remote Interface extends java.rmi.Remote Define a class that implements theRemoteInterface extends java.rmi.RemoteObjector java.rmi.UnicastRemoteObject
RemoteInterfaceExample importjava.rmi.*; publicinterfaceAdder extendsRemote { publicintadd(intx,inty) throws RemoteException;} ECE 451:Introduction to Parallel 11/18/2003
RemoteClassExample import java.rmi.*; import java.rmi.server.*; public class AdderImpl extends UnicastRemoteObject implements Adder { public AdderImpl() throws RemoteException{ } public int add(int x, int y) throws RemoteException{ return x + y; } }
InheritanceDiagraminJava Object Remote RemoteObject RemoteStub RemoteServer Unicast RemoteObject
CompilingRemoteClasses Compile the Java class javac reads .java file produces .class file Compile the Stub and Skeleton rmic reads .class file produces _Skel.class and _Stub.class
CompilingRemoteClasses (Diagram) javac Adder.java(interface) AdderImpl_Skel.class(skeleton classfile) Adder.class (interface classfile) javac AdderImpl.java(remote class) AdderImpl.class(classfile) rmic AdderImpl_Stub.class(stub classfile) ECE 451:Introduction to Parallel 11/18/2003 and Distributed Programming
RegisteringRemoteClasses start the registry running processUnix: rmiregistry& Windows: start/mrmiregistry
RegistryCLASSPATH Registry VM needs to be able to find stub file(s)You must set the CLASSPATH to include thedirectorycontainingthestubfile An easy way to check CLASSPATH is to use the javapcommand, supplying a fully package qualified class name. It uses the current CLASSPATH to find and print the interface to a class. Or, your server needs to specify the java.rmi.server.codebaseSystemproperty(morelater)
Createtheserver Creates a new instance of the remoteobject Registers it in the registry with a nameThat’s it ECE 451:Introduction to Parallel 11/18/2003 and Distributed Programming
RMIServerExample try { AdderImpl adder = newAdderImpl(); Naming. rebind ("adder", adder); System.out.println("Adder bound");} catch (RemoteException re) { re.printStackTrace(); } catch (MalformedURLException me) { me.printStackTrace(); }
LaunchtheServer %javaAdderServer&Adderbound ECE 451:Introduction to Parallel 11/18/2003 and Distributed Programming
ServerLogging invoke from command linejava -Djava.rmi.server.logCalls=trueYourServerImpl or enable inside program RemoteServer.setLog(System.err);
CreatinganRMIClient Install a Security Manager to protect from malicious stubsFind a registry use java.rmi.Naming Lookup the name, returns a referenceCast the reference to the appropriateRemoteInterface Just use it!
RMIURLs rmi://host[:port]/namedefault port is 1099 Specifies hostname of registrycan also use relative URLs name only assumes registry is on local host
RMIClientExample System.setSecurityManager(newRMISecurityManager());Addera=(Adder) Naming.lookup("adder"); intsum=a.add(2,2); System.out.println("2+2="+sum);
RemoteInterfacesvs.Remote Classes Remember that the reference is to an interfaceYou must make references, arrays, etc. out ofthe interfacetype,nottheimplementationtype You can’t cast the remote reference to a normalreference So name your Remote Objects with “Impl” (soyoudon’tgetconfused)
ParameterPassing All parameters are passed by value Primitive types passed by valueObjects passed by value use Java Object Serialization
ObjectSerialization saves the state (data) of a particularinstanceofanobject serialize-to save unserialize -to load
JavaSerialization writes object as a sequence of byteswrites it to a Stream recreates it on the other end creates a brand new object with the olddata
java.io.Serializable Objects that implement the java.io.Serializableinterfacearemarkedasserializable Also subclasses empty interface -just a marker –no needtoimplementanyspecialmethods
RMISecurity Server is untrusted Stubs could be malicious rmic is OK, but someone could custom-codeanevilstub:it’sjusta.classfile
RMISecurityManagers AppletSecurityManager stub can only do what an applet can doRMISecurityManager disables all functions except class definition andaccess A downloaded class is allowed to make a connectionif the connection was initiated via the RMItransport. None Stub loading disabled Stubs still work if they are in local classpath
LimitationsofRMI Java-only but you can use JNI on the serverUses TCP, not UDP At least two sockets per connectionUntested for huge loads
Sunvs.Microsoft RMI is not shipped as part of Microsoft’sproducts RMI will still work in applications include java.rmi.* class files in your classpathdownload rmi.zip from ftp.microsoft.com RMI will work in applets include java.rmi.* class files (or rmi.zip) in yourcodebase extra download time
RMIChatServerObjects Message interface interface ChatServer MessageReceiver - login(MessageReceiver) - receiveMessage(Message) - sendMessage(Message) ChatClient ChatServerImpl implements Dispatcher remote reference MessageQueue local reference