0

I created a simple example to experiment with the RMI feature of Java. It's quite nice. But when I call a remote method which returns a LinkedList object and I add an element to the list: nothing happens - the element isn't added. See my code bellow:

The Interface and implementation on the server (remote object):

public interface FooBar extends Remote {
    List<Object> getList() throws RemoteException;
}

public class FooBarImpl extends UnicastRemoteObject implements FooBar {

    private static final long serialVersionUID = -200889592677165250L;
    private List<Object> list = new LinkedList<Object>();

    protected CompanyImpl() throws RemoteException { }

    public List<Object> getList() { return list; }

}

Code to bind it (server):

Naming.rebind("//" + hostname + "/foobar", new FooBarImpl());

Client code:

FooBar foo = (FooBar) Naming.lookup("//" + hostname + "/foobar");
foo.getList().add(new String("Bar"));

System.out.println(foo.getList().size());

The output will be 0 instead of 1. So my simple question is: how to fix it without using an add method (because with an add method on the server side it works)?

EDIT 1: This code works pretty well:

public class FooBarTest {

    static class FooBarImpl {
        public List<Object> list = new LinkedList<Object>();
        public List<Object> getList() { return list; };
    }

    public static void main(String[] args) {
        FooBarImpl test = new FooBarImpl();

        test.getList().add(new String("Foo"));
        System.out.println(test.getList().size()); // = 1
    }

}

EDIT 2: This code works also (but I'm trying to replecate the simple code from EDIT 1):

@Override
public void add(Object o) throws RemoteException {
    list.add(o);
}

FooBar foo = (FooBar) Naming.lookup("//" + hostname + "/foobar");
foo.add(new String("Bar"));

System.out.println(foo.getList().size()); // == 1
3
  • Are you adding anything in list at server side? If yes then show your code
    – Mac
    Jul 3, 2013 at 18:57
  • No. My scenario is as simple as described on the top.
    – mythbu
    Jul 3, 2013 at 19:03
  • Why should the list be empty? On object initialization the new LinkedList is created and only returned. As you can see in EDIT 1 it works for a simple scenario.
    – mythbu
    Jul 3, 2013 at 19:35

1 Answer 1

0

The output will be 0 instead of 1

This is so because , You are adding the element Bar to the anonymous List object obtained via foo.getList() but you are printing the size of the fresh List object obtained again via foo.getList() which is empty within following line:

System.out.println(foo.getList().size());

You should use following code:

List<Object> list = (List<Object>)foo.getList();
list.add(new String("Bar"));

System.out.println(list.size());
6
  • Am i missing something? i think he is doing the same thing.
    – ajay.patel
    Jul 3, 2013 at 19:07
  • @zerocool Watch my post carefully I have obtained the List object from foo.getList() and added Bar to that list and then printing its size .. it is in contrary to OP code..He is adding Bar in an anonymous List object .. ;) and printing the size of fresh List object obtained again via foo.getList()
    – Mac
    Jul 3, 2013 at 19:09
  • Yes this works, but I want to change the list object on the server and not my local object.
    – mythbu
    Jul 3, 2013 at 19:29
  • @mythbu : then in that case , you send the filled up list object from client to back to server and create a method on server where you will receive the list object from client.
    – Mac
    Jul 3, 2013 at 19:31
  • @Mac: that's an idea, but I'm tring to replicate the simple example (EDIT 1) for now.
    – mythbu
    Jul 3, 2013 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.