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.
In order to run the sample router application, you need to have the
camel-activemq
feature installed in the OSGi container. The
camel-activemq
component is needed for defining Fuse Message Broker-based JMS
endpoints in Fuse Mediation Router. This feature is not installed by
default, so you must install it using the following console command:
karaf@root> features:install camel-activemq
You also need the activemq
feature, but this feature is normally
available, because Fuse ESB installs it by default.
![]() | Tip |
---|---|
Most of the Fuse Mediation Router components are not installed by
default. Whenever you are about to define an endpoint in a Fuse Mediation Router route,
remember to check whether the corresponding component feature is installed.
Fuse Mediation Router component features generally have the same name as the corresponding
Fuse Mediation Router component artifact ID,
|
Example 9.1 gives an example of a Fuse Mediation Router 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 9.1. Sample Route with JMS Endpoints
<?xml version="1.0"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring" xmlns:osgi="http://www.springframework.org/schema/osgi" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd http://www.springframework.org/schema/osgi-compendium http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd"> <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>
In general, it is necessary to create a custom instance of the Fuse Mediation Router
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
.
Example 9.1 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).Note The
activemq
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.
To run the sample router application, perform the following steps:
Using your favorite text editor, copy and paste the router configuration from Example 9.1 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 Fuse ESB runtime by entering the following at a command prompt:
servicemix
Make sure the requisite features are installed in the OSGi container. To install the
camel-activemq
feature, enter the following command at the console:karaf@root> features:install camel-activemq
To ensure that the
activemq-broker
feature is not installed, enter the following command at the console: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 The Default Broker) and then create a new JMS broker instance by entering the following command at the console:
karaf@root> activemq:create-broker --name test
After executing this command, you should see the broker configuration file,
test-broker.xml
, in the
directory.InstallDir
/deploy
Hot deploy the router configuration you created in step 1. Copy the
camel-timer.xml
file into the
directory.InstallDir
/deployWithin 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 Fuse ESB runtime's JMX port. To monitor the broker using JMX, perform the following steps:
To monitor the Fuse ESB 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 the
directory.InstallDir
/deploy