Java中的非法远程方法
-
09-10-2019 - |
题
这是我第一次使用Java RMI*。我有一个自定义课程,可以扩展 UnicastRemoteObject
并实现扩展远程的接口。我认为我已经正确地实现了界面的方法,但我仍然得到 IllegalArgumentException
当我尝试运行代码时(这是关于没有参数的方法)。
JVM声称遇到了一种非法远程方法,但是该方法及其实现对我来说似乎还不错。
除了错误地实施或调用该方法外,还有其他原因可能发生这种例外吗?
这是堆栈跟踪:
SEVERE: null
java.rmi.server.ExportException: remote object implements illegal remote interface; nested exception is:
java.lang.IllegalArgumentException: illegal remote method encountered: public abstract java.lang.String Node.getId()
at sun.rmi.server.UnicastServerRef.exportObject(Unknown Source)
at java.rmi.server.UnicastRemoteObject.exportObject(Unknown Source)
at java.rmi.server.UnicastRemoteObject.exportObject(Unknown Source)
at java.rmi.server.UnicastRemoteObject.<init>(Unknown Source)
at java.rmi.server.UnicastRemoteObject.<init>(Unknown Source)
at NodeImpl.<init>(NodeImpl.java:30)
at NodeLauncher.main(NodeLauncher.java:11)
Caused by: java.lang.IllegalArgumentException: illegal remote method encountered: public abstract java.lang.String Node.getId()
at sun.rmi.server.Util.checkMethod(Unknown Source)
at sun.rmi.server.Util.getRemoteInterfaces(Unknown Source)
at sun.rmi.server.Util.getRemoteInterfaces(Unknown Source)
at sun.rmi.server.Util.createProxy(Unknown Source)
... 7 more
这是接口:
import java.rmi.*;
import java.util.LinkedList;
interface Node extends Remote
{
public boolean isAlive();
public LinkedList<NodeImpl> getLeafNodes();
public LinkedList<NodeImpl> getNeighborhoodList();
public String [] getRoutingTable();
public NodeImpl initiation(String credentials,Object application);
public String route(String message,String key);
public void inform(byte [] id);
public String getId();
public boolean isConnected();
public void applicationClose();
public boolean distanceMeasure();
}
这是班级的构造函数:
public NodeImpl() throws RemoteException
{
super();
l=4;
M=1;
nodeId=new byte [16];
Random r=new Random();
r.nextBytes(nodeId);
leafNodes=new LinkedList<NodeImpl>();
connected=false;
ng=new NodeGUI(this);
for(int i=0;i<l;i++)
{
leafNodes.add(null);
}
neighborhoodList=new LinkedList<NodeImpl>();
anyNodeWhoAnswered=new LinkedList<byte []>();
it=new InformingTimer(this);
Thread informingTimerThread=new Thread(it);
informingTimerThread.start();
try
{
Naming.rebind("rmi://" + "localhost" + ":1099/"+nodeId, this);
}
catch (Exception ex)
{
Logger.getLogger(NodeImpl.class.getName()).log(Level.SEVERE, null, ex);
}
bootstrap();
}
解决方案
RMI上的所有方法 Remote
接口必须声明 RemoteException
在他们的 throws
条款,例如:
public String getId() throws RemoteException;
目前尚不清楚为什么异常名称 getId()
具体来说,这可能只是它检查的第一种方法。
另外, getLeafNodes()
和 getNeighborhoodList()
方法应具有指定的返回类型 Node
, , 不是 NodeImpl
, ,否则他们也可能会失败。
不隶属于 StackOverflow