Chapter 5. To Add Another Route to the CBR Routing Context

This tutorial shows you how to add a second route to the blueprint.xml file in the CBRroute project. The second route:

  • Takes messages directly from the terminal end of the first route’s otherwise branch
  • Sorts the messages according to customers' country
  • Sends each message to the corresponding country folder in the CBRroute/target/messages folder.

Goals

In this tutorial you will:

  • Reconfigure the existing route for direct connection to a second route
  • Add a second route to your <camelContext>
  • Configure the new route to take messages directly from the otherwise branch of the first route
  • Add a content-based router to the new route
  • Add and configure a message header, logging, and target destination to each output branch of the new route’s content-based router

Prerequisites

To complete this tutorial you will need the CBRroute project you modified in Chapter 4, To Add a Content-Based Router.

Note

If you skipped any tutorial after Chapter 2, To Create a New Route, you can use the prefabricated blueprint5.xml file to work through this tutorial (for details, see Chapter 1, Using the Fuse Tooling Resource Files).

Reconfiguring the existing route for direct connection

To configure the existing route for direct connection with the new route:

  1. Open your CBRroute/src/main/resources/OSGI-INF/blueprint/blueprint.xml in the route editor.
  2. On the canvas, select the Route_route1 container to open its properties in the Properties view.
  3. Scroll down to the Shutdown Route property and enter Default.
  4. On the canvas, select the terminal file node To_Valid to display its properties in the Properties view.
  5. In the Uri field, delete the existing text, and then enter direct:OrderFulfillment.
  6. In the Id field, enter _Fulfill.
Note

Instead of repurposing the existing To_Valid terminal file node, you could have replaced it with a ComponentsDirect component, configuring it with the same property values as the repurposed To_Valid node.

Adding the second route

To add a route to the routing context:

  1. In the Palette, open the Routing drawer and select the Route ( Route icon ) pattern.
  2. Drag the Route pattern to the canvas and drop it next to the Route_route1 container:

    tutRte2DropOnCanvas

    The Route pattern becomes the Route_route2 container node on the canvas.

  3. Click the Route_route2 container node to display its properties in the Properties view.
  4. Leave each of the properties as is.
Note

As your multiroute routing context grows in complexity, you may want to focus the route editor on an individual route while you work on it. To do so, in Project Explorer, double-click the route you want the route editor to display on the canvas; for example Route_route2:

tutProjExSwitchRtesOnCanvas

To display all routes in the routing context on the canvas, double-click the project’s .xml context file entry (src/main/resources/OSGI-INF/…​) at the top of the Camel Contexts folder.

