Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.此内容没有您所选择的语言版本。
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
countryfolder in theCBRroute/target/messagesfolder.
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.
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:
-
Open your
CBRroute/src/main/resources/OSGI-INF/blueprint/blueprint.xmlin the route editor. -
On the canvas, select the
Route_route1container to open its properties in the Properties view. -
Scroll down to the Shutdown Route property and enter
Default. -
On the canvas, select the terminal file node
To_Validto display its properties in the Properties view. -
In the Uri field, delete the existing text, and then enter
direct:OrderFulfillment. -
In the Id field, enter
_Fulfill.
Instead of repurposing the existing To_Valid terminal file node, you could have replaced it with a To_Valid node.
Adding the second route 复制链接链接已复制到粘贴板!
To add a route to the routing context:
-
In the Palette, open the Routing drawer and select the Route (
) pattern.
Drag the Route pattern to the canvas and drop it next to the
Route_route1container:The Route pattern becomes the
Route_route2container node on the canvas.-
Click the
Route_route2container node to display its properties in the Properties view. - Leave each of the properties as is.
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:
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.
In the Palette, open the Components drawer and drag a Direct component (
) to the canvas and drop it in the Route_route2container:The
Route_route2container expands to accommodate theFrom_from2node.On the canvas, select the
From_from2node to open its properties in the Properties view:-
In the Uri field, replace name (following
direct:) withOrderFulfillment, and in the Id field, enter_direct:OrderFulfillment. In the Palette, open the Routing drawer and drag a Choice (
) pattern to the canvas and drop it in the Route_route2container.The
Route_route2container expands to accommodate theChoice_choice2node.In the
Route_route2container, select thedirect:OrderFulfillmentnode and drag its connector arrow over theChoice_choice2node, then release it:-
In the Properties view, leave each of the
Choice_choice2node’s properties as is. In the Palette, open the Routing drawer and drag a When (
) pattern to the canvas and drop it in the Choice_choice2container:The
Choice_choice2container expands to accommodate theWhen_when2node.On the canvas, select the
When_when2node to open its properties in the Properties view:Set the node’s properties this way:
- Select xpath from the Language drop-down menu.
-
Enter
/order/customer/country = 'USA'in the Expression field. - Leave Trim enabled.
Enter
_when/usain the Id field.NoteOnce 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
Whennode.
In the Palette, open the Transformation drawer and drag the Set Header pattern to the canvas and drop on the
When_when/usanode:The
When_when/usacontainer expands to accommodate theSetHeader_setHeader3node.On the canvas, select the
SetHeader_setHeader3node to open its properties in the Properties view:Set the node’s properties this way:
- Select constant from the Language drop-down menu.
-
Enter
USAin the Expression field. - Leave Trim enabled.
-
Enter
Destinationin the Header Name field. -
Enter
_setHead_usain the Id field.
In the Palette, open the Components drawer and drag a Log component (
) to the canvas and drop it in the When_when/usacontainer.The
When_when/usacontainer expands to accommodate theLog_log3node.On the canvas, select the
SetHeader_setHead_usanode and drag its connector arrow over theLog_log3node, then release it:On the canvas, select the
Log_log3node to open its properties in the Properties view:In the Properties view:
-
Enter
Valid order - ship animals to USA customerin the Message field. -
Enter
_usain the Id field. - Leave Logging Level as is.
-
Enter
In the Palette, open the Components drawer and drag a File component (
) to the canvas and drop it in the When_when/usacontainer.The
When_when/usacontainer expands to accommodate theTo_to1node.On the canvas, select the
Log_usanode and drag its connector arrow over theTo_to1node, then release it:In the Properties view:
-
Replace directoryName with
target/messages/USAin the Uri field. -
Enter
_USin the Id field.
-
Replace directoryName with
On the menu bar, click
to save the routing context file. The USA branch of
Route_route2should look like this:
With Route_route2 displayed on the canvas:
In the Palette, open the Routing drawer and drag a When pattern (
) to the canvas and drop it in the Choice_choice2container:The
Choice_choice2container expands to accommodate theWhen_when2node.-
On the canvas, select the
When_when2node to open its properties in the Properties view. 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/gbin the Id field.
In the Palette, open the Transformation drawer and drag a Set Header pattern (
) to the canvas and drop it on the When_when/gbnode:The
When_when/gbcontainer expands to accommodate theSetHeader_setHeader3node.-
On the canvas, select the
SetHeader_setHeader3node to open its properties in the Properties view. In the Properties view:
- Select constant from the Language drop-down menu.
-
Enter
UKin the Expression field. - Leave Trim as is.
-
Enter
Destinationin the Header Name field. -
Enter
_setHead_ukin the Id field.
In the Palette, open the Components drawer and drag a Log pattern (
) to the canvas and drop it in the When_when/gbcontainer.The
When_when/gbcontainer expands to accommodate theLog_log3node.On the canvas, select the
SetHeader_setHead_uknode and drag its connector arrow over theLog_log3node, and then release it:-
On the canvas, select the
Log_log3node to open its properties in the Properties view. In the Properties view:
-
Enter
Valid order - ship animals to UK customerin the Message field. -
Enter
_ukin the Id field. - Leave the Logging Level as is.
-
Enter
From the Components drawer, drag a File pattern (
) to the canvas and drop it in the When_when/gbcontainer.The
When_when/gbcontainer expands to accommodate theTo_to1node.On the canvas, select the
Log_uknode and drag its connector arrow over theTo_to1node, and then release it:-
On the canvas, select the
To_to1node to open its properties in the Properties view. In the Properties view:
-
Replace directoryName with
target/messages/GreatBritainin the Uri field. -
Enter
_UKin the Id field.
-
Replace directoryName with
On the menu bar, click
to save the routing context file. The Great Britain branch of
Route_route2should look like this:
With Route_route2 displayed on the canvas:
In the Palette, open the Routing drawer and drag a When pattern (
) to the canvas and drop it in the Choice_choice2container:The
Choice_choice2container expands to accommodate theWhen_when2node.-
On the canvas, select the
When_when2node to open its properties in the Properties view. 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/gerin the Id field.
In the Palette, open the Transformation drawer and drag a Set Header pattern (
) to the canvas and drop it on the When_when/gernode:The
When_when/gercontainer expands to accommodate theSetHeader_setHeader3node.-
On the canvas, select the
SetHeader_setHeader3node to open its properties in the Properties view. In the Properties view:
- Select constant from the Language drop-down menu.
-
Enter
Germanyin the Expression field. - Leave Trim as is.
-
Enter
Destinationin the Header Name field. -
Enter
_setHead_gerin the Id field.
In the Palette, open the Components drawer and drag a Log pattern (
) to the canvas and drop it in the When_when/gercontainer.The
When_when/gercontainer expands to accommodate theLog_log3node.On the canvas, select the
SetHeader_setHead_gernode and drag its connector arrow over theLog_log3node, and then release it:-
On the canvas, select the
Log_log3node to open its properties in the Properties view. In the Properties view:
-
Enter
Valid order - ship animals to Germany customerin the Message field. -
Enter
_gerin the Id field. - Leave the Logging Level as is.
-
Enter
From the Components drawer, drag a File pattern (
) to the canvas and drop it in the When_when/gercontainer.The
When_when/gercontainer expands to accommodate theTo_to1node.On the canvas, select the
Log_gernode and drag its connector arrow over theTo_to1node, and then release it:-
On the canvas, select the
To_to1node to open its properties in the Properties view. In the Properties view:
-
Replace directoryName with
target/messages/Germanyin the Uri field. -
Enter
_GERin the Id field.
-
Replace directoryName with
On the menu bar, click
to save the routing context file. The Germany branch of
Route_route2should look like this:
With Route_route2 displayed on the canvas:
In the Palette, open the *Routing drawer and drag an Otherwise pattern (
) to the canvas and drop it in the Choice_choice2container:The
Choice_choice2container expands to accommodate theOtherwise_otherwise1node.-
On the canvas, select the
Otherwise_otherwise1node to open its properties in the Properties view. -
In the Properties view, enter
_else/frin the Id field. In the Palette, open the Transformation drawer and drag a Set Header pattern (
) to the canvas and drop it on the Otherwise_else/frnode:The
Otherwise_else/frcontainer expands to accommodate theSetHeader_setHeader3node.-
On the canvas, select the
SetHeader_setHeader3node to open its properties in the Properties view. In the Properties view:
- Select constant from the Language drop-down menu.
-
Enter
Francein the Expression field. - Leave Trim as is.
-
Enter
Destinationin the Header Name field. -
Enter
_setHead_frin the Id field.
In the Palette, open the Components drawer and drag a Log pattern (
) to the canvas and drop it in the Otherwise_else/frcontainer.The
Otherwise_else/frcontainer expands to accommodate theLog_log3node.On the canvas, select the
SetHeader_setHead_frnode and drag its connector arrow over theLog_log3node, and then release it:-
On the canvas, select the
Log_log3node to open its properties in the Properties view. In the Properties view:
-
Enter
Valid order - ship animals to France customerin the Message field. -
Enter
_frin the Id field. - Leave the Logging Level as is.
-
Enter
From the Components drawer, drag a File pattern (
) to the canvas and drop it in the Otherwise_else/frcontainer.The
Otherwise_else/frcontainer expands to accommodate theTo_to1node.On the canvas, select the
Log_frnode and drag its connector arrow over theTo_to1node, and then release it:-
On the canvas, select the
To_to1node to open its properties in the Properties view. In the Properties view:
-
Replace directoryName with
target/messages/Francein the Uri field. -
Enter
_FRin the Id field.
-
Replace directoryName with
On the menu bar, click
to save the routing context file. The France branch of
Route_route2should look like this:
Finishing up 复制链接链接已复制到粘贴板!
If needed, on the menu bar, select
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]
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
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
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:
Check the target destinations in Project Explorer to verify that the routes executed properly:
-
Select
CBRroute. - Right-click it to open the context menu, then select Refresh.
Expand the folder
target/messages/as shown in Figure 5.1, “Target message destinations in Project Explorer”. Themessage*.xmlfiles should be dispersed in your target destinations like this:Figure 5.1. Target message destinations in Project Explorer
NoteTo 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