1. Configure Grails for JMS
and JTA using Spring
by Jonas Behmer
PRODUCTS • CONSULTING • APPLICATION MANAGEMENT • IT OPERATIONS • SUPPORT • TRAINING
2. The question
Is it possible to use Grails for my new killer app
that needs all the JEE-stack integration stuff
with distributed transactions on top of it?
YES OF COURSE!
PRODUCTS • CONSULTING • APPLICATION MANAGEMENT • IT OPERATIONS • SUPPORT • TRAINING
3. Grails in two minutes
• Philosophy
• Convention over configuration (sensible defaults)
• Dynamic and flexible
• Builds on existing technologies with a plethora of plugins
• Technology
• Groovy
• Dynamic programming for the JVM
• Spring
• Grails is a Spring MVC in disguise
• CoC is powered by dependency injection
• Hibernate
• Object to Relational db mapping
PRODUCTS • CONSULTING • APPLICATION MANAGEMENT • IT OPERATIONS • SUPPORT • TRAINING
4. Anatomy of a GrailsApp
Where configuration
hides
Your domain will
thrive here
Transactional
components
The views
Write the tests
and prosper
PRODUCTS • CONSULTING • APPLICATION MANAGEMENT • IT OPERATIONS • SUPPORT • TRAINING
5. The scenario
• We have more than one transactional resource (both JDBC and
JMS)
• And we want JBoss to coordinate all the transactions for all of our
transactional resources.
PRODUCTS • CONSULTING • APPLICATION MANAGEMENT • IT OPERATIONS • SUPPORT • TRAINING
6. Todo!
Create a MessageListener
Create a container for the MessageListener
Add a lookup to a JBoss TransactionManager
Configure Hibernate
Deploy to JBoss
PRODUCTS • CONSULTING • APPLICATION MANAGEMENT • IT OPERATIONS • SUPPORT • TRAINING
7. Setting up Messaging
Shopping list: Lots of options in Spring:
SimpleMessageListenerContainer,
• Some kind of Message Listener DefaultMessageListenerContainer
Container that is JEE compatible or a JCA-based MessageListener
container.
• A javax.jms.MessageListener
• A javax.jms.ConnectionFactory
Note!!!
to create connections to the JMS
We could take a short cut
provider and just install the JMS-Plugin
for Grails which uses as Default-
• Destination
MessageListenerContainer behind
the curtains.
• TransactionManager
PRODUCTS • CONSULTING • APPLICATION MANAGEMENT • IT OPERATIONS • SUPPORT • TRAINING
8. Todo!
Create a MessageListener
Create a container for the MessageListener
Add a lookup to a JBoss TransactionManager
Configure Hibernate
Deploy to JBoss
PRODUCTS • CONSULTING • APPLICATION MANAGEMENT • IT OPERATIONS • SUPPORT • TRAINING
9. Setting up JTA for distributed transactions
• Spring handles transactions in
Grails through a
PlatformTransactionManager
By default we have a
• We need a
GrailsHibernateTransactionManager
JtaTransactionManager since we
have both JMS and JDBC
transactional resources
• The JtaTransactionManager can The MessageListenerContainer we've
lookup the TransactionManager setup will also use this
in JBoss through JNDI and TransactionManager
delegate all the work!
PRODUCTS • CONSULTING • APPLICATION MANAGEMENT • IT OPERATIONS • SUPPORT • TRAINING
10. Spring PlatformTransactionManager
PlatformTransactionManager
AbstractPlatformTransactionManager
JtaTransactionManager JmsTransactionManager
JpaTransactionManager
HibernateTransactionManager
GrailsHibernateTransactionManager
Class diagram of some the TransactionManager
options offered by Spring
PRODUCTS • CONSULTING • APPLICATION MANAGEMENT • IT OPERATIONS • SUPPORT • TRAINING
11. Todo!
Create a MessageListener
Create a container for the MessageListener
Add a lookup to a JBoss TransactionManager
Configure Hibernate
Deploy to JBoss
PRODUCTS • CONSULTING • APPLICATION MANAGEMENT • IT OPERATIONS • SUPPORT • TRAINING
12. Configure Hibernate
• The last bit remaining is tying it all up in Hibernate
• Must know that we want to use transactions through JTA
• hibernate.transaction.factory_class =
org.hibernate.transaction.JTATransactionFactory
• Must be made aware of the fact that JBoss coordinates the
transactions:
• hibernate.transaction.manager_lookup_class =
'org.hibernate.transaction.JBossTransactionManagerLookup'
• Lookup a XA compliant datasource in Jboss through JNDI
PRODUCTS • CONSULTING • APPLICATION MANAGEMENT • IT OPERATIONS • SUPPORT • TRAINING
13. Todo!
Create a MessageListener
Create a container for the MessageListener
Add a lookup to a JBoss TransactionManager
Configure Hibernate
Deploy to JBoss
PRODUCTS • CONSULTING • APPLICATION MANAGEMENT • IT OPERATIONS • SUPPORT • TRAINING
14. Where to go from here?
• Clone my git repo
• git clone https://github.com/jonasbehmer/vehicleposition.git
• Buy the books
• The Definitive guide to Grails (2nd edition)
• Groovy in Action (2nd edition this summer)
• Mail a consultant
• jonas.behmer@redpill-linpro.com
PRODUCTS • CONSULTING • APPLICATION MANAGEMENT • IT OPERATIONS • SUPPORT • TRAINING