This tutorial shows how you can connect to HornetQ embedded in JBoss EAP 6 using a Camel Route.

First of all, you need to create an application user on the application server. The user will be granted the guest role which by default has permission to send and consume messages on all JMS destinations, as you can see from this snippet:

<security-settings>

	<security-setting match="#">

		<permission type="send" roles="guest"/>

		<permission type="consume" roles="guest"/>

		<permission type="createNonDurableQueue" roles="guest"/>

		<permission type="deleteNonDurableQueue" roles="guest"/>

	</security-setting>

</security-settings>

So, launch the add-user.sh script contained in the JBOSS_HOME/bin folder and follow this transcript:

What type of user do you wish to add?

a) Management User (mgmt-users.properties)

b) Application User (application-users.properties)

(a): b

Enter the details of the new user to add.

Using realm 'ApplicationRealm' as discovered from the existing property files.

Username : jmsuser

Password :

Re-enter Password :

What groups do you want this user to belong to? (Please enter a comma separated list, or leave blank for none)[ ]: guest

About to add user 'jmsuser' for realm 'ApplicationRealm'

Is this correct yes/no? yes

Done with user creation, now start the application server using a full configuration (or full-ha):

./standalone.sh -c standalone-full.xml

Now create a queue using a management instrument like the CLI:

jms-queue add --queue-address=ExampleQueue --entries=queue/exampleQueue,java:jboss/exported/jms/queue/exampleQueue

Building the Camel route

We have finished with the application server. Now it's time to build our Camel route. We will use Spring XML file to build the route:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring"
	xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

	<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">

		<property name="environment">

			<props>

				<prop key="java.naming.factory.initial">org.jboss.naming.remote.client.InitialContextFactory

				</prop>

				<prop key="java.naming.provider.url">remote://localhost:4447</prop>

			</props>

		</property>

	</bean>

	<bean name="jms" class="org.apache.camel.component.jms.JmsComponent">

		<property name="connectionFactory" ref="authenticatedConnectionFactory" />

	</bean>

	<bean id="jmsQueueConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">

		<property name="jndiTemplate">

			<ref bean="jndiTemplate" />

		</property>

		<property name="jndiName">

			<value>java:jms/RemoteConnectionFactory</value>

		</property>

	</bean>

	<bean id="authenticatedConnectionFactory"
		class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">

		<property name="targetConnectionFactory" ref="jmsQueueConnectionFactory" />
		<property name="username" value="jmsuser" />
		<property name="password" value="Password1!" />

	</bean>

	<camel:camelContext xmlns="http://camel.apache.org/schema/spring">

		<camel:route>

			<camel:from uri="timer:foo?period=1s" />

			<setBody>

				<simple>${body}Message at ${date:now:yyyy-MM-dd HH:mm:ss}</simple>

			</setBody>

			<camel:to uri="jms:queue:ExampleQueue" />

		</camel:route>

	</camel:camelContext>

</beans>

So what are ther key properties of this example: first of all you have to specify the server remote address:

<prop key="java.naming.provider.url">remote://localhost:4447</prop>

Next,we have to specify the name of the RemoteConnectionFactory to be used, which by default is java:jms/RemoteConnectionFactory

<property name="jndiName">
	<value>java:jms/RemoteConnectionFactory</value>
</property>

The, we have to specify the authenticatedConnectionFactory information using the user that we have formerly created on JBoss EAP:

<bean id="authenticatedConnectionFactory"

	class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">

	<property name="targetConnectionFactory" ref="jmsQueueConnectionFactory" />
	<property name="username" value="jmsuser" />
	<property name="password" value="Password1!" />

</bean>

And finally the Camel route: we are addressing messages from the Timer every second to the ExampleQueue. Mind that we are using a simple transformation to provide some body with the date of the messages

Find the complete project on Github

0
0
0
s2smodern