Grails and EJB2.1 integration


If you are working on a Grails project and you want to use existing business logic from EJB2.1 inside your application, you can easily achive this using Spring.

The first step is to include the Beans you want to use, inside your conf/spring/resources.groovy file like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
beans = {
               ejbJndi(org.springframework.jndi.JndiTemplate){
                       environment = [
                               "java.naming.factory.initial" : "org.jnp.interfaces.NamingContextFactory",
                               "java.naming.provider.url" : "jnp://localhost:1099"]
               }

               businessBean(org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean){
                       jndiName = "EJBBusinessBean"
                       businessInterface = "com.my.ejb.EJBBusinessBean"
                       jndiTemplate = ref("ejbJndi")
               }

               personBean(org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean){
                       jndiName = "EJBPerson"
                       businessInterface = "com.my.ejb.EJBPerson"
                       jndiTemplate = ref("ejbJndi")
               }
}

Then you can use any of the defined Beans inside your services or controllers like this:

1
2
3
4
5
6
7
class MyService {
   def personBean //spring injects the bean automatically

   def myMethod(){
     personBean.list()
  }
}

That’s it!
Just remember to include the required JAR files (the EJB jars and the jars required to connect to the container, for instance, JBoss)

, , , , ,

  1. #1 by Martin on November 5th, 2009

    Hi,

    Thanks for this - detailed examples of Grails + EJB2.x are few and far between.

    Unfortunately when I tried this to connect a Grails service to an EJB I receive the following exception:

    org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.NullPointerException: Cannot invoke method list() on null object

    Have you come across this problem while accessing EJBs? (I know, it’s a very generic exception, but thought it was worth asking.)

(will not be published)
  1. No trackbacks yet.