Java Rmi 简明教程

Java RMI Application

要编写 RMI Java 应用程序,您需要按照以下步骤进行操作 −

To write an RMI Java application, you would have to follow the steps given below −

  1. Define the remote interface

  2. Develop the implementation class (remote object)

  3. Develop the server program

  4. Develop the client program

  5. Compile the application

  6. Execute the application

Defining the Remote Interface

远程接口提供了特定远程对象的所有方法的描述。客户端与此远程接口进行通信。

A remote interface provides the description of all the methods of a particular remote object. The client communicates with this remote interface.

要创建远程接口 −

To create a remote interface −

  1. Create an interface that extends the predefined interface Remote which belongs to the package.

  2. Declare all the business methods that can be invoked by the client in this interface.

  3. Since there is a chance of network issues during remote calls, an exception named RemoteException may occur; throw it.

以下是远程界面的示例。在这里,我们定义了一个名为 Hello 的界面,它有一个称为 printMsg() 的方法。

Following is an example of a remote interface. Here we have defined an interface with the name Hello and it has a method called printMsg().

import java.rmi.Remote;
import java.rmi.RemoteException;

// Creating Remote interface for our application
public interface Hello extends Remote {
   void printMsg() throws RemoteException;
}

Developing the Implementation Class (Remote Object)

我们需要实现较早步骤中创建的远程界面。(我们可以单独编写一个实现类,或者我们可以直接让服务器程序实现此界面。)

We need to implement the remote interface created in the earlier step. (We can write an implementation class separately or we can directly make the server program implement this interface.)

要开发一个实现类 -

To develop an implementation class −

  1. Implement the interface created in the previous step.

  2. Provide implementation to all the abstract methods of the remote interface.

以下是实现类。在这里,我们创建了一个名为 ImplExample 的类,并实现了前一步骤中创建的界面 Hello 并为此方法提供了 body ,该方法打印一条消息。

Following is an implementation class. Here, we have created a class named ImplExample and implemented the interface Hello created in the previous step and provided body for this method which prints a message.

// Implementing the remote interface
public class ImplExample implements Hello {

   // Implementing the interface method
   public void printMsg() {
      System.out.println("This is an example RMI program");
   }
}

Developing the Server Program

RMI 服务器程序应该实现远程界面或扩展实现类。在这里,我们应该创建一个远程对象并将它绑定到 RMIregistry

An RMI server program should implement the remote interface or extend the implementation class. Here, we should create a remote object and bind it to the RMIregistry.

要开发一个服务器程序 -

To develop a server program −

  1. Create a client class from where you want invoke the remote object.

  2. Create a remote object by instantiating the implementation class as shown below.

  3. Export the remote object using the method exportObject() of the class named UnicastRemoteObject which belongs to the package java.rmi.server.

  4. Get the RMI registry using the getRegistry() method of the LocateRegistry class which belongs to the package java.rmi.registry.

  5. Bind the remote object created to the registry using the bind() method of the class named Registry. To this method, pass a string representing the bind name and the object exported, as parameters.

以下是 RMI 服务器程序的示例。

Following is an example of an RMI server program.

import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class Server extends ImplExample {
   public Server() {}
   public static void main(String args[]) {
      try {
         // Instantiating the implementation class
         ImplExample obj = new ImplExample();

         // Exporting the object of implementation class
         // (here we are exporting the remote object to the stub)
         Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);

         // Binding the remote object (stub) in the registry
         Registry registry = LocateRegistry.getRegistry();

         registry.bind("Hello", stub);
         System.err.println("Server ready");
      } catch (Exception e) {
         System.err.println("Server exception: " + e.toString());
         e.printStackTrace();
      }
   }
}

Developing the Client Program

在其中编写一个客户端程序,用这个对象获取远程对象并调用所需的方法。

Write a client program in it, fetch the remote object and invoke the required method using this object.

要开发一个客户端程序 -

To develop a client program −

  1. Create a client class from where your intended to invoke the remote object.

  2. Get the RMI registry using the getRegistry() method of the LocateRegistry class which belongs to the package java.rmi.registry.

  3. Fetch the object from the registry using the method lookup() of the class Registry which belongs to the package java.rmi.registry. To this method, you need to pass a string value representing the bind name as a parameter. This will return you the remote object.

  4. The lookup() returns an object of type remote, down cast it to the type Hello.

  5. Finally invoke the required method using the obtained remote object.

下面是一个 RMI 客户端程序示例。

Following is an example of an RMI client program.

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Client {
   private Client() {}
   public static void main(String[] args) {
      try {
         // Getting the registry
         Registry registry = LocateRegistry.getRegistry(null);

         // Looking up the registry for the remote object
         Hello stub = (Hello) registry.lookup("Hello");

         // Calling the remote method using the obtained object
         stub.printMsg();

         // System.out.println("Remote method invoked");
      } catch (Exception e) {
         System.err.println("Client exception: " + e.toString());
         e.printStackTrace();
      }
   }
}

Compiling the Application

要编译应用程序-

To compile the application −

  1. Compile the Remote interface.

  2. Compile the implementation class.

  3. Compile the server program.

  4. Compile the client program.

或者,

Or,

打开存储所有程序的文件夹,然后按如下所示编译所有 Java 文件。

Open the folder where you have stored all the programs and compile all the Java files as shown below.

Javac *.java
stored programs

Executing the Application

Step 1 - 使用以下命令启动 rmi 注册表。

Step 1 − Start the rmi registry using the following command.

start rmiregistry
start execution

这会在一个单独的窗口中启动一个 rmi 注册表,如下所示。

This will start an rmi registry on a separate window as shown below.

separate window

Step 2 - 按如下所示运行服务器类文件。

Step 2 − Run the server class file as shown below.

Java Server
run server

Step 3 - 按如下所示运行客户端类文件。

Step 3 − Run the client class file as shown below.

java Client
run client

Verification - 在启动客户端后,您很快就会在服务器中看到以下输出。

Verification − As soon you start the client, you would see the following output in the server.

output