17.2. JMS Endpoints in a Router Application
The following example shows how you can integrate a JMS broker into a router application. The example generates messages using a timer; sends the messages through the
camel.timerqueue in the JMS broker; and then writes the messages to a specific directory in the file system.
In order to run the sample router application, you need to have the
activemq-camelfeature installed in the OSGi container. The
activemq-camelcomponent is needed for defining Apache ActiveMQ-based JMS endpoints in Apache Camel. This feature is not installed by default, so you must install it using the following console command:
JBossFuse:karaf@root> features:install activemq-camel
You also need the
activemqfeature, but this feature is normally available, because Red Hat JBoss Fuse installs it by default.
Most of the Apache Camel components are not installed by default. Whenever you are about to define an endpoint in a Apache Camel route, remember to check whether the corresponding component feature is installed. Apache Camel component features generally have the same name as the corresponding Apache Camel component artifact ID,
Example 17.2, “Sample Route with JMS Endpoints” gives an example of a Apache Camel route defined using the Spring XML DSL. Messages generated by the timer endpoint are propagated through the JMS broker and then written out to the file system.
Example 17.2. Sample Route with JMS Endpoints
<?xml version="1.0"?> <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"> <bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent"> <property name="brokerURL" value="tcp://localhost:61616"/> </bean> <camelContext xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="timer://MyTimer?fixedRate=true&period=4000"/> <setBody><constant>Hello World!</constant></setBody> <to uri="activemq:camel.timer"/> </route> <route> <from uri="activemq:camel.timer"/> <to uri="file:C:/temp/sandpit/timer"/> </route> </camelContext> </beans>
Camel activemq component
In general, it is necessary to create a custom instance of the Apache Camel
activemqcomponent, because you need to specify the connection details for connecting to the broker. The preceding example uses Spring syntax to instantiate the
activemqbean which connects to the broker URL,
tcp://localhost:61616. The broker URL must correspond to one of the transport connectors defined in the broker configuration file,
Example 17.2, “Sample Route with JMS Endpoints” defines two routes, as follows:
- The first route uses a
timerendpoint to generate messages at four-second intervals. The
setBodyelement places a dummy string in the body of the message (which would otherwise be
null). The messages are then sent to the
camel.timerqueue on the broker (the
activemq:camel.timeris resolved by looking up
activemqin the bean registry, which resolves to the locally instantiated bean with ID,
- The second route pulls messages off the
camel.timerqueue and then writes the messages to the specified directory,
C:\temp\sandpit\timer, in the file system.
Steps to run the example
To run the sample router application, perform the following steps:
- Using your favorite text editor, copy and paste the router configuration from Example 17.2, “Sample Route with JMS Endpoints” into a file called
camel-timer.xml.Edit the file endpoint in the second route, in order to change the target directory to a suitable location on your file system:
<route> <from uri="activemq:camel.timer"/> <to uri="file:YourDirectoryHere!"/> </route>
- Start up a local instance of the Red Hat JBoss Fuse runtime by entering the following at a command prompt:
- Make sure the requisite features are installed in the OSGi container. To install the
activemq-camelfeature, enter the following command at the console:
JBossFuse:karaf@root> features:install activemq-camelTo ensure that the
activemq-brokerfeature is not installed, enter the following command at the console:
JBossFuse:karaf@root> features:uninstall activemq-broker
- Use one of the following alternatives to obtain a broker instance for this demonstration:
- Use the default broker—assuming you have not disabled the default broker, you can use it for this demonstration, because it is listening on the correct port, 61616.
- Create a new broker instance using the console—if you prefer not to use the default broker, you can disable it (as described in Section 17.1, “Working with the Default Broker”) and then create a new JMS broker instance by entering the following command at the console:
JBossFuse:karaf@root> activemq:create-broker --name testAfter executing this command, you should see the broker configuration file,
test-broker.xml, in the
- Hot deploy the router configuration you created in step 1. Copy the
camel-timer.xmlfile into the
- Within a few seconds, you should start to see files appearing in the target directory (which is
C:\temp\sandpit\timer, by default). The file component automatically generates a unique filename for each message that it writes.It is also possible to monitor activity in the JMS broker by connecting to the Red Hat JBoss Fuse runtime's JMX port. To monitor the broker using JMX, perform the following steps:
- To monitor the JBoss Fuse runtime, start a JConsole instance (a standard Java utility) by entering the following command:
- Initially, a JConsole: Connect to Agent dialog prompts you to connect to a JMX port. From the Local tab, select the
org.apache.felix.karaf.main.Bootstrapentry and click Connect.
- In the main JConsole window, click on the MBeans tab and then drill down to
org.apache.activemq|test|Queuein the MBean tree (assuming that
testis the name of your broker).
- Under the
Queuefolder, you should see the
camel.timerqueue. Click on the
camel.timerqueue to view statistics on the message throughput of this queue.
- To shut down the router application, delete the
camel-timer.xmlfile from the
InstallDir/deploydirectory while the Karaf container is running.