I successfully created an RMI service and client. I can call methods and so on. But now I wanted to try the following: I wanted a standard Java object like a LinkedList to be hosted on the service. Also, I wanted to 'pretend' I already had existing code that uses a LinkedList. What I want is to get a LinkedList that is actually managed by the service, but that I can access locally just like it was a normal LinkedList. On top of that I want to do some minimal logging, like if .add() is called it writes on the server: "Add called".
This is not meant for production, just to help me understand how it works!
So far I've tried a lot of things. The most promising is that I have created a class that extends LinkedList and implements Remote. This class tries to register itself with the Registry in the constructor like this:
try {
UnicastRemoteObject.exportObject((Remote)this); Naming.rebind("theList", (Remote)this); } catch (Exception e) { System.out.println("fail"); System.out.println(e.getMessage()); }
I have to do this because I need to extend LinkedList, thus I cannot extend UnicastRemoteObject.
The output I get when I try to run this, on the server side:
fail Connection refused to host: 192.168.178.27; nested exception is: java.net.ConnectException: Connection refused
And on the client side:
java.lang.ClassCastException: MyList_Stub cannot be cast to java.util.LinkedList at $Proxy0.createList(Unknown Source) at RemoteProgram.main(RemoteProgram.java:27)
Thanks in advance!
What you are trying to do is very inefficient and not a very good idea. Basically you can send anything in a method invocation that you can serialize. If you want good performance, I would suggest that you only have one remote object that represents your service and acts as a facade for all the services that you need (each remote object results in separate file descriptor, so having lots of remote objects is typically not a good idea). Additionally, if you are frequently adding and removing objects, then sending a message every time you add or remove an element is not really sensible. I would suggest having a single remote object with the following two very simple methods:
LinkedList retrieveLinkedListByName(String);
boolean commitNewVersionOfLinkedListByName(String,LinkedList);
On application startup, you can download the linked list, and then at regular intervals and at application exit, you can send back the linked list. That should be more efficient then using the network every time you add or remove an element to your linked list. As long as the elements of your LinkedList are serializable, you don't need to do any magic (like extending remote) for it be sent.