要编写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 .
开发服务器程序 :
从您想要调用远程对象的地方创建一个客户端类.
通过实例化实现类来创建远程对象,如下所示.
使用 exportObject()方法导出远程对象名为 UnicastRemoteObject 的类,它属于包 java.rmi.server .
使用属于包 java.rmi.registry 的 LocateRegistry 类的 getRegistry()方法获取RMI注册表.
使用名为注册表的类的 bind()方法将创建的远程对象绑定到注册表.对于此方法,将表示绑定名称和导出对象的字符串作为参数传递.
以下是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(); } } }
写一个其中的客户端程序,获取远程对象并使用此对象调用所需的方法.
开发客户端程序 :
创建一个客户端类,用于调用远程对象.
使用以下命令获取RMI注册表 getRegistry() LocateRegistry 类的方法,该类属于包 java.rmi.registry .
使用属于包 java.rmi的类 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(); } } }
编译application :
编译远程接口.
编译实现类.
编译服务器程序.
编译客户端程序.
或者,
打开存储所有程序的文件夹并编译所有Java文件,如下所示.
Javac * .java
步骤1 : 使用以下命令启动 rmi 注册表.
start rmiregistry
这将开始一个单独的窗口上的 rmi 注册表,如下所示.
第2步 : 运行服务器类文件,如下所示.
Java Server
第3步&减号;运行客户端类文件,如下所示.
java Client
验证 : 一旦启动客户端,您将在服务器中看到以下输出.