简体   繁体   English

RMI Java应用程序无法按预期运行

[英]RMI Java application not working as expected

I am reading about networking and java from this book. 我读关于物联网和Java 本书。

The problem is that I am trying to do an example listed on this book, it is on page 153 till page 159 (paragraph 5.4 Using RMI Meaningfully ) . 问题是我试图做一个本书中列出的示例,它在第153页到第159页之间(第5.4节“有意义地使用RMI” )。

I made some changes, mostly where he uses the old Vector I replaced it with ArrayList. 我做了一些更改,主要是在他使用旧Vector的地方,我将其替换为ArrayList。 Also on Client side I made some minor changes like this: 同样在客户端,我做了一些小的更改,如下所示:

public static void main(String[] args) {
    try {
        // Obtain a reference to the object from the
        // registry and typecast it into the appropriate
        // type...

        String name = "//localhost/Service1";
        Stp temp = (Stp) Naming.lookup(name);

        ArrayList<Account> acc = temp.getAccs();

        System.out.println(acc);

        for (Account cnt : acc) {
            if ("george".equals(cnt.name)) {
                cnt.get(10);
                System.out.println(cnt);
            }
        }

    } catch (ConnectException conEx) {
        System.out.println("Unable to connect to server!");
        System.exit(1);
    } catch (Exception ex) {
        ex.printStackTrace();
        System.exit(1);
    }
}

It works just fine, or almost fine. 它工作正常,或几乎正常。 When the get(int number) method is invoked from client (it is supposed to decrease the balance) ,it actually does what it is meant to do. 当从客户端调用get(int number)方法时(应该减少余额),它实际上会执行应做的事情。 The problem is that if I open another client from another cmd window or even if I run the same client again it doesn't further decrease the value of balance. 问题是,如果我从另一个cmd窗口中打开另一个客户端,或者即使我再次运行相同的客户端,它也不会进一步降低余额的值。 If the balance is 100 and client runs the get(10) he will see 90. But if the client code runs again for another one get(10) , then, the balance doesn't updates and remains again 90. It should be 80. 如果余额为100,并且客户端运行get(10)他将看到90。但是,如果客户端代码再次针对另一个get(10) ,则余额不会更新,并再次保持90。应为80 。

Thanks for any info on this! 感谢您提供任何信息!

That's because the ArrayList is serializable and not remote. 这是因为ArrayList是可序列化的,而不是远程的。 Your client downloaded a copy of it and did something to an element of it which was also in your JVM. 您的客户端下载了它的副本,并对它的元素也做了一些操作,该元素也在您的JVM中。 The other client did the same. 另一个客户也这样做。 Nobody did anything to the element in the server JVM, so it didn't change. 没有人对服务器JVM中的元素做任何事情,因此它没有改变。 You would need to add a remote method for that into your interface. 您需要为此添加一个远程方法到您的界面中。

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

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