【问题标题】:RMI call not adding callback listenerRMI 调用未添加回调侦听器
【发布时间】:2013-01-21 16:53:27
【问题描述】:

我在理解我正在制作的程序的内部运作时遇到了问题。 该程序应该是一个与服务器联系并自行注册的 RMI 客户端。然后,服务器应该循环调用客户端上的方法。 但是,没有添加监听器。

注意输出。在服务器上添加监听器后,该方法打印出正确的大小,但运行服务器的线程没有,因为列表仍然是空的。为什么哦,为什么会这样?

客户

public class GameClient extends Thread implements Remote, Client, ModelChangeListener<Client>{
private static final long serialVersionUID = -394039736555035873L;
protected Queue<GameModelEvent> queue = new ConcurrentLinkedQueue<GameModelEvent>(); 


public GameClient(){

}

public static void main(String[] args){
    GameClient client = new GameClient();
    client.start();
}


protected void bind(){
    System.setProperty("java.rmi.server.codebase","file:bin/");
    try {
        Registry registry = LocateRegistry.getRegistry();
        Client c = (Client)UnicastRemoteObject.exportObject(this, 10999);
        Server stub = (Server) registry.lookup("Server");
        stub.registerClient(c);
    } catch (RemoteException | NotBoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}



@Override
public void run() {
    super.run();
    bind();
    while(!Thread.interrupted()){
        System.out.print(".");
        GameModelEvent event = queue.poll();
        while(event != null){

            System.out.println(event);


            event = queue.poll();
        }

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            return;
        }
    }

}

   .....
}

服务器

public class GameServer extends Thread implements ModelChangeListener<GameServer>, Server{
protected Queue<GameModelEvent> queue = new ConcurrentLinkedQueue<GameModelEvent>(); 
List<Client> clients = Collections.synchronizedList(new ArrayList<Client>());

public static void main(String[] args){
    GameServer server = new GameServer();
    server.start();
}

protected void bind(){
    System.setProperty("java.security.policy","file:policy.policy");
    System.setProperty("java.rmi.server.codebase","file:bin/");

    if (System.getSecurityManager() == null) {
        System.setSecurityManager(new SecurityManager());
    }
    try {
        String name = "Server";
        Server engine = new GameServer();
        Server stub =
            (Server) UnicastRemoteObject.exportObject(engine, 0);
        Registry registry = LocateRegistry.getRegistry();
        registry.rebind(name, stub);
        System.out.println("ComputeEngine bound");
    } catch (Exception e) {
        System.err.println("GameServer exception:");
        e.printStackTrace();
        System.exit(1);
    }
}

public void run() {
    super.run();
    bind();
    while(!Thread.interrupted()){
        System.out.print(clients.size()+ " ");

            try {
                for(Client c : clients)
                c.modifyConnection(null);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        GameModelEvent event = queue.poll();
        while(event != null){

            System.out.println(event);


            event = queue.poll();
        }

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            return;
        }
    }

}
@Override
public void registerClient(Client client) {
    System.out.println("\nAdded client "+client);
    clients.add(client);
    System.out.println("clients size "+clients.size());     
}
 ...
 }

输出

ComputeEngine bound
0 0 0 0 
Added client Proxy[Client,RemoteObjectInvocationHandler[UnicastRef [liveRef: [endpoint:[10.117.2.88:10999](remote),objID:[5c30c56:13c5dfe5faf:-7fff, 1084850783049542281]]]]]
clients size 1
0 0 0 0 0 

【问题讨论】:

    标签: java multithreading rmi


    【解决方案1】:

    您正在绑定的 GameServer 实例与您在 main 方法中创建的实例不同。

    绑定实例将接收 RMI 调用,而主方法实例将被线程使用 - 因此您有两个不同的客户端列表。

    GameServer 中替换行:

    UnicastRemoteObject.exportObject(engine, 0);
    

    与:

    UnicastRemoteObject.exportObject(this, 0);
    

    【讨论】:

    • 哦,大声哭泣!有缘由。搞砸了,我要为这个投反对票!:) 谢谢。点赞并接受
    • 尤其是在处理您不确定应该如何工作的东西时。您倾向于将注意力从明显的“假设”问题上转移
    猜你喜欢
    • 1970-01-01
    • 2012-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-25
    • 2019-01-26
    • 1970-01-01
    相关资源
    最近更新 更多