首页 > 解决方案 > 如何让 Java RMI 进程不终止

问题描述

节点.java:

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.io.Serializable; 

public class Node implements NodeInterface,Serializable {
    
    public Node(String URL) throws RemoteException {
        try {
            if (!(URL.equals("0"))) {
                NodeInterface nodeinterface;
                nodeinterface = (NodeInterface) Naming.lookup("0");
                nodeinterface.join("0");
            }
        }
        catch(Exception e) {
            System.out.println(String.format("Encountering issues while constructing the server class. Error %s%n", e.getMessage()));
        }
    }
      
    public boolean join (String nodeURL) throws RemoteException {
        synchronized (this) {
            try {
                System.out.println("printing from inside node 0");
                return true;
            }
            catch(Exception e) {
                throw new RemoteException(String.format("Error %s", e.getMessage()));
            }
        }
    }

    public static void main(String[] args) {
        try {
            Node node = new Node(args[0]);
            Naming.rebind(args[0],node);
            //System.out.println
        }
        catch(Exception e) {
            System.out.println(e.getMessage());
        }
    }

}

节点接口.java:

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

public interface NodeInterface extends Remote {
  public boolean join (String nodeURL) throws RemoteException;

}

当我编译此代码并执行java Node 0时,进程终止并执行java Node 1产生输出printing from inside node 0

我希望看到这项工作如下:在终端 1:

Java Node 0

然后,在 2 号航站楼:

Java Node 1

然后我希望在 1 号航站楼看到:

printing from inside node 0 

为了使这成为可能,我必须对我的代码进行哪些更改?

标签: javarmi

解决方案


推荐阅读