Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RMI ClassCastException? Look here...

 
Dave Teare
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have seen a lot of people having trouble with class cast problems within their RMI client. I have had this problem several times, and have finally found out why it is happening.
In the client, the Stub returned from the Naming.lookup() must be cast to the interface of the stub, NOT the implementing class of the Stub.
For example, consider the following connection manager of mine:

In my client, I retreive the Stub as follows: Object tmp = Naming.lookup(rmiName);
Now, if I cast tmp to FBNConnectionManagerImpl, things blow up. If I cast it to FBNConnectionManager, everything is fine.
While this might be obvious to those RMI experts out there, as a newbie I thought having an interface that simply implemented Remote seemed unnecessary, so I just had my connection manager implement it directly, and skipped the interface. BIG MISTAKE!

Hope this helps!
--Dave.
 
Andrea Gazzarini
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dave...RMI means Remote Method Invocation...Casting the return of the lookup to the XXXRemoteImpl means that you have the XXXRemoteImpl.class on your local machine!
Greetings
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Dave,
Thanks for your post, it will be very helpful to many of us.
Now in your solution you are talking about the object that gets bound to the Registry. How about the Connection Object which isn't in the Registry? Will you solution help those classes too?
Mark
 
Dave Teare
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andrea,
On my test machine the 'remote' implementation IS on my local machine!!
That is why I got confused
--Dave.
 
Dave Teare
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a good question Mark. Again, I simply got it working, I cannot explain WHY it works! From what little I know, and given the previous post, I think you must have interfaces defined for all objects that are returned, and these interfaces must be available to the client.
My understanding is that objects returned from the stub class need only implement Remote and Serializable, for example:

My code can now happily get a connection to the FBN database by simply calling the getInstance() method on the FBNConnectionManager interface.
Sorry for my scattered thoughts; I am just at the dawn of figuring all this RMI stuff out. Once I have everything ironed out, I will try to post a complete overview of my findings.
--Dave.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serializable
Actually If you implement Remote then extend UnicastRemoteObject, you do not need to implement Serializable.
In the case of a straight Remote object you do no want to implement Serializable as the object remains on the server and only the stub is sent to the client.
The only objects that should implement Serializable are those that you are directly sending to the client and resides on the client.
Mark
 
Dave Teare
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,
I just tried what you suggested, and yes, you are right - I do not need the Serializable.
Thanks!
BTW - Are you still having your class cast exception?
--Dave.
 
Reinhold Gruber
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
Maybe this clarifys some things. If you type

you can investigate the Source of the stub-class
FBNConnectionManagerImpl_Stub.java. This class
and all other stubs extends java.rmi.server.RemoteStub and implements all remote interfaces from FBNConnectionManagerImpl.
Therefore you can't cast the stub to the implementing class, because they have nothing in common besides the remote interfaces.
Reinhold Gruber
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dave Teare:

BTW - Are you still having your class cast exception?
--Dave.

Of course I am.
Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic