有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

作用域Java RMI对来自客户机本身和来自服务器的远程调用的客户机变量有不同的实例化

我们正在尝试使用javasrmi创建一个系统。问题在于,无法使用JavaRMI从服务器访问客户机上维护的列表。RMI连接似乎正在处理初始化列表的副本

下面是一个使用整数的最小示例,客户端每秒递增一个整数,直到它等于10。但服务器始终收到0

有人知道我们做错了什么吗

只需将服务器和客户端作为java应用程序运行即可

ServerDefaultImpl。java

package rmi;

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

public class ServerDefaultImpl implements EIServerRemote, Runnable {
    ClientRemote client;
    private boolean running = true;

    public ServerDefaultImpl() {
        try {
            LocateRegistry.createRegistry(Registry.REGISTRY_PORT);

            ServerDefaultImpl server = this;
            EIServerRemote stub = (EIServerRemote) UnicastRemoteObject.exportObject(server, 0);

            Registry registry = LocateRegistry.getRegistry();
            registry.rebind("test", stub);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        new Thread(this).start();
    }

    public static void main(String[] args) {
        new ServerDefaultImpl();
    }

    @Override
    public void run() {
        while (true == running) {
            try {
                Thread.sleep(1000);
                if (null != client) { //Client not connected yet.
                    int test = client.test();
                    System.out.println(test);
                    running = test <= 10;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void attachClientListener(ClientRemote client) throws RemoteException {
        this.client = client;
    }
}

EIServerRemote。java

package rmi;

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

public interface EIServerRemote extends Remote {
    void attachClientListener(ClientRemote client) throws RemoteException;
}

ClientRemote。java

package rmi;

import java.io.Serializable;
import java.rmi.Remote;

public interface ClientRemote extends Remote,Serializable {
    int test();
}

ClientDefaultImpl。java

package rmi;

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

public class ClientDefaultImpl implements Runnable,
        ClientRemote {

    private static final long serialVersionUID = 4846141863099303590L;

    protected EIServerRemote server = null;

    public int test;

    public boolean running = true;

    public ClientDefaultImpl(String serverName) {
        test = 0;
        try {
            connect(serverName);
        } catch (RemoteException | NotBoundException e) {
            e.printStackTrace();
        }
        new Thread(this).start();
    }

    public static void main(String[] args) {
        new ClientDefaultImpl("test");
    }

    public void connect(String serverName) throws RemoteException,
            NotBoundException {
        Registry registry = LocateRegistry.getRegistry();
        EIServerRemote s = (EIServerRemote) registry.lookup(serverName);
        server = s;
        s.attachClientListener((ClientRemote) this);
    }

    @Override
    public void run() {
        while (true == running) {
            try {
                Thread.sleep(1000);
                System.out.println(test++);
                running = test <= 10;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public int test() {
        return test;
    }
}

共 (1) 个答案

  1. # 1 楼答案

    It seems that the RMI connection is handling a copy of the initialized list.

    没错。列表不是远程对象,因此它是通过序列化传递和返回的