Top Banner
RMI REMOTE METHOD INVOCATION
24
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Basic java

RMI REMOTE METHOD INVOCATION

Page 2: Basic java

RMI applications often comprise two separate programs, a server and a client.

A typical server program creates some remote objects, makes references to these objects accessible, and waits for clients to invoke methods on these objects.

A typical client program obtains a remote reference to one or more remote objects on a server and then invokes methods on them.

RMI provides the mechanism by which the server and the client communicate and pass information back and forth.

Such an application is sometimes referred to as a distributed object application.

Page 3: Basic java

1. Writing an RMI Server Program.

2. Creating a Client Program

Page 4: Basic java

Distributed object applications need to do the following:

Locate remote objects: Applications can use various mechanisms to obtain references to remote objects.

For example, an application can register its remote objects with RMI's simple naming facility, the RMI registry.

Alternatively, an application can pass and return remote object references as part of other remote invocations.

Communicate with remote objects: Details of communication between remote objects are handled by RMI.

To the programmer, remote communication looks similar to regular Java method invocations.

Page 5: Basic java

Load class definitions for objects that are passed around: Because RMI enables objects to be passed back and forth,it provides mechanisms for loading an object's class definitions as well as for transmitting an object's data.

The following illustration depicts an RMI distributed application that uses the RMI registry to obtain a reference to a remote object.

The server calls the registry to associate (or bind) a name with a remote object. The client looks up the remote object by its name in the server's registry and then invokes a method on it.

Page 6: Basic java
Page 7: Basic java

Advantages of Dynamic Code Loading

One of the central and unique features of RMI is its ability to download the definition of an object's class if the class is not defined in the receiver's Java virtual machine.

All of the types and behavior of an object, previously available only in a single Java virtual machine, can be transmitted to another, possibly remote, Java virtual machine.

RMI passes objects by their actual classes, so the behavior of the objects is not changed when they are sent to another Java virtual machine

This capability enables new types and behaviors to be introduced into a remote Java virtual machine, thus dynamically extending the behavior of an application. The compute engine example in this trail uses this capability to introduce new behavior to a distributed program.

Page 8: Basic java

Remote Interfaces, Objects, and Methods

Like any other Java application, a distributed application built by using Java RMI is made up of interfaces and classes. The interfaces declare methods.

The classes implement the methods declared in the interfaces and, perhaps, declare additional methods as well.

In a distributed application, some implementations might reside in some Java virtual machines but not others.

Objects with methods that can be invoked across Java virtual machines are called remote objects.

An object becomes remote by implementing a remote interface, which has the following characteristics:

A remote interface extends the interface java.rmi.Remote. Each method of the interface declares java.rmi.RemoteException in its throws clause, in addition to any application-specific exceptions.

Page 9: Basic java

RMI treats a remote object differently from a non-remote object when the object is passed from one Java virtual machine to another Java virtual machine.

Rather than making a copy of the implementation object in the receiving Java virtual machine, RMI passes a remote stub for a remote object.

The stub acts as the local representative, or proxy, for the remote object and basically is, to the client, the remote reference.

The client invokes a method on the local stub, which is responsible for carrying out the method invocation on the remote object.

Page 10: Basic java

A stub for a remote object implements the same set of remote interfaces that the remote object implements.

This property enables a stub to be cast to any of the interfaces that the remote object implements.

However, only those methods defined in a remote interface are available to be called from the receiving Java virtual machine.

Page 11: Basic java

Creating Distributed Applications by Using RMI

Using RMI to develop a distributed application involves these general steps:

1. Designing and implementing the components of your distributed application.

2. Compiling sources.

3. Making classes network accessible.

4. Starting the application.

Page 12: Basic java

Designing and Implementing the Application Components

First, determine your application architecture, including which components are local objects and which components are remotely accessible.

Defining the remote interfaces: A remote interface specifies the methods that can be invoked remotely by a client.

Clients program to remote interfaces, not to the implementation classes of those interfaces.

The design of such interfaces includes the determination of the types of objects that will be used as the parameters and return values for these methods.

Page 13: Basic java

Writing an RMI Server The compute engine server accepts tasks from clients, runs the tasks, and returns any results.

The server code consists of an interface and a class.

The interface defines the methods that can be invoked from the client. Essentially, the interface defines the client's view of the remote object.

The class provides the implementation.

