java remote method invocation

22
Java Remote Method Java Remote Method Invocation Invocation RMI RMI

Upload: colleen-lopez

Post on 30-Dec-2015

36 views

Category:

Documents


0 download

DESCRIPTION

Java Remote Method Invocation. RMI. Idea. If objects communicate with each other on one JVM why not do the same on several JVM’s? Build on top of the Socket layer Break down objects into a string of bytes and send these over the wire Use Client/Server architecture. - PowerPoint PPT Presentation

TRANSCRIPT

Java Remote Method Java Remote Method InvocationInvocation

RMIRMI

IdeaIdea

• If objects communicate with each other on If objects communicate with each other on one JVM why not do the same on several one JVM why not do the same on several JVM’s?JVM’s?

• Build on top of the Socket layerBuild on top of the Socket layer• Break down objects into a string of bytes Break down objects into a string of bytes

and send these over the wireand send these over the wire• Use Client/Server architectureUse Client/Server architecture

client server

network

params

Returnvalue

localobject

remoteobject

Client/Server ArchitectureClient/Server Architecture

• Client Client requestsrequests data from server data from server• Server sends the data to the client (Server sends the data to the client (responseresponse))• Data is exchanged in standard formatData is exchanged in standard format• RMI: Remote calls will go in both directionsRMI: Remote calls will go in both directions• Client ObjectClient Object: is the object one of whose methods : is the object one of whose methods

issue a remote callissue a remote call• Remote ObjectRemote Object is the object on the server whose is the object on the server whose

method is calledmethod is called

We need a Proxy ObjectWe need a Proxy Object

• The client calls a method on a special type The client calls a method on a special type of interface that is implemented by the of interface that is implemented by the remote objectremote object

• Call this interface a Call this interface a stubstub• We still have to deal with the parameters We still have to deal with the parameters

and return values, i.e. convert them into an and return values, i.e. convert them into an appropriate formatappropriate format

• Call this conversion Call this conversion marshallingmarshalling

MarshallingMarshalling

• Objects need to be disassembled to this formatObjects need to be disassembled to this format• Formatted object is sent over the wireFormatted object is sent over the wire• Object needs to be reassembled on the other sideObject needs to be reassembled on the other side• Hence use character format: if the parameter of Hence use character format: if the parameter of

return value is a primitive type just copy it (as a return value is a primitive type just copy it (as a sequence of bytes)sequence of bytes)

• If it is an object then disassemble to a stringIf it is an object then disassemble to a string• Call this last process Call this last process serializationserialization

What is a Stub?What is a Stub?

• It defines a remote interfaceIt defines a remote interface• Contains methods that will be eventually Contains methods that will be eventually

called remotelycalled remotely• It extends It extends java.rmi.Remotejava.rmi.Remote• All its methods must throw a All its methods must throw a java.rmi.RemoteExceptionjava.rmi.RemoteException

The Stub ProducesThe Stub Produces

• An identifier of the remote objectAn identifier of the remote object• A description of the method invokedA description of the method invoked• Marshalled parametersMarshalled parameters

Server SideServer Side

• There is another interface for the call (now local) There is another interface for the call (now local) that has to unmarshall the parameters and make that has to unmarshall the parameters and make the actual call with thesethe actual call with these

• Called a Called a skeletonskeleton (not needed by programmer) (not needed by programmer)• Possible return value has to be marshalled and Possible return value has to be marshalled and

sent back to the clientsent back to the client• Note the dynamic nature of marshalled objects Note the dynamic nature of marshalled objects

must be accomodatedmust be accomodated• Skeleton resides on the serverSkeleton resides on the server

Stubs must be downloaded!Stubs must be downloaded!

• From server to client, since it is the remote From server to client, since it is the remote object’s methods that must be calledobject’s methods that must be called

• If the client is to know about the stub, then If the client is to know about the stub, then the latter must be the latter must be registeredregistered somewhere somewhere

• The client must look the stub upThe client must look the stub up• The server enters the stub into the registry The server enters the stub into the registry

with with Naming.rebind()Naming.rebind()• The client looks it up with The client looks it up with Naming.lookup()Naming.lookup()

RMI Registry

RMI Registry

Client Server

Naming.lookup() Naming.rebind()Stub Impl, InterfaceInterface

Naming LookupNaming Lookup

• Stubs are normally downloaded when the Stubs are normally downloaded when the client calls a remote methodclient calls a remote method

