为什么Java RMI服务器在我尝试运行它时会显示错误消息?

时间:2017-04-07 09:23:45

标签: java rmi

执行程序后抛出以下异常。

  

未解决的编译问题:方法bind(String,Remote)in   注册表类型不适用于参数(String,   EmployeeRMIMain)

    public static void main(String args[]){
        try {
            EmployeeService obj = new EmployeeService();
            Registry r = LocateRegistry.createRegistry(1234);
            r.bind("Remote", obj);
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (AlreadyBoundException e) {
            e.printStackTrace();
        }
    }

2 个答案:

答案 0 :(得分:0)

编辑:用户已经解决了自己的问题。 @KMuir即使您找到了解决方案也是一种很好的做法。

EmployeeService类的类接口是什么?你确定它实现了远程标记接口吗?

public interface TunnelingMessageBox extends Remote {
  public void pushMessage(Message message) throws RemoteException;
  //..more interface methods
}

public class TunnelingMessageBoxImpl implements TunnelingMessageBox {
  public void pushMessage(Message message) throws RemoteException {
     // does the magic
  }
}

public class MyService {
  private Registry registry;
  private int port;
  public void createRegistry(int port) throws RemoteException {
        Registry reg;
        try {
            reg = LocateRegistry.createRegistry(port);
        } catch (ExportException ex) {
            // get existing registry instance.
            reg = LocateRegistry.getRegistry(port);
        }
        this.port=port;
        registry = reg;
  }

  public vooid closeRegistry() {
    try {
      Remote obj = (Remote)registry.lookup("box1");
      UnicastRemoteObject.unexportObject(obj, true);
      registry.unbind("box1");
    } catch(Exception ex) { ex.printStacktrace(); }
    registry=null;
  }

  public void registerServices() throws RemoteException {
    TunnelingMessageBoxImpl mbox = new TunnelingMessageBoxImpl();
    UnicastRemoteObject.exportObject(mbox, port);
    registry.rebind("box1", mbox);
  }

}

答案 1 :(得分:0)

我已对最初发布的代码进行了更正。问题是我正在创建一个错误类的对象。

我正在创建此类的对象(EmployeeRMIMain),而不是上面代码中更新的EmployeeService类。发生此错误是因为EmployeeRMIMain方法不从以下类UnicastRemoteObject继承。