Rmi getDate()程序在客户端

时间:2015-06-01 18:30:19

标签: java eclipse rmi

我正在尝试第一次编写RMI代码。该程序执行一个非常简单的从服务器获取日期的功能。我设法让服务器运行,但在客户端出现异常:

Exception in thread "main" java.rmi.NotBoundException: //localhost/RemoteDatum
    at sun.rmi.registry.RegistryImpl.lookup(RegistryImpl.java:166)
    at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
    at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:410)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:268)
    at sun.rmi.transport.Transport$1.run(Transport.java:200)
    at sun.rmi.transport.Transport$1.run(Transport.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$254(TCPTransport.java:683)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$$Lambda$1/1287360293.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:276)
    at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:253)
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:379)
    at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
    at MyClient.main(MyClient.java:15)

我的界面" RemoteDatum"看起来像这样:(我将服务器中的接口复制到客户端Project文件夹,并且存根是由RMI eclipse插件生成的)

    import java.rmi.Remote;
    import java.rmi.RemoteException;
    import java.util.Date;


    public interface RemoteDatum extends Remote{
        public Date holeDatum() throws RemoteException; //holeDatum means getDate in german
    }

RemoteDatumImpl:接口的实现

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

        public class RemoteDatumImpl extends UnicastRemoteObject implements RemoteDatum {


         protected RemoteDatumImpl() throws RemoteException {
                super();
            }


        public Date holeDatum() throws RemoteException
            {
                return new Date();
            }
        }

服务器:

    import java.net.MalformedURLException;
    import java.rmi.Naming;
    import java.rmi.Remote;
    import java.rmi.RemoteException;
    import java.rmi.registry.LocateRegistry;
    import java.rmi.registry.Registry;
    import java.rmi.server.UnicastRemoteObject;


    public class MyServer extends UnicastRemoteObject {

        protected MyServer() throws RemoteException {
            super();
        }


        public static void main(String[] args) throws RemoteException, MalformedURLException
        {
            try
            {
                String name = "RemoteDatum";
                Registry r = LocateRegistry.getRegistry();
                MyServer service = new MyServer(); 
                r.rebind(name, service);    
            }catch(Exception ex){ex.printStackTrace();}
        }
    }

我的客户:

    import java.net.MalformedURLException;
    import java.rmi.NotBoundException;
    import java.rmi.RemoteException;
    import java.rmi.registry.LocateRegistry;
    import java.rmi.registry.Registry;


    public class MyClient {


        public static void main(String[] args) throws RemoteException, MalformedURLException, NotBoundException 
        {       
                Registry r = LocateRegistry.getRegistry();
                String name = "//localhost/RemoteDatum";
                RemoteDatum service = (RemoteDatum) r.lookup(name);
                System.out.println(service.holeDatum());

        }

    }

我正在使用Eclipse RMI插件启动RMI注册表,并且不知道这是否相关。我正在使用Mac。

1 个答案:

答案 0 :(得分:0)

首先:

Registry r = LocateRegistry.getRegistry();

除非客户端与服务器在同一主机上运行,​​否则您需要获取服务器Registry,而不是您自己的服务器:

Registry r = LocateRegistry.getRegistry("serverhost);

然后:

String name = "//localhost/RemoteDatum";
RemoteDatum service = (RemoteDatum) r.lookup(name);

如果您使用的是Registry接口,则不应在查找字符串中包含主机名。它应该是"RemoteDatum"

或者,如果您使用Naming类,则包含主机名:

RemoteDatum service = (RemoteDatum)Naming.lookup("//serverhost/RemoteDatum");