Java Rmi 简明教程

Java RMI - Introduction

RMI 代表 Remote Method Invocation 。它是一种允许驻留在一个系统(JVM)中的对象访问/调用运行在另一个 JVM 上的对象的机制。

RMI stands for Remote Method Invocation. It is a mechanism that allows an object residing in one system (JVM) to access/invoke an object running on another JVM.

RMI 用于构建分布式应用程序;它提供 Java 程序之间的远程通信。它在包 java.rmi 中提供。

RMI is used to build distributed applications; it provides remote communication between Java programs. It is provided in the package java.rmi.

Architecture of an RMI Application

在 RMI 应用程序中,我们编写两个程序,一个 server program (驻留在服务器上)和一个 client program (驻留在客户端上)。

In an RMI application, we write two programs, a server program (resides on the server) and a client program (resides on the client).

  1. Inside the server program, a remote object is created and reference of that object is made available for the client (using the registry).

  2. The client program requests the remote objects on the server and tries to invoke its methods.

下图显示了 RMI 应用程序的架构。

The following diagram shows the architecture of an RMI application.

rmi architecture

现在让我们讨论此架构的组件。

Let us now discuss the components of this architecture.

  1. Transport Layer − This layer connects the client and the server. It manages the existing connection and also sets up new connections.

  2. Stub − A stub is a representation (proxy) of the remote object at client. It resides in the client system; it acts as a gateway for the client program.

  3. Skeleton − This is the object which resides on the server side. stub communicates with this skeleton to pass request to the remote object.

  4. RRL(Remote Reference Layer) − It is the layer which manages the references made by the client to the remote object.

Working of an RMI Application

以下要点总结了 RMI 应用程序的工作原理 −

The following points summarize how an RMI application works −

  1. When the client makes a call to the remote object, it is received by the stub which eventually passes this request to the RRL.

  2. When the client-side RRL receives the request, it invokes a method called invoke() of the object remoteRef. It passes the request to the RRL on the server side.

  3. The RRL on the server side passes the request to the Skeleton (proxy on the server) which finally invokes the required object on the server.

  4. The result is passed all the way back to the client.

Marshalling and Unmarshalling

每当客户端调用远程对象上接受参数的方法时,参数都会被捆绑到消息中,然后通过网络发送。这些参数可能是基本类型或对象。如果是基本类型,则将参数放在一起并为其附加标题。如果是对象,则对它们进行序列化。此过程称为 marshalling

Whenever a client invokes a method that accepts parameters on a remote object, the parameters are bundled into a message before being sent over the network. These parameters may be of primitive type or objects. In case of primitive type, the parameters are put together and a header is attached to it. In case the parameters are objects, then they are serialized. This process is known as marshalling.

在服务器端,已打包的参数将被解绑,然后调用所需的方法。此过程称为 unmarshalling

At the server side, the packed parameters are unbundled and then the required method is invoked. This process is known as unmarshalling.

RMI Registry

RMI 注册表是在其上放置所有服务器对象的命名空间。每次服务器创建对象时,它都会使用 RMIregistry(使用 bind()reBind() 方法)注册此对象。它们使用称为 bind name 的唯一名称进行注册。

RMI registry is a namespace on which all server objects are placed. Each time the server creates an object, it registers this object with the RMIregistry (using bind() or reBind() methods). These are registered using a unique name known as bind name.

要调用远程对象,客户端需要该对象的引用。那时,客户端使用其绑定名称(使用 lookup() 方法)从注册表中获取对象。

To invoke a remote object, the client needs a reference of that object. At that time, the client fetches the object from the registry using its bind name (using lookup() method).

以下插图对整个过程进行了说明 −

The following illustration explains the entire process −

registry

Goals of RMI

以下是 RMI 的目标 −

Following are the goals of RMI −

  1. To minimize the complexity of the application.

  2. To preserve type safety.

  3. Distributed garbage collection.

  4. Minimize the difference between working with local and remote objects.