RMI stub loading problems within Tomcat environment
843793Sep 18 2001 — edited Aug 29 2002Hi Java folks,
I encountered something weird in my client-server-application.
First I tried a simple client which has an RMI connection to an RMI server. Client and server reside on different machines. This works fine - the client calls Naming.lookup (<RMI-server-url + service name>) and casts that thing on the common interface used by client and server. Afterwards all methods of the remote objects are available. Fine.
Now I put my (RMI)-client into a servlet. The code for the RMI-connection is similar. The servlet is deployed in Tomcat 3.2.2. Naming.lookup() again returns "some" object. If I use it's toString()-method I can see that the class type is the expected RMI-stub from the server. Now something really odd happens: If I try to cast that stub to my interface I get a ClassCastException!
I have written a main-method to test the servlet outside tomcat. This just calls init() like the servlet enginge would do, and init tries to establish the RMI connection as described above. So if I use that main-method and call init() directly, I can easily cast the stub to my interface. If I use the servlet engine which in turn calls init(), I suddenly cannot cast it to my interface anymore! What the hell happens there?
I can avoid that problem if I put the stub-class-file in a directory in the tomcat servlet directory so that it's inside the classpath. Then the cast will work. This solves my problem, but it's not quite clever. I think the standard way would be that an RMI client (in this case a servlet) just knows about the interface and loads the stub dynamically from the (RMI-)server. The server stub could vary this way and it would still work as the interface stays the same.
Anybody any idea about that?
Regards,
Stefan