Search

Chapter 6. Adding another route to the routing context

download PDF

This tutorial shows you how to add a second route to the camel context in the ZooOrderApp project’s blueprint.xml file. The second route:

  • Takes messages (valid orders) directly from the terminal end of the first route’s otherwise branch.
  • Sorts the valid messages according to the customer’s country.
  • Sends each message to the corresponding country folder in the ZooOrderApp/target/messages folder. For example, an order from the Chicago zoo is copied to the USA folder.

Goals

In this tutorial you complete the following tasks:

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

Prerequisites

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

Reconfiguring the existing route’s endpoint

The existing route sends all valid orders to the target/messages/validOrders folder.

In this section, you reconfigure the endpoint of the existing route’s Otherwise _elseValid branch to instead connect to a second route (which you create in the next section).

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

  1. Open your ZooOrderApp/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 then select 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 Components Direct component, configuring it with the same property values as the repurposed To_Valid node.

To learn more about the Direct component see the Apache Camel Component Reference.

Adding the second route

To add another route to the routing context:

  1. In the Palette, open the Routing drawer and then click the Route ( Route icon ) pattern.
  2. In the canvas, click to the right of 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. Leave the properties as they are.
  4. Save the file.
Note

As your routing context grows in complexity, you might 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 that 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.

Configuring a Choice branch to process USA orders

In this section, you add a Choice branch to the route and configure the route to send USA orders to a new target/messages/validOrders/USA folder. You also set a message header and a log file component.

  1. In the Palette, open the Components drawer and then select the Direct component ( Direct icon ).
  2. In the canvas, click the Route_route2 container:

    The Route_route2 container expands to accommodate the Direct component (the From_from2 node):

    tutCBRrte2From2Add
  3. On the canvas, click the From_from2 node to open its properties in the Properties view.
  4. In the Uri field, replace name (following direct:) with OrderFulfillment, and in the Id field, enter _direct:OrderFulfillment.

    tutCBRrte2From2Props
  5. In the Palette, open the Routing drawer and then select the Choice ( Choice icon ) pattern.
  6. In the canvas, click the From _direct:OrderFulfillment node.

    The Route_route2 container expands to accommodate the Choice_choice2 node:

    tutCBRrte2Choice2Added

    In the Properties view, leave the Choice_choice2 node’s properties as they are.

  7. In the Palette, open the Routing drawer and then select the When ( When icon ) pattern.
  8. In the canvas, click the Choice_choice2 node.

    The Choice_choice2 container expands to accommodate the When_when2 node.

    tutCBRrte2When2Added
  9. On the canvas, select the When_when2 node to open its properties in the Properties view.
  10. Set the When_when2 node’s properties as follows:

    • Select xpath from the Expression drop-down list.
    • In the indented Expression field, type /order/customer/country = 'USA'.
    • Leave Trim enabled.
    • In the second Id field, type _when/usa

      tutCBRrte2WhenUSAprops
  11. In the Palette, open the Components drawer and then select the File component ( File icon ).
  12. In the canvas, click the When_when/usa container.

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

  13. In the Properties view:

    tutRte2EndptUSA
    • In the Uri field, replace directoryName with target/messages/validOrders/USA .
    • In the Id field, type _US.
  14. Save the file.

To set a message header and add a log component:

  1. In the Palette, open the Transformation drawer and then select the Set Header pattern.
  2. In the canvas, click the When_when/usa node.

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

    tutCBRrte2SetHeadUSAadd
  3. On the canvas, select the SetHeader_setHeader3 node to open its properties in the Properties view.
  4. Set the node’s properties as follows:

    • From the Expression drop-down menu, select constant.
    • In the indented Expression field, type: USA
    • Leave Trim enabled.
    • In the Header Name field, type: Destination
    • In the second Id field, type: _setHead_usa

      tutCRErte2SetHeadusaProps
  5. In the Palette, open the Components drawer and then select the Log component ( Log icon ).
  6. In the canvas, click above the SetHeader node.

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

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

    tutCBRrteLog3Props
  8. In the Properties view:

    • In the Message field, type Valid order - ship animals to USA customer .
    • In the Id field, type _usa.
    • Leave Logging Level as is.

      tutCBRrte2Log3usaAdded
  9. Save the file.

    The USA branch of Route_route2 should look like this:

    tutUSAbranchRte2

