了解 RMI 期间的调用序列

发布于 2024-12-22 06:17:29 字数 2562 浏览 2 评论 0原文

我对远程方法调用期间的调用顺序有很多疑问。 以下是我为了理解 RMI 而阅读的 4 段无错误代码。

1.) 接口

import java.rmi.*;

public interface AddServerIntf extends Remote {
    double add(double d1, double d2) throws RemoteException;
}

2.) 实现

import java.rmi.*;
import java.rmi.server.*;

public class AddServerImpl extends UnicastRemoteObject implements AddServerIntf {
 public AddServerImpl() throws RemoteException {} // what is it meant for ?

 public double add(double d1 , double d2) throws RemoteException {
      return d1 + d2;
 }
} 

3. ) AddServer类

import java.net.*;
import java.rmi.*;

  public class AddServer {
   public static void main(String args[]) {
    try {
        AddServerImpl addServerImpl = new AddServerImpl();
        Naming.rebind("AddServer",addServerImpl); // what does it do and how ?
     }  catch(Exception exc) {
        System.out.println("Exception : " + exc);
       }
   }
  }

4.)AddClient类

import java.rmi.*;

public class AddClient {
  public static void main(String args[]) {
    try {
      String addServerURL = "rmi://" + args[0] + "/AddServer";
      AddServerIntf addServerIntf = (AddServerIntf)Naming.lookup(addServerURL); // how does it look up and how does it return ?
      System.out.println("The first number is : " + args[1]);
      double d1 = Double.valueOf(args[1]).doubleValue();
      System.out.println("The second number is : " + args[2]);
      double d2 = Double.valueOf(args[2]).doubleValue();
      System.out.println("The sum is : " + addServerIntf.add(d1,d2)); // what does it mean to call the method of an interface ?

    } catch(Exception exc) {
        System.out.println(exc);
      }
  }
}

服务器上的文件:AddServer.class、AddServerImpl.class、AddServerImpl_Stub.class、AddServerIntf.class

客户端计算机上的文件:AddServerIntf.class、AddClient.class、AddServerImpl_Stub.class

将上述文件保留在各自的位置后,我在服务器计算机上启动rmiregistry,然后使用 java AddServer 启动服务器,然后使用 java 127.0.0.1 20 30 启动客户端

一旦客户端启动,该语句会执行什么操作(AddServerIntf)Naming.lookup(addServerURL); 代码中 AddClient 做什么?

当客户端程序开始执行时执行哪些例程?它如何返回总和?

语句 Naming.rebind("AddServer",addServerImpl); 在类 AddServer 中起什么作用?

最后,当我使用 addServerIntf.add(d1,d2) 调用获取总和时,这是什么意思(调用接口方法)?为什么我要调用接口的方法?那有什么作用呢?

我无法理解RMI的机制,谁调用谁以及如何调用?

I have many doubts regarding the call sequence during Remote Method Invocation.
Following are the 4 error free pieces of code that i read to understand RMI.

1.) INTERFACE

import java.rmi.*;

public interface AddServerIntf extends Remote {
    double add(double d1, double d2) throws RemoteException;
}

2.) IMPLEMENTATION

import java.rmi.*;
import java.rmi.server.*;

public class AddServerImpl extends UnicastRemoteObject implements AddServerIntf {
 public AddServerImpl() throws RemoteException {} // what is it meant for ?

 public double add(double d1 , double d2) throws RemoteException {
      return d1 + d2;
 }
} 

3.) AddServer Class

import java.net.*;
import java.rmi.*;

  public class AddServer {
   public static void main(String args[]) {
    try {
        AddServerImpl addServerImpl = new AddServerImpl();
        Naming.rebind("AddServer",addServerImpl); // what does it do and how ?
     }  catch(Exception exc) {
        System.out.println("Exception : " + exc);
       }
   }
  }

4.)AddClient Class

import java.rmi.*;

public class AddClient {
  public static void main(String args[]) {
    try {
      String addServerURL = "rmi://" + args[0] + "/AddServer";
      AddServerIntf addServerIntf = (AddServerIntf)Naming.lookup(addServerURL); // how does it look up and how does it return ?
      System.out.println("The first number is : " + args[1]);
      double d1 = Double.valueOf(args[1]).doubleValue();
      System.out.println("The second number is : " + args[2]);
      double d2 = Double.valueOf(args[2]).doubleValue();
      System.out.println("The sum is : " + addServerIntf.add(d1,d2)); // what does it mean to call the method of an interface ?

    } catch(Exception exc) {
        System.out.println(exc);
      }
  }
}

Files on server machine : AddServer.class,AddServerImpl.class,AddServerImpl_Stub.class , AddServerIntf.class

Files on client machine : AddServerIntf.class,AddClient.class,AddServerImpl_Stub.class

After keeping the above files in their respective places,i start the rmiregistry on the server machine,then start the server using java AddServer and then start the client using java 127.0.0.1 20 30

As soon as the client starts what does the statement (AddServerIntf)Naming.lookup(addServerURL); in the code AddClient do ?

What are the routines that are carried out when the client program starts executing ? How does it return the sum ?

