スタブを作成するときは、レジストリを起動してコードベースを指定しますか?
RMIアプリケーションを作成しました。私の簡単なアプリケーションは動作します。クライアントパッケージとサーバーパッケージのビルドパスにRemoteObjInterface.classのパッケージがあります。最初にサーバーアプリケーションを起動し、次にクライアントアプリケーションを起動します。
しかし、私はインターネットの他の例を見てきましたが、それらがレジストリを開始し、スタブを作成し、コードベースを指定しているのがわかります。
以下は私のプログラムです:
「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();
}
}