Page 14: Basic java

Designing a Remote Interface

At the core of the compute engine is a protocol that enables tasks to be submitted to the compute engine, the compute engine to run those tasks, and the results of those tasks to be returned to the client

This protocol is expressed in the interfaces that are supported by the compute engine.

Page 15: Basic java

Each interface contains a single method. The compute engine's remote interface, Compute, enables tasks to be submitted to the engine.

The client interface, Task, defines how the compute engine executes a submitted task.

The compute Compute interface defines the remotely accessible part, the compute engine itself. Here is the source code for the Compute interface:

Page 16: Basic java

package compute;

import java.rmi.Remote;

import java.rmi.RemoteException;

public interface Compute extends Remote {

<T> T executeTask(Task<T> t) throws RemoteException;

}

Page 17: Basic java

As a member of a remote interface, the executeTask method is a remote method. Therefore, this method must be defined as being capable of throwing a java.rmi.RemoteException. This exception is thrown by the RMI system from a remote method invocation to indicate that either a communication failure or a protocol error has occurred. A RemoteException is a checked exception, so any code invoking a remote method needs to handle this exception by either catching it or declaring it in its throws clause.

The second interface needed for the compute engine is the Task interface, which is the type of the parameter to the executeTask method in the Compute interface. The compute.Task interface defines the interface between the compute engine and the work that it needs to do, providing the way to start the work. Here is the source code for the Task interface:

package compute;

Page 18: Basic java

public interface Task<T> {

T execute();

}

The Task interface defines a single method, execute, which has no parameters and throws no exceptions. Because the interface does not extend Remote, the method in this interface doesn't need to list java.rmi.RemoteException in its throws clause.

The Task interface has a type parameter, T, which represents the result type of the task's computation. This interface's execute method returns the result of the computation and thus its return type is T.

Page 19: Basic java

The Compute interface's executeTask method, in turn, returns the result of the execution of the Task instance passed to it. Thus, the executeTask method has its own type parameter, T, that associates its own return type with the result type of the passed Task instance.

RMI uses the Java object serialization mechanism to transport objects by value between Java virtual machines. For an object to be considered serializable, its class must implement the java.io.Serializable marker interface. Therefore, classes that implement the Task interface must also implement Serializable, as must the classes of objects used for task results.

Page 20: Basic java

Different kinds of tasks can be run by a Compute object as long as they are implementations of the Task type.

The classes that implement this interface can contain any data needed for the computation of the task and any other methods needed for the computation.

Here is how RMI makes this simple compute engine possible.

Because RMI can assume that the Task objects are written in the Java programming language, implementations of the Task object that were previously unknown to the compute engine are downloaded by RMI into the compute engine's

Page 21: Basic java

Java virtual machine as needed.

This capability enables clients of the compute engine to define new kinds of tasks to be run on the server machine without needing the code to be explicitly installed on that machine.

The compute engine, implemented by the ComputeEngine class, implements the Compute interface, enabling different tasks to be submitted to it by calls to its executeTask method.

These tasks are run using the task's implementation of the execute method and the results, are returned to the remote client.

Page 22: Basic java

Implementing a Remote Interface

In general, a class that implements a remote interface should at least do the following:

1. Declare the remote interfaces being implemented.

2. Define the constructor for each remote object.

3. Provide an implementation for each remote method in the remote interfaces.

Page 23: Basic java

An RMI server program needs to create the initial remote objects and export them to the RMI runtime, which makes them available to receive incoming remote invocations.

This setup procedure can be either encapsulated in a method of the remote object implementation class itself or included in another class entirely.

The setup procedure should do the following:

1. Create and install a security manager

2. Create and export one or more remote objects

3. Register at least one remote object with the RMI registry (or with another naming service, such as a service accessible through the Java Naming and Directory Interface) for bootstrapping purposes

Page 24: Basic java

Declaring the Remote Interfaces Being Implemented

The implementation class for the compute engine is declared as follows:

public class ComputeEngine implements Compute

This declaration states that the class implements the Compute remote interface and therefore can be used for a remote object.

The ComputeEngine class defines a remote object implementation class that implements a single remote interface and no other interfaces.

The ComputeEngine class also contains two executable program elements that can only be invoked locally.

The first of these elements is a constructor for ComputeEngine instances. The second of these elements is a main method that is used to create a ComputeEngine instance and make it available to clients.