What does the statement Naming.rebind("AddServer",addServerImpl); do in the class AddServer ?

Finally when i call to fetch the sum using addServerIntf.add(d1,d2) , what does it mean (calling the method of interface) ? Why am i calling the method of an interface ? And what does that do ?

I am unable to understand the mechanism of RMI , who calls whom and HOW ?.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤千羽 2024-12-29 06:17:29

RMI:远程方法调用

一般来说,当我们说我调用一个方法时,我们隐含地假设类的方法与调用类加载在同一个 JVM 中。 RMI 允许您执行相同的操作,但调用方法和被调用者位于不同的 JVM 中(通常位于两个不同的物理机上)。

在典型的 RMI 中,有两个部分:

  1. 客户端:调用的对象
  2. 服务器:接受客户端调用的对象。

在一一回答您的问题之前,先介绍一下典型的客户端服务器通信是如何发生的(高级)。

服务器启动

  • 远程 (AddServerImpl) 对象注册到其注册表。
  • 开始连接端口(标准 999)。

客户端调用远程方法

  • 客户端查询服务器注册表中的远程对象 ((AddServerIntf)Naming.lookup(addServerURL))
  • 服务器返回远程存根(它实现与远程对象相同的接口)对象)
  • 客户端调用存根上的方法(addServerIntf.add(d1,d2))
  • 存根序列化(转换为可以通过以下方式传输的格式网络)输入((d1,d2)),将它们作为请求传递到服务器。
  • 在服务器端,输入被反序列化(从通过网络接收的数据重新创建 java 对象),服务器在真实对象上执行方法 (AddServerImpl)。
  • 方法输出(return d1 + d2)由方法返回,服务器序列化输出并将其作为响应发送到调用客户端。
  • 存根反序列化响应,您就得到了输出!

让我回答您的问题

代码 AddClient 中的语句 (AddServerIntf)Naming.lookup(addServerURL); 有何作用

--这是对服务器的请求,以提供对我们打算调用其方法的对象的远程引用。

Naming.rebind("AddServer",addServerImpl); 语句的作用是什么?在 AddServer 类中做什么?

--这是为了确保服务器知道(在其注册表中添加)有一个名为“AddServer”的对象,以便每当有调用该对象的方法的请求时,就可以上桌了。

当我调用 addServerIntf.add(d1,d2) 来获取总和时,这是什么意思(调用接口方法)?为什么我要调用接口的方法?它有什么作用?

--接口是一种实现抽象的java机制。现在,当您调用接口时,在幕后,会调用实现相同接口的对象的方法(在本例中是远程调用服务器上的对象的方法)。为了回答“为什么”,抽象可以帮助您将使用和实现分开。这样调用者就不需要知道implementation()的细节,而只需要关心输入和输出参数。

回答您剩下的问题

当客户端程序开始执行时执行的例程是什么?它是如何返回总和的?

--到目前为止的背景,请参考此链接。它的解释比我更好:)。

RMI : Remote Method invocation.

Generally when we say I invoke a method, we implicitly assume that the method of a class loaded in the same JVM as the invoking class. RMI lets you do the same thing but the calling method and the callee are in different JVMs (typically on two different physical machine).

In a typical RMI there are two parts:

  1. Client : the object which calls
  2. Server : The object which entertains the calls of the client.

Before answering your questions one by one, here is how a typical client server communication takes place (high level).

Server starts up

  • Register the Remote (AddServerImpl ) object to its registry.
  • Starts litening to a port (standard 999).

Client invoking remote method

  • Client queries the server regitery for remote object ((AddServerIntf)Naming.lookup(addServerURL))
  • Server returns remote stub (which implements the same interface as the remote object)
  • Client calls method on the stub (addServerIntf.add(d1,d2)))
  • The stub serializes (converting to a format which could be transmitted over network) the inputs ((d1,d2)), passes them to sever as a request.
  • On server side, he input is de-serialized (recreating java objects from the data received over network) and server executes the method on the real object (AddServerImpl ).
  • The method output (return d1 + d2) is returned by method, server serializes the output and sends it as response to calling client
  • The stub de-serializes the response and you have output !

Let me answer your questions

what does the statement (AddServerIntf)Naming.lookup(addServerURL); in the code AddClient do ?

--This is the request to the Server to give the remote reference to the object whose method we intent to call.

What does the statement Naming.rebind("AddServer",addServerImpl); do in the class AddServer ?

--This is to make sure that server knows (added in its registry) that there is an object with name "AddServer" SO THAT whenever there is a request for invocation of method of this object, it could be served.

when i call to fetch the sum using addServerIntf.add(d1,d2) , what does it mean (calling the method of interface) ? Why am i calling the method of an interface ? And what does that do ?

--An interface is a java mechanism to achieve abstraction. Now when you call an interface, under the hood, a method of an object implementing the same interface is called (in this case remotely, of the object on server). To answer 'Why', abstraction helps you keep usage and implementation separate. So that the caller need not know the details of implementation () and it should only worry only about the the input and output parameters.

To answer your remaining question

What are the routines that are carried out when the client program starts executing ? How does it return the sum ?

--With the background so far, please refer to this link . Its has explained in far nicer way than I could :).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文