JavaRMI何时创建存根、启动注册表并指定代码库?
何时创建存根,启动注册表并指定代码库
我已经创建了一个RMI应用程序。我的简单应用程序可以工作。我有远程接口。类的包在客户端和服务器包的构建路径中。我首先启动服务器应用程序,然后启动客户端应用程序
然而,我看过internet上的其他示例,我看到它们启动注册表、创建存根并指定代码库
以下是我的节目:
“RemoteObjInterface.class”是我的接口,“RemoteObjImplementation.class”是我的服务器,“客户端.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 楼答案
创建存根是导出远程对象的一个副作用,如果它扩展了
UnicastRemoteObject
,这反过来又是构造它的一个副作用当你想开始的时候。例如,在开始调用
bind()
或rebind()
之前您根本不需要使用此功能,它是可选的。如果希望客户端能够动态下载类而不是提前将它们分发到客户端,请在导出任何远程对象(包括注册表)之前在服务器JVM中指定
java.rmi.server.codebase
系统属性,并确保它指向注册表和客户端都可访问的URL