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


Abstract

This tutorial walks you through adding a second route to the camelContext.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 CBRroute/target/messages/<country> directory

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 camelContext5.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/camelContext.xml in the route editor.
  2. Click the canvas to display the existing route's properties in the Properties editor.
  3. Enter Route1 in the Id field.
  4. Select the terminal file: node file:target/messages/toValid to display its properties in the Properties editor.
  5. In the Uri field, delete the existing text, and then enter direct:OrderFulfillment.
  6. In the Id field, enter toFulfill.

Adding the second route

Note
The route editor displays each route in a multiroute routing context on its own slice of canvas.
To add a route to the routing context:
  1. Select Routes Add Route.
    The tooling adds another route to your camelContext, and the route editor opens a clean canvas for you to construct the second route.
    Outline view, shown here, displays both routes and their components. Clicking on a route in Outline view displays it on the route editor's canvas.
    Or you can switch between routes by selecting Routes Route:RouteName on the menu bar, where RouteName is the string you entered in the route's Id field in the Properties editor.
  2. Click the canvas to display the new route's properties in the Properties editor.
  3. Enter Route2 in the Id field.

Building and configuring the USA branch of the second route

With Route2 displayed on the route editor's canvas:
  1. Drag an Generic element ( endpoint icon ) from the Palette's Components drawer onto the canvas.
  2. In the Properties editor, enter direct:OrderFulfillment in the Uri field.
  3. Right-click the direct:OrderFulfi... node to open the context menu, and select Add Routing Choice.
  4. In the Properties editor, enter choice2 in the Id field.
  5. Right-click the choice2 node to open the context menu, and select Add Routing When.
  6. In the Properties editor:
    • Enter /order/customer/country = 'USA' in the Expression field.
    • Select xpath from the Language drop-down menu.
    • Enter when/usa in the Id field.
  7. Right-click the when/usa node to open the context menu, and select Add Transformation SetHeader.
  8. In the Properties editor:
    • Enter Destination in the Header Name field.
    • Enter USA in the Expression field.
    • Select constant from the Language drop-down menu.
    • Enter setHead_usa in the Id field,
  9. Right-click the setHead_usa node to open the context menu, and select Add Components Log.
  10. In the Properties editor:
    • Enter Valid order - ship animals to USA customer in the Message field.
    • Enter log_usa in the Id field.
  11. Right-click the log_usa node to open the context menu, and select Add Components File.
  12. In the Properties editor:
    • Replace directoryName with target/messages/USA in the Uri field.
    • Enter toUS in the Id field.
The USA branch of Route2 should look like this:

Building and configuring the Great Britain branch of the second route

With Route2 displayed on the canvas:
  1. Right-click the choice2 node again to open the context menu, and select Add Routing When.
  2. In the Properties editor:
    • Enter /order/customer/country = 'Great Britain' in the Expression field.
    • Select xpath from the Language drop-down menu.
    • Enter when/gb in the Id field.
  3. Right-click the when/gb node to open the context menu, and select Add Transformation SetHeader.
  4. In the Properties editor:
    • Enter Destination in the Header Name field.
    • Enter UK in the Expression field.
    • Select constant from the Language drop-down menu.
    • EntersetHead_uk in the Id field,
  5. Right-click the setHead_uk node to open the context menu, and select Add Components Log.
  6. In the Properties editor:
    • Enter Valid order - ship animals to UK customer in the Message field.
    • Enter log_uk in the Id field.
  7. Right-click the log_uk node to open the context menu, and select Add Components File.
  8. In the Properties editor:
    • Replace directoryName with target/messages/GreatBritain in the Uri field.
    • Enter toUK in the Id field.
The Great Britain branch of Route2 should look like this:

Building and configuring the Germany branch of the second route

With Route2 displayed on the canvas:
  1. Right-click the choice2 node again to open the context menu, and select Add Routing When.
  2. In the Properties editor:
    • Enter /order/customer/country = 'Germany' in the Expression field.
    • Select xpath from the Language drop-down menu.
    • Enter when/ger in the Id field.
  3. Right-click the when/ger node to open the context menu, and select Add Transformation SetHeader.
  4. In the Properties editor:
    • Enter Destination in the Header Name field.
    • Enter Germany in the Expression field.
    • Select constant from the Language drop-down menu.
    • Enter setHead_ger in the Id field,
  5. Right-click the setHead_ger node to open the context menu, and select Add Components Log.
  6. In the Properties editor:
    • Enter Valid order - ship animals to Germany customer in the Message field.
    • Enter log_ger in the Id field.
  7. Right-click the log_ger node to open the context menu, and select Add Components File.
  8. In the Properties editor:
    • Replace directoryName with target/messages/Germany in the Uri field.
    • Enter toGR in the Id field.
The Germany branch of Route2 should look like this:

Building and configuring the France branch of the second route

With Route2 displayed on the canvas:
  1. Right-click the choice2 node again to open the context menu, and select Add Routing Otherwise.
  2. In the Properties editor:
    Enter else/fr in the Id field.
  3. Right-click the else/fr node to open the context menu, and select Add Transformation SetHeader.
  4. In the Properties editor:
    • Enter Destination in the Header Name field.
    • Enter France in the Expression field.
    • Select constant from the Language drop-down menu.
    • Enter setHead_fr in the Id field,
  5. Right-click the setHead_fr node to open the context menu, and select Add Components Log.
  6. In the Properties editor:
    • Enter Valid order - ship animals to France customer in the Message field.
    • Enter log_fr in the Id field.
  7. Right-click the log_fr node to open the context menu, and select Add Components File.
  8. In the Properties editor:
    • Replace directoryName with target/messages/France in the Uri field.
    • Enter toFR in the Id field.
The France branch of Route2 should look like this:

Saving the new routing context

  1. On the toolbar, select File Save to save the routing context.
    The routes on the canvas should look like this:

    Figure 5.1. Completed route1

    Completed first route in the CBRroute routing context

    Figure 5.2. Completed route2

    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.

    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: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 id="Route1">
        <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="direct:OrderFulfillment" id="toFulfill"/>
          </otherwise>
        </choice>
      </route>
      <route id="Route2">
        <from 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 message="Valid order - ship animals to USA customer" id="log_usa"/>
            <to uri="file:target/messages/USA" id="toUS"/>
          </when>
          <when id="when/gb">
            <xpath>/order/customer/country = 'Great Britain'</xpath>
            <setHeader headerName="Destination" id="setHead_uk">
              <constant>UK</constant>
            </setHeader>
            <log message="Valid order - ship animals to UK customer" id="log_uk"/>
            <to uri="file:target/messages/GreatBritain" id="toUK"/>
          </when>
          <when id="when/ger">
            <xpath>/order/customer/country = 'Germany'</xpath>
            <setHeader headerName="Destination" id="setHead_ger">
              <constant>Germany</constant>
            </setHeader>
            <log message="Valid order - ship animals to Germany customer" id="log_ger"/>
            <to uri="file:target/messages/Germany" id="toGR"/>
          </when>
          <otherwise id="else/fr">
            <setHeader headerName="Destination" id="setHead_fr">
              <constant>France</constant>
            </setHeader>
            <log message="Valid order - ship animals to France customer" id="log_fr"/>
            <to uri="file:target/messages/France" id="toFR"/>
          </otherwise>
        </choice>
      </route>
     </camelContext>
    </blueprint>

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:
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.3. The message*.xml files should be dispersed in your target destinations like this:

    Figure 5.3. Target message destinations in Project Explorer

    message destinations
    Note
    To view message content, double-click a message to open it in the route editor'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.x documentation
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.