Tuesday, December 12, 2006

Proxy That Proxy

One day or the other, you will wake up with the bold idea of deploying several EAR files in your JBoss 4.0.x Application Server. Why bold? Because it is almost sure that:

  • you will need to isolate the class loaders of the EAR files (for example to load different versions of the same class in the different applications),

  • you will want EJBs to communicate between the different EAR files.
It will then be the day you will meet the often challenged allways challenging Unified Class Loader (a disciple of Dolph Lundgren) and its scoped deployment configuration options, get a few ClassCastException and finally opt for switching all your EJB calls to the "by value" semantics. Then you will start drinking to forget about not only the amount of pain involved in the process, but also the loss of performance induced by passing all parameters by value.

If your EJBs only take and return classes from the JDK, rejoice! There is hope for you! Indeed, you can remove this problematic casting:

HomeInterface hi = (HomeInterface) lookedUpHome;

which generates exceptions because the looked-up interface has been loaded by a different class loader than the one you cast to, by using a dynamic proxy that directly implements the business interface of your remote EJB.

To simplify this, you can leverage Spring Framework's SimpleRemoteStatelessSessionProxyFactoryBean, which will lookup your EJB, create it for you and return a dynamic proxy that implements the business methods. By this mean, the need for casting is removed.

Of course, as I said before, this only works if you exchange primitives or objects from the JDK (because they are loaded from a common hierachy of class loaders that includes the system class loader). But it is a viable alternative to keep in mind...

3 comments:

J-F Declercq said...

Now that I'm in Sales and far away from Java class loaders, I'm reading your blog and I think to myself : "What an activity in David's brain"...

It must be very tiring.

David Dossot said...

Fortunately, there are some times my brain goes garbage collecting so I can have some rest ;-)

But I have the feeling that Sales could also be pretty tiring...

Olivier F said...

How can that guy anticipate all the problems I can encounter ?