此内容没有您所选择的语言版本。
14.2. JMS Endpoints in a Router Application
Overview
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.timer
queue in the JMS broker; and then writes the messages to a specific directory in the file system.
Prerequisites
In order to run the sample router application, you need to have the
activemq-camel
feature installed in the OSGi container. The activemq-camel
component 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
activemq
feature, but this feature is normally available, because Red Hat JBoss Fuse installs it by default.
Tip
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,
camel-ComponentName
.
Router configuration
Example 14.3, “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 14.3. 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
activemq
component, because you need to specify the connection details for connecting to the broker. The preceding example uses Spring syntax to instantiate the activemq
bean 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, deploy/test-broker.xml
.
Sample routes
Example 14.3, “Sample Route with JMS Endpoints” defines two routes, as follows:
- The first route uses a
timer
endpoint to generate messages at four-second intervals. ThesetBody
element places a dummy string in the body of the message (which would otherwise benull
). The messages are then sent to thecamel.timer
queue on the broker (theactivemq:camel.timer
endpoint).NoteTheactivemq
scheme inactivemq:camel.timer
is resolved by looking upactivemq
in the bean registry, which resolves to the locally instantiated bean with ID,activemq
. - The second route pulls messages off the
camel.timer
queue 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 14.3, “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:
servicemix
- Make sure the requisite features are installed in the OSGi container. To install the
activemq-camel
feature, enter the following command at the console:JBossFuse:karaf@root> features:install activemq-camel
To ensure that theactivemq-broker
feature 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 14.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 test
After executing this command, you should see the broker configuration file,test-broker.xml
, in theInstallDir/deploy
directory.
- Hot deploy the router configuration you created in step 1. Copy the
camel-timer.xml
file into theInstallDir/deploy
directory. - 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:
jconsole
- 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.Bootstrap
entry and click Connect. - In the main JConsole window, click on the MBeans tab and then drill down to
org.apache.activemq|test|Queue
in the MBean tree (assuming thattest
is the name of your broker). - Under the
Queue
folder, you should see thecamel.timer
queue. Click on thecamel.timer
queue to view statistics on the message throughput of this queue.
- To shut down the router application, delete the
camel-timer.xml
file from theInstallDir/deploy
directory while the Karaf container is running.