Building and configuring the USA branch of the second route

  1. In the Palette, open the Components drawer and drag a Direct component ( Direct icon ) to the canvas and drop it in the Route_route2 container:

    tutCBRrte2From2Add

    The Route_route2 container expands to accommodate the From_from2 node.

  2. On the canvas, select the From_from2 node to open its properties in the Properties view:

    tutCBRrte2From2Props
  3. In the Uri field, replace name (following direct:) with OrderFulfillment, and in the Id field, enter _direct:OrderFulfillment.
  4. In the Palette, open the Routing drawer and drag a Choice ( Choice icon ) pattern to the canvas and drop it in the Route_route2 container.

    The Route_route2 container expands to accommodate the Choice_choice2 node.

  5. In the Route_route2 container, select the direct:OrderFulfillment node and drag its connector arrow over the Choice_choice2 node, then release it:

    tutCBRrte2Choice2Added
  6. In the Properties view, leave each of the Choice_choice2 node’s properties as is.
  7. In the Palette, open the Routing drawer and drag a When ( When icon ) pattern to the canvas and drop it in the Choice_choice2 container:

    tutCBRrte2When2Added

    The Choice_choice2 container expands to accommodate the When_when2 node.

  8. On the canvas, select the When_when2 node to open its properties in the Properties view:

    tutCBRrte2When2usaProps
  9. Set the node’s properties this way:

    tutCBRrte2WhenUSAprops
    • Select xpath from the Language drop-down menu.
    • Enter /order/customer/country = 'USA' in the Expression field.
    • Leave Trim enabled.
    • Enter _when/usa in the Id field.

      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.

  10. In the Palette, open the Transformation drawer and drag the Set Header pattern to the canvas and drop on the When_when/usa node:

    tutCBRrte2SetHeadUSAadd

    The When_when/usa container expands to accommodate the SetHeader_setHeader3 node.

  11. On the canvas, select the SetHeader_setHeader3 node to open its properties in the Properties view:

    tutCRErte2SetHead3Props
  12. Set the node’s properties this way:

    tutCRErte2SetHeadusaProps
    • Select constant from the Language drop-down menu.
    • Enter USA in the Expression field.
    • Leave Trim enabled.
    • Enter Destination in the Header Name field.
    • Enter _setHead_usa in the Id field.
  13. In the Palette, open the Components drawer and drag a Log component ( Log icon ) to the canvas and drop it in the When_when/usa container.

    The When_when/usa container expands to accommodate the Log_log3 node.

  14. On the canvas, select the SetHeader_setHead_usa node and drag its connector arrow over the Log_log3 node, then release it:

    tutCBRrte2Log3Added
  15. On the canvas, select the Log_log3 node to open its properties in the Properties view:

    tutCBRrteLog3Props
  16. In the Properties view:

    tutCBRrte2Log3usaAdded
    • Enter Valid order - ship animals to USA customer in the Message field.
    • Enter _usa in the Id field.
    • Leave Logging Level as is.
  17. In the Palette, open the Components drawer and drag a File component ( File icon ) to the canvas and drop it in the When_when/usa container.

    The When_when/usa container expands to accommodate the To_to1 node.

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

    tutCBRrte2USAtermFile
  19. In the Properties view:

    tutRte2EndptUSA
    • Replace directoryName with target/messages/USA in the Uri field.
    • Enter _US in the Id field.
  20. On the menu bar, click FileSave to save the routing context file.

    The USA branch of Route_route2 should look like this:

    tutUSAbranchRte2

Building and configuring the Great Britain branch of the second route

With Route_route2 displayed on the canvas:

  1. In the Palette, open the Routing drawer and drag a When pattern ( When icon ) to the canvas and drop it in the Choice_choice2 container:

    tutCBRrte2WhenGBAdd

    The Choice_choice2 container expands to accommodate the When_when2 node.

  2. On the canvas, select the When_when2 node to open its properties in the Properties view.
  3. In the Properties view:

    • Select xpath from the Language drop-down menu.
    • Enter /order/customer/country = 'Great Britain' in the Expression field.
    • Leave Trim enabled.
    • Enter _when/gb in the Id field.
  4. In the Palette, open the Transformation drawer and drag a Set Header pattern ( Set Header icon ) to the canvas and drop it on the When_when/gb node:

    tutCBRrte2SetHeadUKAdd

    The When_when/gb container expands to accommodate the SetHeader_setHeader3 node.

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

    • Select constant from the Language drop-down menu.
    • Enter UK in the Expression field.
    • Leave Trim as is.
    • Enter Destination in the Header Name field.
    • Enter_setHead_uk in the Id field.
  7. In the Palette, open the Components drawer and drag a Log pattern ( Log icon ) to the canvas and drop it in the When_when/gb container.

    The When_when/gb container expands to accommodate the Log_log3 node.

  8. On the canvas, select the SetHeader_setHead_uk node and drag its connector arrow over the Log_log3 node, and then release it:

    tutCBRrte2LogUKAdd
  9. On the canvas, select the Log_log3 node to open its properties in the Properties view.
  10. In the Properties view:

    • Enter Valid order - ship animals to UK customer in the Message field.
    • Enter _uk in the Id field.
    • Leave the Logging Level as is.
  11. From the Components drawer, drag a File pattern ( File icon ) to the canvas and drop it in the When_when/gb container.

    The When_when/gb container expands to accommodate the To_to1 node.

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

    tutCBRrte2UKtermFile
  13. On the canvas, select the To_to1 node to open its properties in the Properties view.
  14. In the Properties view:

    • Replace directoryName with target/messages/GreatBritain in the Uri field.
    • Enter _UK in the Id field.
  15. On the menu bar, click FileSave to save the routing context file.

    The Great Britain branch of Route_route2 should look like this:

    tutUKbranchRte2

