Red Hat Training

A Red Hat training course is available for Red Hat Fuse

Chapter 5. Adding a Content-Based Router

This tutorial shows how to add a Content-Based Router (CBR) and logging to a route.

A CBR routes a message to a destination based on its content. In this tutorial, the CBR that you create routes messages to different folders based on the value of each message’s quantity field (the number of animals in the order). The maximum value of animals for each order is 10. The CBR routes the messages to different folders, depending on whether the quantity is greater than 10.

Goals

In this tutorial you complete the following tasks:

  • 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 Set Header EIP after each log endpoint
    • Add an Otherwise branch to the content-based router

Prerequisites

To start this tutorial, you need the ZooOrderApp project resulting from one of the following:

Adding and configuring a Content-Based Router

To add and configure a Content-Based Router for your route:

  1. In Project Explorer, double-click ZooOrderApp/src/main/resources/OSGI-INF/blueprint/blueprint.xml to open it in the Editor view.
  2. On the Design canvas, select the To_Received node and then select the trash can icon to delete it.
  3. In the Palette, open the Routing drawer and drag a Choice ( Choice icon ) pattern to the canvas and drop it in the Route_route1 container.

    The Route_route1 container expands to accommodate the Choice_choice1 node. The error icon indicates that the Choice_choice1 node requires a child node, which you add next.

  4. From the Routing drawer, drag a When ( When icon ) pattern to the canvas and drop it on the Choice_choice1 node:

    tutCBRaddWhen1

    The Choice_choice1 container expands to accommodate the When_when1 node. The warning icon decorating the When_when1 node indicates that one or more required property values must be set.

    Note

    The tooling prevents you from dropping a pattern onto an invalid drop point in a Route container.

  5. On the canvas, select the When_when1 node, to open its properties in the Properties view:

    tutCBRWhen1OpenProps
  6. Click the drop-down menu icon button in the Language field to open the list of available languages.
  7. Select xpath (for the XML query language) because the test messages are written in XML.

    Note

    Once you select the expression Language, the Properties view displays its properties in an indented list directly below the Language field. The Id property in this list sets the ID of the expression. The Id property following the Description field sets the ID of the When node.

  8. In the Expression field, type: /order/orderline/quantity/text() > 10

    This expression specifies that only messages in which the value of the quantity field is greater than 10 travel this path in the route (to the invalidOrders folder).

  9. Leave each of the remaining properties as they are.

    Note

    Enabling Trim removes any leading or trailing white spaces and line breaks from the message.

    tutCBRWhen1Props
  10. In the Route_route1 container, select the From_from1 node and drag its connector arrow over the Choice_choice1 node, then release it:

    tutCBRaddChoice1
  11. On the menu bar, click FileSave to save the routing context file.
  12. Click the Source tab to view the XML for the route:

    tutCBRaddedSourceV

Adding and configuring logging

Adding logging to a route allows you to track the activity of the route.

To add logging to your CBR route:

  1. In the Palette, open the Components drawer and select a Log ( Log icon ) component.
  2. Drag the Log component to the canvas and drop it on the When_when1 node.

    The When_when1 container expands to accommodate the Log_log1 node:

    tutCBRlog1Added
  3. On the canvas, select the Log_log1 node to open its properties in the Properties view.
  4. In the Message field, type: The quantity requested exceeds the maximum allowed - contact customer.

    tutCBRlog1Properties

Leave each of the remaining properties as they are.

+

Note

The tooling auto-generates a log node id value. In the 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 the Chapter 8, Tracing a message through a route tutorial). In the Console, it adds the contents of the log node’s Message field to the log data whenever the route runs.

  1. On the menu bar, click FileSave to save the routing context file.

Adding and configuring message headers

A message header contains information to process a message.

To add and configure message headers:

  1. In the Palette, open the Transformation drawer and select a Set Header ( Set Header icon ) pattern.
  2. Drag the Set Header pattern to the canvas and drop it in the When_when1 container.

    The When_when1 container expands to accommodate the SetHeader_setHeader1 node.

  3. On the canvas, select the Log_log1 node and drag its connector arrow over the SetHeader_setHeader1 node, and then release it:

    tutSetHead1Added
  4. On the canvas, select the SetHeader_setHeader1 node to open its properties in the Properties view:

    tutSetHeadPropEdNew
  5. Click the drop-down menu icon button in the Language field to open the list of available languages, and select constant:
  6. In the Expression field, type Invalid.
  7. In the Header Name field, type Destination.
  8. Leave each of the remaining properties as they are.

    tutSetHead1Properties2
  9. In the Palette, open the Components drawer and select the File ( File icon ) component.
  10. Drag the File component to the canvas and drop it in the When_when1 container.

    The When_when1 container expands to accommodate the To_to1 node.

  11. On the canvas, select the SetHeader_setHeader1 node and drag its connector arrow over the To_to1 node, and then release it:

    tutCBRWhen1TargetFile
  12. On the canvas, select the To_to1 node to open its properties in the Properties view:

    tutCBRNewTargetFileProps1
  13. On the Details tab, replace directoryName with target/messages/invalidOrders in the Uri field, and type _Invalid in the Id field:

    tutCBRNewTargetFileProps2
  14. On the menu bar, click FileSave to save the routing context file.
  15. Click the Source tab to view the XML for the route:

    tutCBRLogHeaderSourceV

