¿SessionBeans and Grails?
One of the qualities of Grails that personally I like most is its integration with JEE. Since we can use Grails tags JSP, Session Beans and EntityBeans as if they were an integral part of the framework.
In this post we will focus on how we can invoke SessionBeans whether local or remote from our code Grails. This possibility, together with the management of EntityBeans Grails brings to adaptability and scalability. A I mean by this, that being neat we can develop an entire application in Grails and then later go and service models to Entities and SessionBeans respectively. This way we can quickly start developing your application using the advantages of being in a dynamic environment and then when the application is more defined and the changes are not common then we can move the business layer to JEE. This change does not affect any other part of Grails such as controllers or views.
To achieve the latter all we do is replace the code of service operations by delegation to the same operation on the SessionBeans created.
Then we will see how Grails is configured to use both EJB3.0 SessionBeans as EJB2.1
Injecting the SessionBeans
Grails allows injection in both the services and in any SB drivers like you do with your services. When Grails uses Spring in the management of its components is possible to use all the advantages of this within Grails. The easiest way to set the objects to be injected into the drivers or services, and especially the SB, is using the file resources.groovy. This file is equivalent to the Spring applicationContext.xml file only instead of being an XML file is a class that uses the SpringBuilder groovy as DSL for configuration. By default the file resources found in grails-app/conf/spring and has the following contents:
/ / Place your Spring DSL code here beans = { }
Within the closure is where you should add the definitions of the beans.
JNDI server configuration
To access any component in an application server must do so using JNDI. For this Spring provides a utility class which provides a server operations such as JNDI name lookup and it also acts as a JNDI context provider. To this we must add the following definition in resources.groovy
ejbJndi (org.springframework.jndi.JndiTemplate) {environment = ["java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory", "java.naming.provider.url", "JNP: / / http : / / localhost: 1099 Where:
- ejbJndi
- The name of the bean which is of type org.springframework.jndi.JndiTemplate. This name can be anything. It is this name that will be referenced to determine the name server that provides access to an object.
- environment
- JndiTempalte is the property of which is configured with a map that has the JNDI properties
In a file could be as many definitions as JndiTemplate JNDI providers require access.
JNDI server configuration ENVIRONMENT
It is very likely and necessary, that requires different configuration JNDI server IP and port as the environment development, production or testing. To achieve this should be appended to the previous configuration as follows:
- Place the value of the IP and port of the JBoss environment in each of Config.groovy.
- Import the file type resources.groovy ConfigurationHolder
- Replace the ip and port of the variable "java.naming.provider.url" charged by the configuration.
Example
Config.groovy
... environments { {Development ... jboss.ip = "10.200.8.173" jboss.port = "1099" ... } production { ... jboss.ip = "172.16.2.105" jboss.port = "1099" ... }
Resources.groovy
org.codehaus.groovy.grails.commons import .* ... ejbJndi (org.springframework.jndi.JndiTemplate) { environment = [ "Java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory" "Java.naming.provider.url", "JNP ://"+ +":"+ ConfigurationHolder.config.jboss.ip ConfigurationHolder.config.jboss.port] }
Configuring EJB 3.0 components
To inject EJB 3.0 components have to add the following configuration:
xXXServices (org.springframework.jndi.JndiObjectFactoryBean) { jndiName = "XXXServices / remote" jndiTemplate = ref ("ejbJndi") }
Where:
- xXXServices
- Is the name you give the remote reference to the SB and will be used to inject this into the drivers and services. Any driver or service that has a named property is xXXServices inject the reference to the remote SB defined by that name in the file resources.groovy. The first x is lowercase to indicate that by convention the name must begin in lower case.
- jndiName
- Name by which to locate the reference to the JNDI server SB
- jndiTemplate
- Name of the bean to be used as a provider to access the JNDI object. Is defined in the previous section.
To use the SB in a service would be:
EjemploServices {public class def xXXServices unMetodo void (param1, param2) { xXXServices.invocoMetodoEnEJB (param1, param 2) } }
Configuring EJB 2.1 components
In order to inject EJB 2.1 components have to add the following configuration is similar to that seen for EJB 3.0.
xXXServices (org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean) { jndiName = "EJBXXXServices" businessInterface = "biz.aquait.yyy.ejb.EJBXXXServices" jndiTemplate = ref ("ejbJndi") refreshHomeOnConnectFailure = true lookupHomeOnStartup = false }
Where:
- xXXServices
- Is the name you give the remote reference to the SB and will be used to inject this into the drivers and services. Any driver or service that has a property with this name is xXXServices inject the reference to the remote SB defined by that name in the file resources.groovy. The first x is lowercase to indicate that by convention the name must begin in lower case.
- jndiName
- Name by which to locate the reference to the JNDI server SB
- refreshHomeOnConnectFailure
- Refresh the home when it detects a connection failure, for example when the server restarts
- lookupHomeOnStartup
- It makes the application does not do the lookup to raise but to use services from SB.
- jndiTemplate
- Name of the bean to be used as a provider to access the JNDI object. Is defined in the first point.
- businessInterface
- The remote interface of the SB, ie that extends EJBObject.
How to use the SB within a service or other component is the same way that references to SB implemented with EJB 3.0.
Conclusion
Following the steps presented above we can say that SB configure Grails to use is cumbersome and that the use of these within the framework is transparent and used in the same way that a Grails service.

SessionBeans A project could be deployed in a web container like tomcat? or would it be necessary to use an application server like GlassFish?
Good post
The SB statefull or stateless either need an EJB container and therefore need to be run within an application server such as GlassFish or Jboss. On the other hand Tomcat is just a web server and as such has a web container in which components deployer PODES web pages such as JSP, JSF and Servlets.
If for some reason not want to have your application running in a Grails application server might have a distributed architecture. This would have a Tomcat Web Application which uses remotely in SB who are in an Application Server installed on another machine.