Configuring an Otherwise branch to process Germany orders

With Route_route2 displayed on the canvas:

  1. In the Palette, open the Routing drawer and then select the Otherwise pattern ( Otherwise icon ).
  2. In the canvas, click the Choice_choice2 container.

    The Choice_choice2 container expands to accommodate the Otherwise_otherwise1 node.

    tutCBRrte2WhenGERAdd
  3. Select the Otherwise_otherwise1 node to open its properties in the Properties view.
  4. In the Properties view, enter _else/ger for the Id field.
  5. In the Palette, open the Transformation drawer and then select the Set Header pattern ( Set Header icon ).
  6. In the canvas, click the Otherwise_else/ger node.

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

    tutCBRrte2SetHeadGERAdd
  7. On the canvas, select the SetHeader_setHeader3 node to open its properties in the Properties view.
  8. In the Properties view:

    • From the Expression drop-down list, select constant.
    • In the second Expression field, type Germany.
    • Leave Trim as is.
    • In the Header Name field, type Destination.
    • In the second Id field, type _setHead_ger.
  9. In the Palette, open the Components drawer and then select the Log pattern ( Log icon ).
  10. In the canvas, click below the SetHeader_setHead_ger node.

    The Otherwise_else/ger container expands to accommodate the Log_log3 node. If needed, drag the connector error from the Log_log3 node to the SetHeader_setHead_ger node:

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

    • In the Message field, type Valid order - ship animals to Germany customer.
    • In the Id field, type _ger.
    • Leave the Logging Level as is.
  13. In the Components drawer, select a File pattern ( File icon ) and then click below the Log_ger node.

    The Otherwise_else/ger container expands to accommodate the To_to1 node. If needed, drag the connector error from the SetHeader_setHead_ger node to the To_to1 node:

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

    • In the Uri field, replace directoryName with target/messages/validOrders/Germany
    • In the Id field, type _GER.
  16. Save the file.

The Germany branch of Route_route2 should look like this:

tutGERbranchRte2

Verifying the second route

The routes on the canvas should look like this:

Completed route1

Completed first route in the ZooOrderApp routing context

Completed route2

Completed second route in the ZooOrderApp routing context

In the Source tab at the bottom of the canvas, the XML for the camelContext element should look like that shown in Example 6.1, “XML for dual-route content-based router”:

Example 6.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() &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="_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>
                   <log id="_usa" message="Valid order - ship animals to USA customer"/>
                   <setHeader headerName="Destination" id="_setHead_usa">
                       <constant>USA</constant>
                   </setHeader>
                   <to id="_US" uri="file:target/messages/validOrders/USA"/>
               </when>
               <otherwise id="_else/ger">
                   <log id="_ger" message="Valid order - ship animals to Germany customer"/>
                   <setHeader headerName="Destination" id="_setHead_ger">
                       <constant>Germany</constant>
                   </setHeader>
                   <to id="_GER" uri="file:target/messages/validOrders/Germany"/>
               </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 ZooOrderApp project might fail to run.

To make sure that your updated project works as expected, follow these steps:

  1. Run the ZooOrderApp/Camel Contexts/blueprint.xml as a local Camel Context (without tests).
  2. Check the end of the Console’s output. You should see these lines:

    tutCBRrte2Console
  3. Check the target destination folders to verify that the routes executed properly:

    1. In Project Explorer, right-click ZooOrderApp and then select Refresh.
    2. Expand the target/messages/ folder.

      The message*.xml files should be dispersed in your the destinations as shown:

      Figure 6.1. Target message destinations in Project Explorer

      message destinations

Next steps

In the next tutorial, Chapter 7, Debugging a routing context, you learn how to use the Fuse Tooling debugger.

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.