Building and configuring the Germany branch of the second route

With Route_route2 displayed on the canvas:

  1. In the Palette, open the Routing drawer and drag a When pattern ( When icon ) to the canvas and drop it in the Choice_choice2 container:

    tutCBRrte2WhenGERAdd

    The Choice_choice2 container expands to accommodate the When_when2 node.

  2. On the canvas, select the When_when2 node to open its properties in the Properties view.
  3. In the Properties view:

    • Select xpath from the Language drop-down menu.
    • Enter /order/customer/country = 'Germany' in the Expression field.
    • Leave Trim enabled.
    • Enter _when/ger in the Id field.
  4. In the Palette, open the Transformation drawer and drag a Set Header pattern ( Set Header icon ) to the canvas and drop it on the When_when/ger node:

    tutCBRrte2SetHeadGERAdd

    The When_when/ger container expands to accommodate the SetHeader_setHeader3 node.

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

    • Select constant from the Language drop-down menu.
    • Enter Germany in the Expression field.
    • Leave Trim as is.
    • Enter Destination in the Header Name field.
    • Enter_setHead_ger in the Id field.
  7. In the Palette, open the Components drawer and drag a Log pattern ( Log icon ) to the canvas and drop it in the When_when/ger container.

    The When_when/ger container expands to accommodate the Log_log3 node.

  8. On the canvas, select the SetHeader_setHead_ger node and drag its connector arrow over the Log_log3 node, and then release it:

    tutCBRrte2LogGERAdd
  9. On the canvas, select the Log_log3 node to open its properties in the Properties view.
  10. In the Properties view:

    • Enter Valid order - ship animals to Germany customer in the Message field.
    • Enter _ger in the Id field.
    • Leave the Logging Level as is.
  11. From the Components drawer, drag a File pattern ( File icon ) to the canvas and drop it in the When_when/ger container.

    The When_when/ger container expands to accommodate the To_to1 node.

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

    tutCBRrte2GERtermFile
  13. On the canvas, select the To_to1 node to open its properties in the Properties view.
  14. In the Properties view:

    • Replace directoryName with target/messages/Germany in the Uri field.
    • Enter _GER in the Id field.
  15. On the menu bar, click FileSave to save the routing context file.

    The Germany branch of Route_route2 should look like this:

    tutGERbranchRte2

Building and configuring the France branch of the second route

With Route_route2 displayed on the canvas:

  1. In the Palette, open the *Routing drawer and drag an Otherwise pattern ( Otherwise icon ) to the canvas and drop it in the Choice_choice2 container:

    tutCBRrte2OtherFRAdd

    The Choice_choice2 container expands to accommodate the Otherwise_otherwise1 node.

  2. On the canvas, select the Otherwise_otherwise1 node to open its properties in the Properties view.
  3. In the Properties view, enter _else/fr in the Id field.
  4. In the Palette, open the Transformation drawer and drag a Set Header pattern ( Set Header icon ) to the canvas and drop it on the Otherwise_else/fr node:

    tutCBRrte2SetHeadFRAdd

    The Otherwise_else/fr container expands to accommodate the SetHeader_setHeader3 node.

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

    • Select constant from the Language drop-down menu.
    • Enter France in the Expression field.
    • Leave Trim as is.
    • Enter Destination in the Header Name field.
    • Enter_setHead_fr in the Id field.
  7. In the Palette, open the Components drawer and drag a Log pattern ( Log icon ) to the canvas and drop it in the Otherwise_else/fr container.

    The Otherwise_else/fr container expands to accommodate the Log_log3 node.

  8. On the canvas, select the SetHeader_setHead_fr node and drag its connector arrow over the Log_log3 node, and then release it:

    tutCBRrte2LogFRAdd
  9. On the canvas, select the Log_log3 node to open its properties in the Properties view.
  10. In the Properties view:

    • Enter Valid order - ship animals to France customer in the Message field.
    • Enter _fr in the Id field.
    • Leave the Logging Level as is.
  11. From the Components drawer, drag a File pattern ( File icon ) to the canvas and drop it in the Otherwise_else/fr container.

    The Otherwise_else/fr container expands to accommodate the To_to1 node.

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

    tutCBRrte2FRtermFile
  13. On the canvas, select the To_to1 node to open its properties in the Properties view.
  14. In the Properties view:

    • Replace directoryName with target/messages/France in the Uri field.
    • Enter _FR in the Id field.
  15. On the menu bar, click FileSave to save the routing context file.

    The France branch of Route_route2 should look like this:

    tutFRbranchRte2

