RMI RemoteException

时间:2010-04-30 23:43:32

标签: java rmi

在尝试从Windows调用Unix机器上的方法时,为什么会出现RemoteException? 我在网络内部并且不认为这是因为防火墙问题,因为我可以在unix框启动RMI服务器后从Windows“unnet”执行“telnet”。我也无法理解为什么要使用本地环回IP?

堆栈追踪:

RemoteException occured, details java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is: 
    java.net.ConnectException: Connection refused: connect
java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is: 
    java.net.ConnectException: Connection refused: connect

非常感谢提前。

2 个答案:

答案 0 :(得分:12)

您可能没有在Linux机器上正确配置主机名。我敢打赌,如果你从Linux框中ping $(hostname),它会ping 127.0.0.1。通常这是因为/etc/hosts文件中的条目。

有几种方法可以解决这个问题。困难的方法是让您的Linux机箱正确地将其自己的主机名解析为其IP地址。您可以编辑/etc/hosts文件,设置DNS服务器,无论您需要做什么。挑战在于,尽管这可能会使技术更加正确,但您仍有可能破坏依赖旧行为的事物。

最少更改的路径是set the system property java.rmi.server.hostname到Linux机器的主机名或IP地址。 (即java -Djava.rmi.server.hostname=$(hostname) ...)。

为什么?

Java RMI注册服务器实际上是一个网络注册服务器。其他计算机上的对象可以将自己绑定到此注册表。

注册远程对象时,注册包括网络地址作为注册的一部分。默认情况下,它使用的地址是“本地主机的IP地址”,采用“dotted-quad”格式。在您的设置中,此地址为127.0.0.1

当您的Windows框与注册服务联系以获取远程对象的地址时,它将返回127.0.0.1。然后它尝试联系该地址的远程对象。这就是为什么它会转到环回地址。

答案 1 :(得分:0)

我建议使用基于自定义RMISocketFactory的解决方案。

如在Sun网站上解释的那样,您可以提供自己的SocketFactory: http://docs.oracle.com/javase/7/docs/technotes/guides/rmi/socketfactory/

我的解决方案使用此机制来拦截客户端套接字创建,并用客户熟知的好IP替换收到的主机(127.0.0.1)。

其余的通信机制仍然基于java rmi标准。

通过这种实现,导出器不必知道它自己的IP,这有时并不容易(多个网络接口......)

以下是树类,工厂,服务器和客户端。 Hello类和接口也上传为详尽无遗。

希望它应该是有用的

的SocketFactory:

import java.io.IOException;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;
import java.rmi.server.RMISocketFactory;

/**
 * Socket Factory for RMI calls.
 * 
 * This classe, instanciated from server when RMI objects are exported, is send
 * to the client who use it (transparently) for create sockets which call remote objects.
 * 
 * This implementation give the ability to modify dynamically the target host cible.
 * 
 * The host will not be aware of it's own IP.
 */
public class MySocketFactory extends RMISocketFactory implements Serializable {

    /**Target host for RMI calls, setted by caller. */
    private static String server = "localhost";

    /**
     * Create a client socket, replacing required host by the host setted when the service is called,
     * via {@link #setServer(String)}.
     * The host received is usually 127.0.0.1, depending on property java.rmi.server.hostname on the exporter.
     */
    @Override
    public Socket createSocket(String host, int port) throws IOException {
        System.out.println("change host from " + host + " to " + server);
        return getFactory().createSocket(server, port);
    }

    /**
     * Create a server socket.
     */
    @Override
    public ServerSocket createServerSocket(int port) throws IOException {
        return getFactory().createServerSocket(port);
    }

    /**
     * Use default RMI factory.
     */
    private RMISocketFactory getFactory() {
        return RMISocketFactory.getDefaultSocketFactory();
    }

    /**
     * Save the target host. This method must be called before use of a service (before Naming.lookup).
     */
    public static void setServer(String host) {
        server = host;
    }

}

出口商:

import java.io.IOException;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.RMISocketFactory;
import java.rmi.server.UnicastRemoteObject;

/**
 * RmiExport
 */
public class MyRmiExporter {

    /**
     * java -Djava.security.policy=java.policy MyRmiExporter
     */
    public static void main(String[] args) throws RemoteException, IOException {
        System.setSecurityManager(new RMISecurityManager());

        Hello export = new HelloImpl();
        RMISocketFactory sf = new MySocketFactory();

        UnicastRemoteObject.unexportObject(export, true);
        Remote stub = UnicastRemoteObject.exportObject(export, 0, sf, sf);
        String url = "rmi://0.0.0.0:" + Registry.REGISTRY_PORT + "/Hello";

        LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
        Naming.rebind(url, stub);

        System.out.println("Exported " + url);
    }

}

客户:

import java.io.IOException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.registry.Registry;

public class MyClient {

    /**
     * java MyClient localhost
     */
    public static void main(String[] args) throws IOException, NotBoundException, InterruptedException {
        String host = args[0];
        MySocketFactory.setServer(host);
        String url = "rmi://" + host + ":" + Registry.REGISTRY_PORT + "/Hello";;
        System.out.println("look up " + url);
        Hello proxy = (Hello) Naming.lookup(url);
        System.out.println("OK, remote getted !");
        System.out.println(proxy.hello("bonjour"));
    }
}

Bean:

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

public interface Hello extends Remote, Serializable {

    String hello(String mess) throws RemoteException;

}

Impl:

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class HelloImpl extends UnicastRemoteObject implements Hello {

    public HelloImpl() throws RemoteException {
    }

    @Override
    public String hello(String mess) throws RemoteException {
        return "hello : " + mess;
    }

}

最后和最不重要的,java.policy:

grant {
    permission java.security.AllPermission;
};