Hi, recently, I had a requirement to externalize the data of the embedded ActiveMQ broker in my grails application to survive the server re-start. In order to achieve this, I had to change the default data directory of the broker. As shown by the official sample configuration here for the broker, I had to set the attribute dataDirectory of broker element.

Configuring broker in plain Spring XML configuration is easy as shown by the sample configuration and it can be done with the following 2 steps:

  • Add ActiveMQ name-space to the XML
    xmlns:amq="http://activemq.apache.org/schema/core"
  • Add broker configuration
    <amq:broker brokerName="defaultBroker"
                useJmx="false"
                persistent="true"
                dataDirectory="activemq-data">
    
            <amq:transportConnectors>
                <amq:transportConnector uri="tcp://localhost:61616" />
            </amq:transportConnectors>
        </amq:broker>
    
      

In Grails, broker configuration can be provided through the grails-app/conf/spring/resources.groovy. By default, you’d have the beans closure like this:

  beans = {

	}
  

You need to provide Spring configuration using Groovy DSL. So, we need to modify the above two steps as per the Groovy DSL:

  • Add ActiveMQ name-space to the beans closure
          	beans = {
        		xmlns amq:"http://activemq.apache.org/schema/core"
    		}
          
  • Add broker configuration to the beans closure
    beans = {
        xmlns amq:"http://activemq.apache.org/schema/core"
    
        amq.'broker'(
                useJmx:'false',
                persistent:'true',
                //change this to use some other directory for data
                dataDirectory: 'my-activemq-data'){
            amq.'transportConnectors'{
                amq.'transportConnector'(uri:'tcp://localhost:61616')
            }
        }
    }
    
      

That’s all. The broker is configured to use my-activemq-data directory for the data.

Hope, this helps. 🙂