Just a finding I'd like to share about RMI and the local/remote interfaces problem.
I was thinking of a way where I could write a single local impl, and my remote impl would basically call the local impl to perform all the tasks (code reuse basically).
Based on the follow snippets:
When I run my server and client, I will always get an exception that says "$Proxy0 cannot be cast to xxx.Service".
After some trial and error, I made the following changes
Everything else works fine... and when I call getStatus() I will get "from xx.ServiceRemoteImpl" which is exactly what I want
My question is, why wouldn't my first approach work? Since ServiceRemoteImpl implements Remote and extends ServiceImpl (which implements Service) and therefore should be just what I need. Any RMI gurus out there that can provide a good answer?
As far as I'm concerned, the 2nd approach doesn't seems to break any SUN's rules and I'd prob use it for my assignment... however, it's all about learning and I'd like to find out what went wrong!
Thanks for viewing...
Welcome to the JavaRanch!
Concerning your question: if you want to use your Service as a remote interface, it is a requirement to extend from java.rmi.Remote (more info about what you must do with regard to RMI can be found here).
Roel De Nijs wrote:Concerning your question: if you want to use your Service as a remote interface, it is a requirement to extend from java.rmi.Remote (more info about what you must do with regard to RMI can be found here).
"A remote interface must at least extend, either directly or indirectly, the interface java.rmi.Remote."
In my original impl,
ServiceRemoteImpl extends ServiceImpl and implements Remote.
Hence, it indirectly implements the interface java.rmi.Remote.
That's why I didn't understand why it didn't work.
According to the link I provided:
In RMI, a remote interface is an interface that declares a set of methods that may be invoked from a remote Java virtual machine.
So a remote interface is an interface and that interface must extend from java.rmi.Remote . In your example the remote interface implementation is implementing the java.rmi.Remote interface, but your remote interface isn't. And that's why it doesn't work: you violated one of the constraints.
I also experimented a bit with RMI, because I was unfamiliar with it. This thread can be found here.