• Aucun résultat trouvé

Remote Method Invocation (RMI) .1 RMI Architecture

Dans le document DISTRIBUTED NETWORK SYSTEMS (Page 192-196)

Table of Figures

CHAPTER 6 TCP/UDP COMMUNICATION IN JAVA

7.5 Remote Method Invocation (RMI) .1 RMI Architecture

Java Remote Method Invocation (RMI) is a simple, yet powerful, Java-based framework for distributed object design and implementation. A remote invocation is a form of the RPC, where procedures can be invoked from remote machines. Java RMI extends the RPC further to the distributed objects’ world, that is, RMI permits executing methods of objects residing on remote machines, with results returned to the calling environment.

RMI is a higher level abstraction than servers. In client-server computing, we typically develop an application level protocol to communicate between Java clients and servers, but with RMI we do not need to do this as RMI takes care of communication details for us. Using RMI is as simple as invoking a method of an object.

Figure 7.9: The RMI architecture

A client RMI call invokes the client-side stub (the proxy of the remote method that resides on the client’s machine). The stub uses Object Serialization to marshal the arguments, i.e., render argument object values into a stream of bytes that can be transmitted over a network. The stub then passes control to the Java Virtual Machine’s RMI layer. The skeleton on the server side dispatches the call to the actual remote object after unmarshaling the arguments into variables in memory.

The stub and skeleton programs are generated by the rmic compiler.

The Remote Reference layer permits various protocols for remote invocation, such as unicast point-to-point (the one currently has been implemented).

Before a remote object can be accessed, it has to be registered into the naming server. The RMI framework provides a simple naming service. Remote objects can register to the naming server using the java.rmi.Naming class with a URL-like naming scheme.

7.5.2 RMI Implementation

The key interfaces and classes in RMI are:

Remote is an interface in the java.rmi package. It defines all remote interfaces.

RMI server functions are provided by the RemoteObject and its subclasses.

Figure 7.9 shows the RMI architecture in which a Java client invokes a remote Java server object.

RemoteServer is a subclass of RemoteObject.

UnicastRemoteObject is a subclass of RemoteObject in the java.rmi.server package.

Remote Exception is a class in the java.rmi package, used for RMI to throw exceptions at runtime.

The RMI implementation involves the following steps:

Defining the remote interface. This is the interface through which remote clients will access the server and is done by extending the Remote interface and defining methods that can be invoked remotely.

Implementing the remote interface. Remote method calls will ultimately be made upon their implementations. The interface is normally implemented via the extending of the UnicastRemoteObject class. The UnicastRemoteObject class defines a remote object which is valid when the server is running. This object hides the implementation of the interface from the public interface and can contain some methods that are not visible through the interface. Any remote object passed as an argument to RMI must also be defined as an interface.

Creating stubs and skeletons using rmic compiler.

Compiling the remote interface and implementation file using javac compiler.

Creating a client program, either a pure Java application or an applet and the HTML page to invoke the server services.

A Simple RMI Example

This example creates a simple date server to provide the date and time information to clients. The first step is to define the server interface, named DateServer, that lists all methods a client can call. In this example, only one method is defined. Here is the Java program DateServer.java:

Note that the getDate() method must throw a RemoteException to allow the program to detect problems occurred in remote invocation. The second step is to implement the remote object interface, through the program DateServerImpl.java:

All remote object implementations must extend RemoteObject or one of its subclasses (such as the UnicastRemoteObject, provided by the JDK for implementing TCP-based client-server programs). The getDate() method simply returns the date information of the server host. The main() method creates a new DateServerImpl named dateServer and registers it to the RMI naming registry using the name of “Date Server”. If the name is already registered, then an AlreadyBoundException will be raised. To overcome this, we could use the rebind() method instead of the bind() method.

The third step is to generate the stub and the skeleton programs:

The two classes (DateServerImpl_Stub.class and DateServerImpl_Skel.class) will be generated after the compilation.

The fourth step is to create the client program to access the services provided by the server. The client is named DateClient.java:

The client program uses the lookup() method of java. rmi. Naming to get the information of the “Date Server” from the registry. The lookup() method has two parameters, one provides the location of the registry and the other provides the name of the server.

The last step of using this simple RMI program is to run it via the following executions:

Start the registry: rmiregistry Start the server: java DateServerImpl Start the client: java DateClient localhost 7.5.3 Interfaces and Classes

The main packages for the RMI framework are: java.rmi, java.rmi.server, java.rmi.registry, java.rmi.dgc, and java.rmi.activation (Java 1.2).

java.rmi: provides the Remote interface, a class for accessing remote names, the MarshalledObject class, and a security manager for RMI.

java.rmi.registry: provides classes and interfaces that are used by the remote registry.

java.rmi.server: provides the classes and interfaces used to implement remote objects, stubs, and skeletons, and to support RMI communication. This package implements the bulk of the RMI API.

java.rmi.activation: supports persistent object references and remote object activation.

java.rmi.dgc: provides classes and interfaces that are used by the RMI distributed garbage collector.

Dans le document DISTRIBUTED NETWORK SYSTEMS (Page 192-196)