Red Hat Training

A Red Hat training course is available for Red Hat Fuse

Chapter 3. 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 a route
  • configure the content-based router
  • add a log endpoint to each output branch of the content-based router
  • rewire the route to use the content-based router and log its output

Prerequisites

To complete this tutorial you will need the simple-route project you created in Chapter 1, To Create a New Route.

Procedure

To add a content-based router with logging to your route:
  1. In Project Explorer, double-click simple-route/src/main/resources/META-INF/spring/camelContext.xml to open your simple-route project.
  2. Select the connector joining the two endpoint nodes file:src/data?noop=true and file:target/messages/others.
  3. Right-click it to open the context menu, and select EditRemove 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 endpoint node, file:target/messages/others, and drag it out of the way.
  5. On the canvas, select the starting endpoint node, file:src/data?noop=true, and right-click it to open the context menu.
  6. Select AddRoutingChoice.
    A choice node ( choice icon ) appears on the canvas connected to the starting endpoint node.
  7. On the canvas, select the choice node, then right-click it to open the context menu.
  8. Select AddRoutingWhen.
    A when node ( when icon ) 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 in Figure 3.1.

    Figure 3.1. When property editor

    when node property editor
  9. In the Expression field, enter /person/city='London' .
    This XPath expression determines which messages will transit this path in the route.
  10. From the Language drop-down menu, select xpath.
  11. In the Id field, enter when1.
  12. On the canvas, reselect the Choice node, then right-click it to open the context menu.
  13. Select AddRoutingOtherwise.
    An otherwise node ( otherwise icon ) appears on the canvas, connected to the choice node.
    The otherwise node will eventually route to the terminal endpoint (file:target/messages/others) any message that does not match the XPath expression set for the when1 node.
  14. On the canvas, select the when1 node, and then right-click it to open the context menu.
  15. Select AddEndpointsLog
    A log node ( log icon ) appears on the canvas, connected to the when1 node. The Properties editor opens, displaying the log node's property fields for you to edit.
  16. In the Message field, enter uk messages, and in the Log Name field, enter 1.
    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 4.4). In the Console, it adds the contents of the log node's Message field to the log data whenever the route runs.
  17. On the canvas, select the otherwise node, and then right-click it to open the context menu.
  18. Select AddEndpointsLog
    A log node ( log icon ) appears on the canvas, connected to the otherwise node. The Properties editor opens, displaying the log node's property fields for you to edit.
  19. In the Message field, enter other messages, and in the Log Name field, enter 2.
  20. On the canvas, select the log 1 node, and then right-click it to open the context menu.
  21. Select AddEndpointsEndpoint
    An Endpoint node ( endpoint icon ) appears on the canvas, connected to the log 1 node. The Properties editor opens, displaying the Endpoint node's property fields for you to edit.
  22. In the Uri field, enter file:target/messages/uk, and leave the other property fields blank.
  23. On the canvas, select the log 2 node, and then drag its connector arrow ( connector arrow icon ) to the terminal endpoint node, file:target/messages/others, and release it.
  24. 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 3.2.

    Figure 3.2. Completed content-based router with logs

    content-based route with logging
  25. On the toolbar, select FileSave to save the completed route.
  26. 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 3.1.

    Example 3.1. XML for content-based router

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:camel="http://camel.apache.org/schema/spring"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://camel.apache.org/schema/spring 
            http://camel.apache.org/schema/spring/camel-spring.xsd">
    
      <camelContext trace="false" xmlns="http://camel.apache.org/schema/spring">
        <route>
            <from uri="file:src/data?noop=true"/>
            <choice>
                <when id="when1">
                    <xpath>/person/city='London'</xpath>
                    <log logName="1" message="uk messages"/>
                    <to uri="file:target/messages/uk"/>
                </when>
                <otherwise>
                    <log logName="2" message="other messages"/>
                    <to uri="file:target/messages/others"/>
                </otherwise>
            </choice>
        </route>
    </camelContext>
    
    </beans>

Next steps

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

Further reading

To learn more about message enrichment see: