Java RMI - 何时创建存根,启动注册表并指定Codebase?

时间:2013-03-19 23:11:07

标签: java rmi

何时创建Stub,启动注册表并指定Codebase?

我已经创建了一个RMI应用程序。我的简单应用程序。我在构建路径中为Client和Server包安装了RemoteObjInterface.class包。我首先启动Server应用程序,然后启动Client应用程序。

但是,我已经查看了互联网上的其他示例,我看到他们启动注册表,创建存根并指定代码库。

以下是我的计划:

RemoteObjInterface.class ”是我的界面,“ RemoteObjImplementation.class ”是我的服务器,“ Client.class ”是我的客户端。

public interface RemoteObjInterface extends Remote {
    public String someMethod() throws RemoteException;
        }

public class RemoteObjImplementation extends UnicastRemoteObject implements
    RemoteObjInterface {
   private static final long serialVersionUID = 1L;
   private static final int PORT = 1099;
   private static Registry registry;

public RemoteObjImplementation() throws RemoteException {
    super();
    }

@Override
public String someMethod() throws RemoteException {

    return new String("Hello");
    }

public static void main(String args[]) {

    Registry registry = LocateRegistry.createRegistry(PORT);

    registry.bind(RemoteObjInterface.class.getSimpleName(),
            new RemoteObjImplementation());

     }
}

public class Client {
    private static final String HOST = "localhost";
    private static final int PORT = 1099;
    private static Registry registry;

public static void main(String[] args) throws Exception {

registry = LocateRegistry.getRegistry(HOST, PORT);

 RemoteObjInterface remoteApi = (RemoteObjInterface) registry.lookup(RemoteObjInterface.class.getSimpleName());

    System.out.println("Message = " +
            remoteApi.someMethod();

    }
}

1 个答案:

答案 0 :(得分:1)

  

何时创建存根

创建存根是导出远程对象的副作用,如果它扩展UnicastRemoteObject,这又是构建它的副作用。

  

启动注册表

当你想要启动它时。在您开始致电bind()rebind()之前。

  

并指定Codebase?

您根本不需要使用此功能,它是可选的。如果您希望客户端能够动态下载类而不是提前将它们分发到客户端,请在导出任何远程对象(包括注册表)之前在服务器JVM中指定java.rmi.server.codebase系统属性,并确保它指向注册表和客户端都可以访问的URL。