java - RMI 服务器,变量值未更新

标签 java rmi

这只是一个测试,旨在了解 java RMI 服务器如何工作。如果我执行服务器并与 RMI 客户端通信,则当客户端远程调用 getData() 方法时,应该更新变量“data”。但程序却继续打印“hello 2”。这意味着变量没有更新,可能是因为服务器在另一个进程上执行。有人可以澄清这一点吗?

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.logging.Level;
import java.util.logging.Logger;

public class RMIServer extends UnicastRemoteObject implements RMIi{

    public RMIServer() throws RemoteException{
        super();
    }

    static String data = "Hello";

    @Override
    public String getData(String text) throws RemoteException {
        data = "Hello 4";
        return data;
    }

    public static void main (String[] args){
        while(true) {
            try{
                data = "Hello 2";
                Registry reg = LocateRegistry.createRegistry(1099);
                reg.rebind("server", new RMIServer());
                System.out.println("Server Started");
                data = "Hello 3";
            }
            catch(Exception e){
                System.out.println(e);
            }

            System.out.println(data);
        }
    }

}

最佳答案

If I execute the server and communicate with an RMI client, the variable "data" should be updated when the getData() method is remotely called by the client.

正确。

But instead the program keeps printing "hello 2".

什么程序?这里没有任何东西可以打印出来。但是 data 是“Hello 2”,因为 main() 中存在毫无意义的循环。删除它。

This means that the variable is not updated

不,没有。这意味着您在一个毫无意义的循环中不断将其设置为该值,同时还不断创建新的服务器实例。这里没有证据表明您已经执行了客户端。

maybe because the server is executed on another process.

没有。

注意数据不应该是静态的。

关于java - RMI 服务器,变量值未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35089595/

相关文章:

c# - dot net (C#)中是否有类似于java RMI的概念或实现?

java - 跨Java系统的配置设计模式

java - 这个java错误是什么意思

java - 由于外部库中的 org.junit.Test 类依赖关系,无法使用 LeakCanary

java - 阅读源代码

java - JMX/RMI,其中服务器没有全局唯一的网络名称

java - 如何在 ItestListener 中获取当前的类驱动程序

java - 如何在不对 IP 进行硬编码的情况下从本地主机外部访问 Java RMI/JMX?

parameters - 使用多个参数调用 Apache Camel RMI 端点调用方法

java - RMI : determine the IP address of a remote object