• What do you do when there is no remote What do you do when there is no remote interface to facilitate downloading?interface to facilitate downloading?

• Use the Use the bootstrap registry servicebootstrap registry service the first the first time roundtime round

• This associates a This associates a namename (string) to an (string) to an objectobject on the server.on the server.

Naming Lookup (ctd)Naming Lookup (ctd)

• The The server Naming.rebind()server Naming.rebind() accepts accepts a (unique) name--a normal string, say a (unique) name--a normal string, say xxxxxx

• The client accesses it by means of a so-The client accesses it by means of a so-called called RMI URLRMI URL, e.g. , e.g. rmi://localhost:90/xxxrmi://localhost:90/xxx

• This is the argument to This is the argument to Naming.lookup()Naming.lookup() to obtain the remote reference.to obtain the remote reference.

Defining the Remote InterfaceDefining the Remote Interface

• It extends It extends java.rmi.Remotejava.rmi.Remote• Must be publicMust be public• Contains declarations of methods to be Contains declarations of methods to be

called remotelycalled remotely• Is the way to pass remote objectsIs the way to pass remote objects• All methods must declare All methods must declare java.rmi.RemoteExceptionjava.rmi.RemoteException in their in their throwsthrows clause clause

How to Generate StubsHow to Generate Stubs

• Stubs and skeletons are generated on the Stubs and skeletons are generated on the server from class filesserver from class files

• Use the RMI compiler Use the RMI compiler rmicrmic• Format:Format:

rmic <full class name> <includesrmic <full class name> <includes>>• Generates both in same directory as the Generates both in same directory as the

class fileclass file

Naming ConventionsNaming Conventions

• Assume the remote interface is called Assume the remote interface is called xxxxxx• Then the stub is called Then the stub is called xxx_Stubxxx_Stub• The skeleton is called The skeleton is called xxx_Skelxxx_Skel• The original class file is called The original class file is called xxxImplxxxImpl• The client’s class is called The client’s class is called xxxClientxxxClient

Write the ImplementationWrite the Implementation

• Implement the remote interfaceImplement the remote interface• Extend Extend java.rmi.server.UnicastRemoteObjectjava.rmi.server.UnicastRemoteObject

• Implement the methods to be called Implement the methods to be called remotelyremotely

• Other medthods may be available only Other medthods may be available only locallylocally

• Constructor must throw a remote exceptionConstructor must throw a remote exception

UnicastRemoteObjectUnicastRemoteObject

• Objects of this type are accessible by the Objects of this type are accessible by the TCP/IP protocolTCP/IP protocol

• It automatically connects to the RMI runtime It automatically connects to the RMI runtime systemsystem

• Can perform equality checks with other Can perform equality checks with other remote objectsremote objects

• You can also construct objects of this type You can also construct objects of this type with a specific port and,optionally, a with a specific port and,optionally, a SocketFactory on both server and client.SocketFactory on both server and client.

Set Up the ServerSet Up the Server

• Write a java main programWrite a java main program• Instantiate classes to become remote Instantiate classes to become remote

objectsobjects• Put in a Security ManagerPut in a Security Manager• Bind the remote objects to a name in the Bind the remote objects to a name in the

registry (more later)registry (more later)

Set Up a ClientSet Up a Client

• Obtain references to remote objects from Obtain references to remote objects from Naming.lookup()Naming.lookup()

• Call methods on those remote objects Call methods on those remote objects • Should set up a Security ManagerShould set up a Security Manager

How to ProceedHow to Proceed

1.1. Write the Write the remote interfaceremote interface. This is shared . This is shared between client and serverbetween client and server

2.2. Write the remote object implementationWrite the remote object implementation

3.3. Write the client application that invokes methods Write the client application that invokes methods of 2.of 2.

4.4. Compile all programs:Compile all programs:

javac xxx*.java -d classesjavac xxx*.java -d classes

5.5. Write server program that creates the remote Write server program that creates the remote objectsobjects

Running Your ProgramRunning Your Program

1. Start the RMI registry:1. Start the RMI registry:

start rmiregistrystart rmiregistry

2. Locate classes to make stubs: run 2. Locate classes to make stubs: run rmicrmic

3. Start the server:3. Start the server:

java -Djava.security.policy = java -Djava.security.policy = server.policy xxxServerserver.policy xxxServer

4. Run the client:4. Run the client:

java -Djava.security.policy = java -Djava.security.policy = client.policy xxxClientclient.policy xxxClient