Chapter 4. To Add a Content-Based Router


Abstract

This tutorial walks you through adding a content-based router with logging to a route.

Goals

In this tutorial you will:
  • add a content-based router to your route
  • configure the content-based router
    • add a log endpoint to each output branch of the content-based router
    • add a SetHeader EIP after each log endpoint
    • add an Otherwise branch to the content-based router

Prerequisites

To complete this tutorial you will need the CBRroute project you created in Chapter 2, To Create a New Route.

Adding and configuring a content-based router

To add and configure a content-based router for your route:
  1. In Project Explorer, double-click CBRroute/src/main/resources/OSGI-INF/blueprint/camelContext.xml to open your CBRroute project.
  2. Select the connector joining the two file: nodes file:src/data?noop=true and file:target/messages/others.
  3. Right-click it to open the context menu, and select Remove to delete the connector.
    Note
    Alternatively, you can delete the connector by selecting it, then selecting Delete from the toolbar's Edit menu.
  4. On the canvas, select the terminal file: node, file:target/messages/others, and in the Properties editor, change the Uri and Id properties to:
    • Uri:file:target/messages/validOrders
    • Id:toValid
    Then drag the node out of the way. You will connect it to another node later in this tutorial.
  5. On the canvas, select the starting file: node, file:src/data?noop=true, and right-click it to open the context menu.
  6. Select Add Routing Choice.
    A choice node ( choice icon ) appears on the canvas connected to the starting file: node.
  7. In the Properties editor, enter choice1 in the Id field.
  8. On the canvas, select the choice node, then right-click it to open the context menu.
  9. Select Add Routing When.
    A when node appears on the canvas connected to the choice node. The Properties editor opens, displaying the when node's property fields for you to edit, as shown:
    Note
    When a required property is blank, the Properties editor marks it with error icon . The number of properties that require configuring is displayed in the title bar. Error icons and message disappear when you configure the required properties.
  10. In the Expression field, enter /order/orderline/quantity/text() > /order/orderline/maxAllowed/text() .
    This expression determines which messages will transit this path in the route.
  11. From the Language drop-down menu, select xpath.
  12. In the Id field, enter when1.
    Figure 4.1 shows the when1 node configured.

    Figure 4.1. when1 configuration

    when1 configuration

Adding and configuring logging

