java中的非法远程方法 [英] Illegal remote method in java

查看:49
本文介绍了java中的非法远程方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用 java Rmi*.我有一个自定义类,它扩展了 UnicastRemoteObject 并实现了一个扩展远程的接口.我认为我已经在类中正确地实现了接口的方法,但是当我尝试运行我的代码时我仍然得到一个 IllegalArgumentException(它是关于一个没有参数的方法).

It's the first time I use java Rmi*. I have a custom class which extends UnicastRemoteObject and implements an interface which extends remote. I think that I have implemented the methods of the interface correctly in the class but still I get an IllegalArgumentException when I try to run my code (and it's about a method which has no arguments).

jvm 声称遇到了一个非法的远程方法,但该方法及其实现对我来说似乎很好.

The jvm claims to have encountered an illegal remote method but the method and its implementation seem fine to me.

除了错误地实现或调用方法之外,是否还有其他原因可能导致此异常?

Is there any other reason as a result of which this exception might occur except for implementing or calling the method wrongly?

这是堆栈跟踪:

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 接口上的所有方法都必须在它们的 throws<中声明 RemoteException/code> 子句,例如:

All of the methods on a RMI Remote interface must declare RemoteException in their throws clause, e.g.:

public String getId() throws RemoteException;

不清楚为什么异常命名为 getId(),它可能只是它检查的第一个方法.

It's not clear why the exception names getId() specifically, it's probably just the first method it checked.

此外,getLeafNodes()getNeighborhoodList() 方法应该具有指定 Node 而不是 NodeImpl 的返回类型代码>,否则他们也可能会失败.

Also, the getLeafNodes() and getNeighborhoodList() methods should have return types that specify Node, not NodeImpl, otherwise they will likely fail also.

这篇关于java中的非法远程方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