1

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!

3 Answers 3

2

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.

1
  • Hey Michael, thanks for the response. But I'm only trying to understand the concepts. The way I specified it - how would you go about doing it, so that each time something is changed it's transmitted to the server, albeit being very inefficient?
    – partel
    Jul 11, 2010 at 12:07
2
java.lang.ClassCastException: MyList_Stub cannot be cast to java.util.LinkedList
at $Proxy0.createList(Unknown Source)
at RemoteProgram.main(RemoteProgram.java:27)

LinkedList is a concrete class, RMI works with interfaces so you should be casting to the List interface on the client side.

1
  • ...as for the connection refused exception, I'd start by looking into the security policy you are using (assuming no external issues such as firewalls).
    – darri
    Jul 11, 2010 at 11:47
0

Agree with the other posters, this is a very inefficient design.

As for your exceptions, I don't see know how you can get a 'connection refused' in the server and still be able to run the client. You would need to post the stack trace.

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.