To add logging to your route:
  1. On the canvas, select the when1 node, and then right-click it to open the context menu.
  2. Select Add Components Log.
    A log node appears on the canvas, connected to the when1 node. The Properties editor opens, displaying the log node's property fields for you to edit.
  3. In the Message field, enter quantity requested exceeds the maximum allowed - contact customer.
  4. In the Id field, enter log1.
    Figure 4.2 shows the log1 node configured.

    Figure 4.2. Log1 configuration

    Log1 configuration
    Note
    In Fuse Integration perspective's Messages View, the tooling inserts the contents of the log node's Id field in the Trace Node Id column for message instances, when tracing is enabled on the route (see Figure 7.11, “Fuse Integration perspective's message tracing components”). In the Console, it adds the contents of the log node's Message field to the log data whenever the route runs.

Adding and configuring message headers

To add and configure message headers:
  1. On the canvas, select the log1 node, and then right-click it to open the context menu.
  2. Select Add Transformation SetHeader.
    A setHeader node ( setHeader icon ) appears on the canvas, connected to the log 1 node. The Properties editor opens, displaying the setHeader node's property fields for you to edit, as shown:
  3. In the Header Name field, enter Destination.
  4. In the Expression field, enter InvalidOrders.
  5. Select constant from the Language drop-down menu.
  6. In the Id field, enter setHead1.
    Figure 4.3 shows the setHead1 node configured.

    Figure 4.3. setHead1 configuration

    setHead1 configuration
  7. On the canvas, select the setHead1 node, and then right-click it to open the context menu.
  8. Select Add Components File.
    A file:directoryName node appears on the canvas, connected to the setHead1 node. The Properties editor opens, displaying the file:directoryName node's property fields for you to edit.
  9. On the Generic tab, replace directoryName with target/messages/invalidOrders in the Uri field, and enter toInvalid in the Id field.

Adding and configuring an otherwise branch

To add and configure the otherwise branch to your route:
  1. On the canvas, reselect the Choice node, then right-click it to open the context menu.
  2. Select Add Routing Otherwise.
    An otherwise node appears on the canvas, connected to the choice node. The Properties editor opens, displaying the otherwise node's property fields for you to edit.
  3. In the Id field, enter else2 .
    The else2 node will eventually route to the terminal file: node (file:target/messages/validOrders) any message that does not match the XPath expression set for the when1 node.
  4. On the canvas, select the else2 node, and then right-click it to open the context menu.
  5. Select Add Components Log.
    A log node appears on the canvas, connected to the else2 node. The Properties editor opens, displaying the log node's property fields for you to edit.
  6. In the Message field, enter valid order - process, and in the Id field, enter log2.
  7. On the canvas, select the log2 node, and then right-click it to open the context menu.
  8. Select Add Transformation SetHeader.
    A setHeader node ( setHeader icon ) appears on the canvas, connected to the log2 node. The Properties editor opens, displaying the setHeader node's property fields for you to edit.
  9. In the Header Name field, enter Destination.
  10. In the Expression field, enter Dispatcher.
  11. Select constant from the Language drop-down menu.
  12. In the Id field, enter setHead2.
  13. On the canvas, drag the terminal file: node, file:target/messages/validOrders, close to the setHead2 node.
  14. Select the setHead2 node, and then drag its connector arrow ( connector arrow icon ) to the terminal file: node and release it.
  15. To quickly realign all of the nodes on the canvas, right-click the canvas to open the context menu, and then select Layout Diagram.
    The route on the canvas should resemble Figure 4.4.

    Figure 4.4. Completed content-based router with logs and message headers

    content-based route with logging and message headers
  16. On the toolbar, select File Save to save the completed route.
  17. Click the Source tab at the bottom, left of the canvas to display the XML for the route.
    The camelContext element will look like that shown in Example 4.1.

    Example 4.1. XML for content-based router

    <?xml version="1.0" encoding="UTF-8"?>
    <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:camel="http://camel.apache.org/schema/blueprint"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 
               http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
           http://camel.apache.org/schema/blueprint 
               http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">
    
      <camelContext trace="false" xmlns="http://camel.apache.org/schema/blueprint">
      <route>
        <from uri="file:src/data?noop=true"/>
        <choice id="choice1">
          <when id="when1">
            <xpath>/order/orderline/quantity/text() > /order/orderline/maxAllowed/text()</xpath>
            <log message="quantity requested exceeds maximum allowed - contact customer" id="log1"/>
            <setHeader headerName="Destination" id="setHead1">
              <constant>InvalidOrders</constant>
            </setHeader>
            <to uri="file:target/messages/invalidOrders" id="toInvalid"/>
          </when>
          <otherwise id="else2">
            <log message="valid order - process" id="log2"/>
            <setHeader headerName="Destination" id="setHead2">
              <constant>Dispatcher</constant>
            </setHeader>
            <to uri="file:target/messages/validOrders" id="toValid"/>
          </otherwise>
        </choice>
      </route>
     </camelContext>
    </blueprint>

Next steps

You can run the new route as described in the section called “Running the route”.
After you run it, you can easily verify whether the route executed properly by checking the target destinations in Project Explorer:
  1. Select CBRroute.
  2. Right-click it to open the context menu, then select Refresh.
  3. Under the project root node (CBRroute), locate the folder target/messages/ and expand it, as shown in Figure 4.5.

    Figure 4.5. Target message destinations in Project Explorer

    message destinations
  4. Check that the target/messages/invalidOrders folder contains message2.xml and message4.xml.
    In these messages, the value of the quantity element should exceed the value of the maxAllowed element.
  5. Check that the target/messages/validOrders folder contains the four message files that contain valid orders: message1.xml, message3.xml , message5.xml and message6.xml.
    In these messages, the value of the quantity element should be less than or equal to the value of the maxAllowed element.
    Note
    To view message content, double-click each message to open it in the route editor's xml editor.

Further reading

To learn more about message enrichment see:
Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.