Kennis Blogs Configuring Camel to use HornetQ 2.2.13 in JBoss AS 7.1.1

Configuring Camel to use HornetQ 2.2.13 in JBoss AS 7.1.1

JBoss AS 7.1.1 has version 2.2.13 of the HornetQ messaging server built-in. I needed to use JMS for a Camel route from outside the container and found that configuring and using HornetQ is not really difficult. It can be confusing however, mainly because the documentation and examples I found often refer to a different version of either HornetQ or JBoss AS. In the end I got it to work; to save you the trouble of having to find it all out by yourself, I thought I'd write a walk-through of the process...



First, you will need to enable HornetQ and configure it. I used a simple configuration without clustering or HA. You will need the following additions to your JBoss configuration file:


In <extensions>, add:

&lt;extension module=""/&gt;

In <profile>, add the messaging subsystem:

<subsystem xmlns="urn:jboss:domain:messaging:1.1">
<netty-connector name="netty" socket-binding="messaging"/>
<netty-connector name="netty-throughput" socket-binding="messaging-throughput">
<param key="batch-delay" value="50"/>
<in-vm-connector name="in-vm" server-id="0"/>
<netty-acceptor name="netty" socket-binding="messaging"/>
<netty-acceptor name="netty-throughput" socket-binding="messaging-throughput">
<param key="batch-delay" value="50"/>
<param key="direct-deliver" value="false"/>
<in-vm-acceptor name="in-vm" server-id="0"/>
<address-setting match="#">
<connection-factory name="InVmConnectionFactory">
<connector-ref connector-name="in-vm"/>
<entry name="java:/ConnectionFactory"/>
<connection-factory name="RemoteConnectionFactory">
<connector-ref connector-name="netty"/>
<entry name="RemoteConnectionFactory"/>
<entry name="java:jboss/exported/jms/RemoteConnectionFactory"/>
<pooled-connection-factory name="hornetq-ra">
<transaction mode="xa"/>
<connector-ref connector-name="in-vm"/>
<entry name="java:/JmsXA"/>
<jms-queue name="someQueue">
<entry name="queue/someQueue"/>
<jms-queue name="someOtherQueue">
<entry name="queue/someOtherQueue"/>

In the <socket-binding-group>:

<socket-binding name="messaging" port="5445"/>
<socket-binding name="messaging-throughput" port="5455"/>


Now, when you start JBoss, you will see some HornetQ logging as well:


15:18:55,511 INFO  [org.hornetq.core.server.impl.HornetQServerImpl] (MSC service thread 1-4) live server is starting with configuration HornetQ Configuration (clustered=false,backup=false,sharedStore=true,journalDirectory=/Applications/jboss-as-7.1.1.Final/standalone/data/messagingjournal,bindingsDirectory=/Applications/jboss-as-7.1.1.Final/standalone/data/messagingbindings,largeMessagesDirectory=/Applications/jboss-as-7.1.1.Final/standalone/data/messaginglargemessages,pagingDirectory=/Applications/jboss-as-7.1.1.Final/standalone/data/messagingpaging)
15:18:55,513 INFO  [org.hornetq.core.server.impl.HornetQServerImpl] (MSC service thread 1-4) Waiting to obtain live lock
15:18:55,542 INFO  [org.hornetq.core.persistence.impl.journal.JournalStorageManager] (MSC service thread 1-4) Using NIO Journal
15:18:55,605 INFO  [org.hornetq.core.server.impl.FileLockNodeManager] (MSC service thread 1-4) Waiting to obtain live lock
15:18:55,606 INFO  [org.hornetq.core.server.impl.FileLockNodeManager] (MSC service thread 1-4) Live Server Obtained live lock
15:18:55,785 INFO  [org.hornetq.core.remoting.impl.netty.NettyAcceptor] (MSC service thread 1-4) Started Netty Acceptor version 3.2.5.Final-a96d88c localhost:5445 for CORE protocol
15:18:55,786 INFO  [org.hornetq.core.remoting.impl.netty.NettyAcceptor] (MSC service thread 1-4) Started Netty Acceptor version 3.2.5.Final-a96d88c localhost:5455 for CORE protocol
15:18:55,787 INFO  [org.hornetq.core.server.impl.HornetQServerImpl] (MSC service thread 1-4) Server is now live
15:18:55,787 INFO  [org.hornetq.core.server.impl.HornetQServerImpl] (MSC service thread 1-4) HornetQ Server version 2.2.13.Final (HQ_2_2_13_FINAL_AS7, 122) [6b03751f-0e2a-11e2-a9c7-1040f3ac1aa0]) started
15:18:55,790 INFO  [org.hornetq.core.server.impl.HornetQServerImpl] (MSC service thread 1-7) trying to deploy queue jms.queue.someQueue
15:18:55,811 INFO  [org.hornetq.core.server.impl.HornetQServerImpl] (MSC service thread 1-8) trying to deploy queue jms.queue.someOtherQueue
15:18:55,842 INFO  [org.hornetq.ra.HornetQResourceAdapter] (MSC service thread 1-7) HornetQ resource adaptor started


The next step will be to connect Camel to one of the queues. Because the JNDI lookup is secured, you need to provide a user in the ApplicationRealm. This user needs no special roles (for now). If you don't have a user for this, create one (using bin/


Now we can add the JNDI lookup connection to our Spring application context:


<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<prop key="java.naming.factory.initial">org.jboss.naming.remote.client.InitialContextFactory</prop>
<prop key="java.naming.provider.url">remote://localhost:4447</prop>
<prop key="">[your-user]</prop>
<prop key="">[your-password]</prop>


And the actual lookup of the JMS connection factory:


<bean id="jmsQueueConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate">
<ref bean="jndiTemplate"/>
<property name="jndiName">


Finally, we can create the Camel JMS component:


<bean name="jms" class="org.apache.camel.component.jms.JmsComponent">
<property name="connectionFactory" ref="jmsQueueConnectionFactory"/>


Now you can access the queues directly from your Camel route builder:


public void configure() throws Exception {


Note that the "queue:" part for the endpoint is optional. Queue is the default; if you want to connect to a topic, "topic:" is required.

Also note that you must use the name of the queue (jms-queue name=), not the entry, since the latter is the JNDI location of the queue.

As you may have noted, this example runs without security on the queues, it only uses security for the JNDI lookup. Security is enabled by default, and for good reason, so let's enable and configure it now. First, enable security by changing "security-enabled" in the messaging domain to true (or you could remove the entry entirely):



Next, add the security settings after the acceptors:

<security-setting match="#">
<permission type="send" roles="messaging"/>
<permission type="consume" roles="messaging"/>


The match="#" attribute means these settings apply to all queues and topics. The '#'-character is the wildcard here. You could enter several security-setting elements and apply them to different sets of queues or topics by using other match strings, e.g. match="some#".


This settings allows users with the role "messaging" to send messages to the queues and consume messages from the queues. To still be able to send and consume messages from Camel you will have to supply credentials for a user with that role when accessing the connection factory.


First, you'll have to edit add the role "messaging" to the user you created earlier. Then, when obtaining a connection to the queue, you need to provide the credentials again. I used the Spring UserCredentialsConnectionFactoryAdapter to wrap the connection factory:


<bean id="authenticatedConnectionFactory" class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
<property name="targetConnectionFactory" ref="jmsQueueConnectionFactory"/>
<property name="username" value="[your-user]"/>
<property name="password" value="[your-password]"/>
<bean name="jms" class="org.apache.camel.component.jms.JmsComponent">
<property name="connectionFactory" ref="authenticatedConnectionFactory"/>


And there you are, all ready to go!