Adding and configuring a branch to handle valid orders

So far, the CBR handles messages that contain invalid orders.

To add and configure an otherwise branch of your route to handle valid orders:

  1. In the Palette, open the Routing drawer and select the Otherwise ( Otherwise icon ) pattern.
  2. Drag the Otherwise pattern to the canvas and drop it into the Choice_choice1 container:

    tutCBRaddOtherwise

    The Choice_choice1 container expands to accommodate the Otherwise_otherwise1 node.

  3. On the canvas, select the Otherwise_otherwise1 node to open its properties in the Properties view.
  4. In the Id field, type _elseValid:

    tutCBROtherwiseProps
    Note

    The elseValid node will route to the terminal file node (file:target/messages/validOrders) any message that does not match the XPath expression set for the When_when1 node.

  5. In the Palette, open the Components drawer and select the Log ( Log icon ) component.
  6. Drag the Log component to the canvas and drop it on the Otherwise_elseValid node:

    tutCBROtherwiseLogAdd

    The Otherwise-elseValid container expands to accommodate the Log_log2 node.

  7. On the canvas, select the Log_log2 node to open its properties in the Properties view.
  8. In the Message field, type This is a valid order - OK to process.

    tutCBROtherwiseLog2

    Leave the remaining properties as they are.

  9. In the Palette, open the Transformation drawer and select the Set Header pattern.
  10. Drag the Set Header pattern to the canvas and drop it into the Otherwise_elseValid container.

    The Otherwise_elseValid container expands to accommodate the SetHeader_setHeader2 node.

  11. On the canvas, select the Log_log2 node and drag its connector arrow over the SetHeader_setHeader2 node, and then release it:

    tutCBRSetHead2Add
    Note

    You can collapse containers to free up space when the diagram becomes congested. To do so, select the container you want to collapse, and then click its collapse icon button:

    tutCBRcollapseWhen1Container

    To reopen the container, select it and then click its expand icon button:

    tutCBRexpandWhen1Container

    Collapsing and expanding containers in the Design tab does not affect the routing context file. It remains unchanged.

  12. On the canvas, select the SetHeader_setHeader2 node to open its properties in the Properties view.
  13. Click the drop-down menu icon button in the Language field to open the list of available languages, and select constant.
  14. In the Expression field, type ReadyForDispatcher.
  15. In the Header Name field, type Destination.
  16. Leave the remaining properties as they are.

    tutCBROtherwiseSetHeadProps2
  17. In the Palette, open the Components drawer and select the File ( File icon ) component.
  18. Drag the File component to the canvas and drop it into the Otherwise_elseValid container.

    The Otherwise_elseValid container expands to accommodate the To_to1 node.

  19. On the canvas, select the SetHeader_setHeader2 node, and drag its connector arrow over the To_to1 node and then release it:

    tutCBROtherwiseFileAdd
  20. On the canvas, select the To_to1 node to open its properties in the Properties view.
  21. In the URI field, replace directoryName with target/messages/validOrders, and in the Id field, type _Valid.

    tutCBROtherwiseTargFile2
  22. Save the routing context file.

    Here is the completed content-based router with logs and message headers:

    tutCBRfinalDesignV
  23. Click the Source tab at the bottom, left of the canvas to display the XML for the route.

    <?xml version="1.0" encoding="UTF-8"?>
    <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0
        https://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 id="_context1" xmlns="http://camel.apache.org/schema/blueprint">
            <route id="_route1">
                <from id="_from1" uri="file:src/data?noop=true"/>
                <choice id="_choice1">
                    <when id="_when1">
                        <xpath>/order/orderline/quantity/text() &gt; 10</xpath>
                        <log id="_log1" message="The quantity requested exceeds the maximum allowed - contact customer."/>
                        <setHeader headerName="Destination" id="_setHeader1">
                            <constant>Invalid</constant>
                        </setHeader>
                        <to id="_Invalid" uri="file:target/messages/invalidOrders"/>
                    </when>
                    <otherwise id="_elseValid">
                        <log id="_log2" message="This is a valid order - OK to process."/>
                        <setHeader headerName="Destination" id="_setHeader2">
                            <constant>ReadyForDispatcher</constant>
                        </setHeader>
                        <to id="_Valid" uri="file:target/messages/validOrders"/>
                    </otherwise>
                </choice>
            </route>
        </camelContext>
    </blueprint>

Verifying the CBR

You can run the new route as described in the the section called “Running the route” tutorial.

After you run it, to verify whether the route executed properly you can check the target destination folders in Project Explorer:

  1. Select ZooOrderApp.
  2. Right-click it to open the context menu, and then select Refresh.
  3. Under the project root node (ZooOrderApp), locate the target/messages/ folder and expand it.

    message destinations
  4. Check that the target/messages/invalidOrders folder contains message1.xml and message3.xml.

    In these messages, the value of the quantity element exceeds 10.

  5. Check that the target/messages/validOrders folder contains the four message files that contain valid orders: message2.xml, message4.xml , message5.xml and message6.xml.

    In these messages, the value of the quantity element is less than or equal to 10.

    Note

    To view message content, double-click each message to open it in the route editor’s XML editor.

Next steps

In the next tutorial, Chapter 6, Adding another route to the routing context, you add a second route that further processes valid order messages.