This tutorial explains how to define a Fuse Mediation Router route featuring an Fuse Message Broker endpoint, where the route is defined using Spring XML and then deployed into the OSGi container as a bundle. The implementation of the route is simple: it generates a stream of messages using a timer and the messages are then sent to a JMS queue in an Fuse Message Broker broker.
The key feature of this example is that the Fuse Message Broker endpoint at the end of the route must be configured to open a secure connection to the broker. In order to define a secure endpoint, the Fuse Message Broker component is customized to enable both SSL security and JAAS security in the underlying JMS connection factory.
This tutorial part builds on Tutorial I: SSL/TLS Security and Tutorial II: JAAS Authentication. All of the prerequisites from Prerequisites apply here and you must complete the previous tutorial parts before proceeding.
To define an Fuse Mediation Router route, which is deployed in the OSGi container and can communicate with a secure Fuse Message Broker endpoint, perform the following steps:
Edit the pom.xml
file in your Maven project and add the following
dependency as a child of the dependencies
element:
<dependencies>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.5.1-fuse-00-08</version>
<scope>provided</scope>
</dependency>
...
</dependencies>
In order to build the Fuse Mediation Router route in Maven, you need to add the following
package imports to the configuration of the maven-bundle-plugin
plug-in:
org.apache.activemq.pool org.apache.activemq.camel.component, org.apache.camel.component.jms
These imports are needed, because the Maven bundle plug-in is not able to figure out all of the imports required by beans created in the Spring configuration file.
Edit the pom.xml
file in your Maven project and add the preceding
package imports, so that the maven-bundle-plugin
configuration looks
like the following:
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Import-Package>org.apache.activemq.xbean,
org.apache.activemq.spring,
org.apache.activemq.broker,
org.apache.activemq.security,
org.apache.activemq.jaas,
org.apache.activemq.pool,
org.apache.activemq.camel.component,
org.apache.camel.component.jms,
*</Import-Package>
</instructions>
</configuration>
</plugin>
...
</plugins>
Edit the broker-spring.xml
file in the
src/main/resources/META-INF/spring
directory of your Maven project
and add the following bean definitions, which configure the Camel ActiveMQ
component:
<?xml version="1.0" encoding="UTF-8"?> <beans ... > ... <!-- Configure the activemq component: --> <bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQSslConnectionFactory"> <property name="brokerURL" value="ssl://localhost:61001" /> <property name="userName" value="smx"/> <property name="password" value="smx"/> <property name="trustStore" value="/conf/client.ts"/> <property name="trustStorePassword" value="password"/> </bean> <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"> <property name="maxConnections" value="8" /> <property name="maximumActive" value="500" /> <property name="connectionFactory" ref="jmsConnectionFactory" /> </bean> <bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration"> <property name="connectionFactory" ref="pooledConnectionFactory"/> <property name="transacted" value="false"/> <property name="concurrentConsumers" value="10"/> </bean> <bean id="activemqs" class="org.apache.activemq.camel.component.ActiveMQComponent"> <property name="configuration" ref="jmsConfig"/> </bean> </beans>
This configuration defines the secure activemqs
component, which you
can now use to define endpoints in your Fuse Mediation Router routes. The activemqs
bean references jmsConfig
, which configures the the component. The
jmsConfig
bean in turn references a chain of JMS connection
factories: the pooled connection factory wrapper,
pooledConnectionFactory
, which is important for performance; and
the secure connection factory, jmsConnectionFactory
, which is capable
of creating secure connections to the broker.
Configure a Fuse Mediation Router route that generates messages using a timer endpoint and then
sends the generated messages to the security.test
queue on the secure
broker. For this route, you need to use the secure activemqs
component
to define the endpoint that connects to the broker.
Edit the broker-spring.xml
file in the
src/main/resources/META-INF/spring
directory of your Maven project
and add the following camelContext
element, which contains the route
definition:
<?xml version="1.0" encoding="UTF-8"?> <beans ...> ... <camelContext xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="timer://myTimer?fixedRate=true&period=5000"/> <transform><constant>Hello world!</constant></transform> <to uri="activemqs:security.test"/> </route> </camelContext> ... </beans>
You must also add the location of the Fuse Mediation Router XML schema to the
xsi:schemaLocation
attribute, as highlighted in the following
example:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.4.0.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
...
Use Maven to build the bundle. Open a command prompt, switch the current directory
to
, and then enter
the following command:ProjectDir
/esb-security
mvn clean install
If you have not already done so, start up the Apache ServiceMix console (and container instance) by entering the following command in a new command prompt:
servicemix
The camel-activemq
feature, which defines the bundles required for
the Camel ActiveMQ component, is not installed by default. To
install the camel-activemq
feature, enter the following console
command:
karaf@root> features:install camel-activemq
To deploy and activate the broker bundle, enter the following console command:
karaf@root> osgi:install -s mvn:org.fusesource.example/esb-security
You can monitor the contents of the security.test
queue using a JMX
management tool, such as Java's jconsole
. Open a command prompt and
enter the following command:
jconsole
A JConsole: Connect to Agent dialog opens. From the
Local tab, select the
org.apache.karaf.main.Main
entry and click
Connect. The main JConsole window appears. Select the
MBeans tab and then drill down to
org.apache.activemq|Queue|security.test
, as shown in the screenshot
below. From the EnqueueCount
bean attribute, you can see how many
messages have been sent to the queue. By clicking Refresh to
update the bean attributes, you can see that messages are arriving at the rate of
one every five seconds.
