如何使javarmi进程不终止

ar7v8xwq  于 2021-07-14  发布在  Java
关注(0)|答案(0)|浏览(172)

节点.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());
        }
    }

}

nodeinterface.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

为了实现这一点,我需要对代码进行哪些更改?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题