Question

I am reading about networking and java from this book.

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) .

I made some changes, mostly where he uses the old Vector I replaced it with 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. 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. 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.

Thanks for any info on this!

Was it helpful?

Solution

That's because the ArrayList is serializable and not remote. Your client downloaded a copy of it and did something to an element of it which was also in your JVM. The other client did the same. Nobody did anything to the element in the server JVM, so it didn't change. You would need to add a remote method for that into your interface.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top