Finishing up

  1. If needed, on the menu bar, select FileSave to save the routing context.

    The routes on the canvas should look like this:

    image::./images/tutCBRRte1Completed.png[Completed first route in the CBRroute routing context]

    image::./images/tutCBRRte2Completed.png[Completed second route in the CBRroute routing context]

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

    The camelContext element should look like that shown in Example 5.1, “XML for dual-route content-based router”:

    Example 5.1. XML for dual-route 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" shutdownRoute="Default">
              <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 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="_Fulfill" uri="direct:OrderFulfillment"/>
                  </otherwise>
              </choice>
          </route>
          <route id="_route2">
              <from id="_direct:OrderFulfillment" uri="direct:OrderFulfillment"/>
              <choice id="_choice2">
                  <when id="_when/usa">
                      <xpath>/order/customer/country = 'USA'</xpath>
                      <setHeader headerName="Destination" id="_setHead_usa">
                          <constant>USA</constant>
                      </setHeader>
                      <log id="_usa" message="Valid order - ship to USA customer"/>
                      <to id="_US" uri="file:target/messages/USA"/>
                  </when>
                  <when id="_when/gb">
                      <xpath>/order/customer/country = 'Great Britain'</xpath>
                      <setHeader headerName="Destination" id="_setHead_uk">
                          <constant>UK</constant>
                      </setHeader>
                      <log id="_uk" message="Valid order - ship animals to UK customer"/>
                      <to id="_UK" uri="file:target/messages/GreatBritain"/>
                  </when>
                  <when id="_when/ger">
                      <xpath>/order/customer/country = 'Germany'</xpath>
                      <setHeader headerName="Destination" id="_setHead_ger">
                          <constant>Germany</constant>
                      </setHeader>
                      <log id="_ger" message="Valid order - ship to Germany customer"/>
                      <to id="_GER" uri="file:target/messages/Germany"/>
                  </when>
                  <otherwise id="_else/fr">
                      <setHeader headerName="Destination" id="_setHead_fr">
                          <constant>France</constant>
                      </setHeader>
                      <log id="_fr" message="Valid order - ship animals to France customer"/>
                      <to id="_FR" uri="file:target/messages/France"/>
                  </otherwise>
              </choice>
          </route>
      </camelContext>
    </blueprint>
Important

If the tooling added the attribute shutdownRoute=" " to the second route element (<route id="route2">), delete that attribute. Otherwise, the CBRroute project might fail to run.

Next steps

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

Check the end of the Console’s output. You should see these lines:

tutCBRrte2Console

Check the target destinations in Project Explorer to verify that the routes executed properly:

  1. Select CBRroute.
  2. Right-click it to open the context menu, then select Refresh.
  3. Expand the folder target/messages/ as shown in Figure 5.1, “Target message destinations in Project Explorer”. The message*.xml files should be dispersed in your target destinations like this:

    Figure 5.1. Target message destinations in Project Explorer

    message destinations
    Note

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

Further reading

To learn more about the Direct component see the Red Hat JBoss Fuse: Apache Camel Component Reference at Red Hat JBoss Fuse 6.3 documentation