简体   繁体   English

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

[英]Implementing an RMI system in Java - no such object in table

I am trying to implement a simple RMI system in Java using three pairs of client/servers.我正在尝试使用三对客户端/服务器在 Java 中实现一个简单的 RMI 系统。 The createDrecord is just a test method for now. createDrecord 目前只是一个测试方法。

Client Code:客户代码:

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();
    }
  }
}

Server Code:服务器代码:

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");

}

The server runs fine, with the message being displayed, but when I run the client, I get this as an error:服务器运行良好,并显示消息,但是当我运行客户端时,出现以下错误:

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)

where line 36 is this line in the ManagerClient class:其中第 36 行是 ManagerClient 类中的这一行:

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

I've read other threads on here with a similar problem but can't seem to figure out how it applies to my code specifically.我在这里阅读了其他有类似问题的线程,但似乎无法弄清楚它是如何专门应用于我的代码的。

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

You don't need different port numbers for different remote objects.不同的远程对象不需要不同的端口号。 You can use 2020 for all of them and the Registry too.您可以将 2020 用于所有这些以及注册表。

Registry r = LocateRegistry.createRegistry(2020);

You need to make this variable static.您需要将此变量设为静态。 Otherwise it can be garbage-collected, which can lead to garbage-collection of the servers too, which is what causes this problem.否则它可能会被垃圾收集,这也可能导致服务器的垃圾收集,这就是导致此问题的原因。

 System.setSecurityManager(new RMISecurityManager());

You don't need a security manager in the client unless you're using the RMI codebase feature, which isn't mentioned in your question.除非您使用 RMI 代码库功能,否则您不需要客户端中的安全管理器,这在您的问题中没有提到。

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

This will get a java.rmi.ConnectException , as the port number isn't 2020.这将得到一个java.rmi.ConnectException ,因为端口号不是 2020。

I changed all the ports to 2020 in the Naming.lookup() method and it executes fine.我在 Naming.lookup() 方法中将所有端口更改为 2020 并且它执行正常。 I thought this method took the server port number as an argument, not the Registry port number.我认为此方法将服务器端口号作为参数,而不是注册表端口号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM