用Java实现RMI系统 - 表中没有这样的对象

时间:2016-05-25 07:08:09

标签: java rmi distributed-computing

我正在尝试使用三对客户端/服务器在Java中实现一个简单的RMI系统。 createDrecord现在只是一种测试方法。

客户代码:

public class ManagerClient { 

public static void main(String[] args) {
    try{
    System.setSecurityManager(new RMISecurityManager());
    ClinicServerInterface mtlServer = (ClinicServerInterface)Naming.lookup("rmi://localhost:2020/mtl");
    ClinicServerInterface lvlServer = (ClinicServerInterface)Naming.lookup("rmi://localhost:2021/lvl");
    ClinicServerInterface ddoServer = (ClinicServerInterface)Naming.lookup("rmi://localhost:2022/ddo");

    mtlServer.createDRecord("asdf", "asfa", "asda", "as", "asd");

    }catch(Exception e){
        e.printStackTrace();
    }
  }
}

服务器代码:

public class ClinicServer implements ClinicServerInterface { 

private int port;
private String location;

public ClinicServer(int port, String location){
    this.port = port;
    this.location = location;
}

public static void main(String[] args){

    int mtlPort = 2020;
    int lvlPort = 2021;
    int ddoPort = 2022;

    try{
        ClinicServer mtlServer = new ClinicServer(mtlPort, "mtl");
        ClinicServer lvlServer = new ClinicServer(lvlPort, "lvl");
        ClinicServer ddoServer = new ClinicServer(ddoPort, "ddo");

        Remote mtlObj = UnicastRemoteObject.exportObject(mtlServer,mtlPort);
        Remote lvlObj = UnicastRemoteObject.exportObject(lvlServer,lvlPort);
        Remote ddoObj = UnicastRemoteObject.exportObject(ddoServer,ddoPort);

        Registry r = LocateRegistry.createRegistry(2020);

        r.bind("mtl", mtlObj);
        r.bind("lvl", lvlObj);
        r.bind("ddo", ddoObj);


        System.out.println("New Server is up and running!");

    }catch(Exception e){

        e.printStackTrace();
    }

}

@Override
public void createDRecord(String firstName, String lastName, String address, String phone, String specialization)
        throws RemoteException {
    System.out.println("Create D Record");

}

服务器运行正常,显示消息,但是当我运行客户端时,我认为这是一个错误:

java.rmi.NoSuchObjectException: no such object in table
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
    at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
    at sun.rmi.server.UnicastRef.invoke(Unknown Source)
    at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
    at java.rmi.Naming.lookup(Unknown Source)
    at assignment1.ManagerClient.main(ManagerClient.java:36)

其中第36行是ManagerClient类中的这一行:

ClinicServerInterface lvlServer = (ClinicServerInterface)Naming.lookup("rmi://localhost:2021/lvl");

我已经在这里阅读了其他类似问题,但似乎无法弄清楚它是如何特别适用于我的代码的。

2 个答案:

答案 0 :(得分:1)

int mtlPort = 2020;
int lvlPort = 2021;
int ddoPort = 2022;

不同的远程对象不需要不同的端口号。您也可以将2020用于所有这些和注册表,实际上如果在导出时指定为零,它们仍将共享注册表端口。

Registry r = LocateRegistry.createRegistry(2020);

您需要将此变量设为静态。否则它可能被垃圾收集,这也可能导致服务器的垃圾收集,这就是导致这个问题的原因。

 System.setSecurityManager(new RMISecurityManager());

除非您使用的是RMI代码库功能,否则客户端中不需要安全管理器,这在您的问题中未提及。

ClinicServerInterface lvlServer = (ClinicServerInterface)Naming.lookup("rmi://localhost:2021/lvl");

这将获得java.rmi.ConnectException,因为端口号不是2020。

答案 1 :(得分:-1)

我在Naming.lookup()方法中将所有端口更改为2020,并且执行正常。我认为这种方法将服务器端口号作为参数,而不是注册表端口号。