Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Next »

It's a Groovy Categories that make it very easy to use JMS in Groovy application.


  • You have to provide the JMS Factory, refer to the Spring ActiveMQ setup section on this page, or check the JMS setup examples in the Grails JMS Plugin page.


Let's start with some examples first:

Setup JMS Connection Factory

For every example, it's assumed a JMS Connection Factory called "jms" is existed. For example, you could create a ActiveMQ connection factory programmatically with:

Simple usages

  1. Subscribe to a Topic message
  2. Send a message to a Queue or Topic
  3. Receive a Queue message
    The receive parameter means "receives the next message that arrives within the specified timeout interval", check JMS JavaDoc for details:
  4. Reply to a message

Advanced Usages/Issues

  1. JMS Connection and Session reuse, and close()
    1. Connection and Session re-use
      By default, it reuses a single JMS Connection and Session. The connection and session are created when any of connect(), session(), topic() or queue() method is first called, and the connection and session are binded to internal ThreadLocal variables until close() is called. So,
      • connection and session are not share/re-used across threads
      • you have to call close() to remove a session in order to use a new connection and session.
    2. When to close()?
      The close() method call a connection.start(), unset all ThreadLocal variables, and call the connection.close() to close the connection.

For server applications, every request are run in its own thread, and the thread may be re-used.

For singleton services in server applications, notice that the GroovyJMS is thread safe in a way different thread has its own connection and session. If you want to share the Connection and/or Session, simply create the connection and session. If you want to utilize the convenient connect() and session() methods, you could use:

It's a matter of taste. You for sure can call the origianl jms.createConnection() directly. But if you will do some jms operation, it will save some code. You could also retrieve any created Connection and Session with:

    1. When you need to call start()?
      If you don't close your connection and session
    2. As createTopic() and createQueue() do not actually make connection to the server but to create a reference. So i personally think it's not entirely necessary to reuse reference to a queue or topic for general cases.

How it works?


  • It's current hardcoded to use Log4j. It could be changed or removed on request. 
  • No labels