이 콘텐츠는 선택한 언어로 제공되지 않습니다.

Chapter 4. To Add a Content-Based Router


This tutorial shows how to add 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 Set Header 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/blueprint.xml to open your CBRroute project.
  2. On the canvas, select the To_Others node and then select the trash can above and to the right 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.

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

  5. In the Properties view, _choice1 appears in the Id field:

    Leave the Id field as is.

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

    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 at an invalid drop point in a Route container.

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

  8. Click the drop-down menu icon button in the Language field to open the list of available languages, and select xpath:

    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.

  9. 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.

  10. Leave each of the remaining properties as is.

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

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

Adding and configuring logging

To add logging to your 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.

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

  5. Leave each of the remaining properties as is.

    Note

    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 Chapter 7, To Trace a Message Through a Route). In the Console, it adds the contents of the log node’s Message field to the log data whenever the route runs.

  6. On the menu bar, click File Save to save the routing context file.

Adding and configuring message headers

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:

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

  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, enter InvalidOrders.
  7. In the Header Name field, enter Destination.
  8. Leave each of the remaining properties as is.
  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:

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

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

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

Adding and configuring an otherwise branch

To add and configure the otherwise branch of your route:

  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:

    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.
  4. In the Id field, enter _else2:

    Note

    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 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_else2 node:

    The Otherwise-else2 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, enter valid order - process:

    Leave each of the remaining properties as is.

  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_else2 container.

    The Otherwise_else2 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:

    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:

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

    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, enter Dispatcher.
  15. In the Header Name field, enter Destination.
  16. Leave each of the remaining properties as is.
  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_else2 container.

    The Otherwise_else2 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:

  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, enter _Valid:

  22. On the menu bar, click File Save to save the routing context file.

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

  23. 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, “XML for content-based router”.

    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: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() > /order/orderline/maxAllowed/text()</xpath>
                  <log id="_log1" message="quantity requested exceeds the maximum allowed - contact customer"/>
                  <setHeader headerName="Destination" id="_setHeader1">
                      <constant>InvalidOrders</constant>
                  </setHeader>
                  <to id="_Invalid" uri="file:target/messages/invalidOrders"/>
               </when>
               <otherwise id="_else2">
                  <log id="_log2" message="valid order - process"/>
                  <setHeader headerName="Destination" id="_setHeader2">
                      <constant>Dispatcher</constant>
                  </setHeader>
                  <to id="_Valid" uri="file:target/messages/validOrders"/>
               </otherwise>
            </choice>
        </route>
      </camelContext>
    </blueprint>
    Copy to Clipboard Toggle word wrap

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.

  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

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2025 Red Hat