目录

Java RMI - RMI应用程序( RMI Application)

要编写RMI Java应用程序,您必须按照以下步骤操作 -

  • 定义远程接口
  • 开发实现类(远程对象)
  • 开发服务器程序
  • 开发客户端程序
  • 编译应用程序
  • 执行应用程序

定义远程接口

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

要创建远程接口 -

  • 创建一个扩展属于该包的预定义接口Remote的接口。

  • 声明此接口中客户端可以调用的所有业务方法。

  • 由于远程调用期间可能存在网络问题,因此可能会发生名为RemoteException的异常; 丢它。

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

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

开发实现类(远程对象)

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

开发实现类 -

  • 实现上一步中创建的接口。
  • 为远程接口的所有抽象方法提供实现。

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

// 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");  
   }  
} 

开发服务器程序

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

开发服务器程序 -

  • 从要调用远程对象的位置创建客户端类。

  • 通过实例化实现类来Create a remote object ,如下所示。

  • 使用名为UnicastRemoteObject的类的exportObject()方法导出远程对象,该类属于java.rmi.server包。

  • 使用属于java.rmi.registry包的LocateRegistry类的LocateRegistry getRegistry()方法获取RMI注册表。

  • 使用名为Registry的类的bind()方法bind()创建的远程对象绑定到Registry 。 对于此方法,将表示绑定名称和导出对象的字符串作为参数传递。

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

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(); 
      } 
   } 
} 

开发客户端程序

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

制定客户计划 -

  • 从您想要调用远程对象的位置创建一个客户机类。

  • 使用属于java.rmi.registry包的LocateRegistry类的LocateRegistry getRegistry()方法获取RMI注册表。

  • 使用属于java.rmi.registry包的类Registry的方法lookup()从注册表中获取对象。

    对于此方法,您需要将表示绑定名称的字符串值作为参数传递。 这将返回远程对象。

  • lookup()返回一个remote类型的对象,向下转换为Hello类型。

  • 最后使用获取的远程对象调用所需的方法。

以下是RMI客户端程序的示例。

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(); 
      } 
   } 
}

编译应用程序

编译应用程序 -

  • 编译远程接口。
  • 编译实现类。
  • 编译服务器程序。
  • 编译客户端程序。

要么,

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

Javac *.java

存储程序

执行申请

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

start rmiregistry

开始执行

这将在单独的窗口上启动rmi注册表,如下所示。

单独的窗口

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

Java Server

运行服务器

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

java Client 

运行客户端

Verification - 一旦启动客户端,您将在服务器中看到以下输出。

产量
↑回到顶部↑
WIKI教程 @2018