Grails and EJB 3 Session Beans (Dave Klein)
Dave Klein, EJB, Grails July 16th, 2008
Following on yesterday’s Mastering Grails: Grails and Legacy Databases by Scott Davis, Dave Klein writes about his latest experience with EJB2/EJB3 and Grails. I turns out that Dave had previously worked with EJB2 and Grails and everything was peachy, so to speak (given that EJB2 was involved). Switching to an EJB3 based project and Grails afterwards shouldn’t be so difficult right? well there was a vendor related gotcha involved. In Dave’s words:
For starters the Spring class that we used to connect to an EJB2 session bean, SimpleRemoteStatelessSessionProxyFactoryBean (I wonder if there is a TinyURL! for class names) [Editor: no such thing yet bu you can at least create aliases on imports with Groovy] was not working with EJB3. Several people on the forums said that it should, but our application did not believe them. It turns out that there is a Spring class that does work with EJB3 and it even has a shorter name! The JndiObjectFactoryBean is a general purpose Factory for looking up JNDI objects and making them available to other beans. The JndiObjectFactoryBean will even use the JndiTemplate that I had setup before, though I’ll show it again so you don’t have to hunt it down.
So now we should be in business. Except that it didn’t work. When using the SimpleRemoteStatelessSessionProxyFactoryBean we could just use the Session Bean’s simple interface name, which was also its JNDI name. That was not working with the JndiObjectFactoryBean. This critter wanted a fully qualified, certified and USDA approved JNDI name. The bummer here is that this is vendor specific. So the rest of this will work for BEAORACLE WebLogic but it might be completely different for your app server.
First we have to make sure that the EJB3 session bean has the mappedName attribute in the @Stateless annotation. (In our case …mappedName=”CoverageSession”) That makes up the first part of the JNDI name. Then we add a # symbol and top it off with the fully qualified name of the Remote Interface. So we ended up with something like this for our JNDI name: CoverageSession#org.blah.app.ejb.session.ICoverageSessionRemote Once we gave it that mouthful it was happy.
So there you have it. Grails is EJB friendly, be it by allowing JPA annotated domain classes or Session Beans as services. It may be true that EJB/JPA integration in Grails is without some corner cases, just remember what is proposed in the roadmap for version 1.1.
Tags: Dave Klein, EJB, Grails
About