此内容没有您所选择的语言版本。

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&amp;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

  1. The first route uses a timer endpoint to generate messages at four-second intervals. The setBody element places a dummy string in the body of the message (which would otherwise be null). The messages are then sent to the camel.timer queue on the broker (the activemq:camel.timer endpoint).
    Note
    The activemq scheme in activemq:camel.timer is resolved by looking up activemq in the bean registry, which resolves to the locally instantiated bean with ID, activemq.
  2. 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:
  1. 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>
  2. Start up a local instance of the Red Hat JBoss Fuse runtime by entering the following at a command prompt:
    servicemix
  3. 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 the activemq-broker feature is not installed, enter the following command at the console:
    JBossFuse:karaf@root> features:uninstall activemq-broker
  4. 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 the InstallDir/deploy directory.
  5. Hot deploy the router configuration you created in step 1. Copy the camel-timer.xml file into the InstallDir/deploy directory.
  6. 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:
    1. To monitor the JBoss Fuse runtime, start a JConsole instance (a standard Java utility) by entering the following command:
      jconsole
    2. 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.
    3. 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 that test is the name of your broker).
    4. Under the Queue folder, you should see the camel.timer queue. Click on the camel.timer queue to view statistics on the message throughput of this queue.
  7. To shut down the router application, delete the camel-timer.xml file from the InstallDir/deploy directory while the Karaf container is running.
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.