Integrating Grails with EJB 2 applications - No Fluff Just Stuff

Integrating Grails with EJB 2 applications

Posted by: Dave Klein on March 26, 2008

When attempting to convince an organization to adopt Grails you may have to demonstrate more than the superior developer productivity it provides. Integrating with legacy systems or even new systems that may continue to be developed in other tools will probably become an important topic. One way to integrate is to use the Grails ORM DSL to hook legacy database tables into a Grails application. Another way is to connect to existing EJB based applications. As you would expect Grails makes this so easy it seems like cheating.

In this example we have an EJB 2 server running in WebLogic. There is a CMP entity bean called Employee, a stateless session bean called EmpSession and a data transfer object called vEmployee. The EJB components and the DTO are in a jar called EmpEjb.jar. The session bean contains methods for retrieving and saving Employee entities via the vEmployee DTOs. We will focus on on how to get access to the EmpSession instance from our Grails application.

First we'll add a couple of beans to our grails-app/conf/spring/resources.groovy file using the SpringBuilder DSL.

beans = {
ejbJndi(org.springframework.jndi.JndiTemplate){
environment = [
"java.naming.factory.initial" : "weblogic.jndi.WLInitialContextFactory",
"java.naming.provider.url" : "t3://some.enterprise.server:7001",
"java.naming.security.principal" : "dave",
"java.naming.security.credentials" : "1234"
]
}
empSession(org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean){
jndiName = "EmpSession"
businessInterface = "com.enterprise.some.ejb.session.EmpSession"
jndiTemplate = ref("ejbJndi")
}
}

Let's look at what this code is doing. We have what looks like a method: ejbJndi. It is really a Spring bean id and its argument is the bean class. The block that follows includes one or more property assignments in the form of properyName = propertyValue. for the ejbJndi bean we only have one property, environment, which in turn contains several entries. The entries take the form of a map of propKey:propBody pairs. This sets up our JNDI Template which Spring will use to lookup our session bean.

Next we declare our remote session bean using Spring's SimpleRemoteStatelessSessionProxyFactoryBean (they said "Simple" not "Short"). We declare this the same way as the ejbJndi bean. This one has three properties, the session bean's jndi name, its remote interface class and a reference to the JNDI Template. For this last property we use the form property = ref("referencedBeanId").

Now we need to copy some jars to our application/lib directory. Since we are using WebLogic all of the EJB and JNDI classes we will need are in weblogic.jar. You may have one or more jars depending on your application server. Our EJB components and our DTO classes are in a single jar, EmpEjb.jar. These may often be in different jars, in which case both would need to be placed in the lib directory.

Beans defined in resources.groovy can be auto-wired by name so we can just place def empSession in any Service or Controller class and start calling session methods.

With this ability to integrate so easily with legacy applications there is just one less reason not to start using Grails in the enterprise.
Dave Klein

About Dave Klein

After 28 years as a developer, architect, project manager (recovered), author, trainer, conference organizer, and homeschooling dad, Dave Klein landed his dream job as a developer advocate at Confluent. Dave is marveling in and eager to help others explore the amazing world of event streaming with Apache Kafka.

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »