Chapter 6. To Debug a Routing Context


Abstract

The Camel debugger works only on the routing context. The routing context and each node with a breakpoint set must have a unique ID. You can set them manually or let the tooling set them automatically.

Goals

In this tutorial you will:
  • In Source view, enter a unique ID for the camelContext element
  • In Design view, set breakpoints on the nodes of interest in Route1
  • Switch to Route2, and set breakpoints on the nodes of interest
  • Invoke the Camel debugger
  • Step through the route, examining route and message variables as they change
  • Step through the route again, changing the value of message variables and observing the effects

Prerequisites

To complete this tutorial you will need the CBRroute project you updated in Chapter 5, To Add Another Route to the CBR Routing Context.
Note
If you skipped any tutorial after Chapter 2, To Create a New Route you can use the prefabricated camelContext6.xml file to work through this tutorial (for details, see Chapter 1, Using the Fuse Tooling Resource Files).

Setting breakpoints

You can set both conditional and unconditional breakpoints, but in this tutorial, you will set unconditional breakpoints only.
  1. If necessary, open your CBRroute/src/main/resources/OSGI-INF/blueprint/camelContext.xml in the route editor.
    By default, the route editor displays Route1 on the canvas.
  2. Click the Source tab at the bottom of the route editor to switch to Source view.
  3. In the camelContext element, add id="blueprintContext" like this:
    <camelContext trace="false" id="blueprintContext" 
          xmlns="http://camel.apache.org/schema/blueprint">
  4. Click File Save to save your routing context file.
  5. Click the Design tab at the bottom of the route editor to switch to Design view.
  6. Select the choice1 node, and then click its red icon icon to set an unconditional breakpoint.
    Note
    In the route editor, you can disable or delete a specific breakpoint by clicking the node's gray icon icon or its delete icon icon, respectively. You can delete all set breakpoints by right-clicking the canvas and selecting Delete all breakpoints.
    Note
    If you had not already set unique ID on the camelContext element and the nodes in your routing context, the Please Confirm dialog would have appeared now prompting you to let the tooling do it for you.
  7. Repeat Step 6 to set an unconditional breakpoint on the following Route1 nodes:
    • log1
    • setHead1
    • toInvalid
    • log2
    • setHead2
    • toFulfill
  8. Click File Save to save your routing context file.
  9. Click Routes Route2 to open Route2 on the canvas.
  10. Repeat Step 6 to set an unconditional breakpoint on the following Route2 nodes:
    • choice2
    • setHead_usa
    • log_usa
    • toUSA
    • setHead_uk
    • log_uk
    • toUK
    • setHead_ger
    • log_ger
    • toGR
    • setHead_fr
    • log_fr
    • toFR
  11. Click File Save to save your routing context file.
Note
Each time you modify your routing context, be sure to save it before you invoke the Camel debugger, otherwise the changes you made will not be included in the debugging session.

Stepping through the CBRroute routing context

You can step through the routing context in two ways:
  • Step over ( Step Over icon )—Jumps to the next node of execution in the routing context, regardless of breakpoints.
  • Resume ( Resume icon )—Jumps to the next active breakpoint in the routing context.
Note
You can temporarily narrow then later re-expand the debugger's focus by disabling and re-enabling the breakpoints you set in the routing context. This enables you, for example, to focus on problematic nodes in your routing context. To do so, open the Breakpoints tab and clear the check box of each breakpoint you want to temporarily disable. Then use Resume icon to step through the route. It will skip over the disabled breakpoints.
  1. In Project Explorer, expand the root node CBRroute to expose the camelContext.xml file in the Camel Contexts folder.
  2. Right-click the camelContext.xml file to open its context menu, and then click Debug As... Local Camel Context (without tests).
    Note
    If you select Local Camel Context, it will fail to run because you have not yet created a JUnit test for the CBRroute project. You will do that later in Chapter 8, To Test a Route with JUnit.
    The Camel debugger suspends execution at the first breakpoint it encounters and asks whether you want to open Debug perspective now.
  3. Click Yes.
    Note
    If you click No, the confirmation pane appears several more times. After the third refusal, it disappears, and the Camel debugger resumes execution. To interact with the debugger at this point, you need to open the Debug perspective by clicking Window Open Perspective Debug.
    Debug perspective opens with the routing context suspended at choice1 in Route1 [camelContext] as shown in Debug view.
    Note
    Breakpoints are held for a maximum of five minutes before the debugger automatically resumes, moving on to the next breakpoint or to the end of the routing context, whichever comes next.
  4. In Variables view, expand the nodes to expose the variables and values available for each node.
    As you step through the routing context, the variables whose values have changed since the last breakpoint are highlighted in yellow. You may need to expand the nodes at each breakpoint to reveal variables that have changed.
  5. Click Resume icon to step to the next breakpoint, log2 in Route1 [camelContext].
  6. Expand the nodes in Variables view to examine the variables that have changed since the last breakpoint at choice1 in Route1 [camelContext].
  7. Click Resume icon to step to the next breakpoint, setHead2 in Route1 [camelContext].
    Examine the variables that changed since the breakpoint at log2 in Route1 [camelContext].
  8. In Debug view, click log2 in Route1 [camelContext] to populate Variables view with the variable values from the breakpoint log2 in Route1 [camelContext] for a quick comparison.
    In Debug view, you can switch between breakpoints within the same message flow to quickly compare and monitor changing variable values in Variables view.
    Note
    Message flows can vary in length. For messages that transit the invalidOrders branch of Route1, the message flow is short. For messages that transit the validOrders branch of Route1, which continues on to Route2, the message flow is longer.
  9. Continue stepping through the routing context. When one message completes the routing context and the next message enters it, the new message flow appears in Debug view, tagged with a new breadcrumb ID.
    In this case, ID-janemurpheysmbp-home-55986-1423155548173-0-3 identifies the second message flow, corresponding to message2.xml having entered the routing context. Breadcrumb IDs are incremented by 2.
    Note
    Exchange and Message IDs are identical and remain unchanged throughout a message's passage through the routing context. Their IDs are constructed from the message flow's breadcrumb ID, and incremented by 1. So, in the case of message2.xml, its ExchangeId and MessageId are ID-janemurpheysmbp-home-55986-1423155548173-0-4.
  10. When message3.xml enters the breakpoint choice1 in Route1 [camelContext], examine the Processor variables. The values displayed are the metrics accumulated for message1.xml and message2.xml, which previously transited the routing context.
    Timing metrics are in milliseconds.
  11. Continue stepping each message through the routing context, examining variables and console output at each processing step. When message6.xml enters the breakpoint toGR in Route2 [camelContext], the debugger begins shutting down the breadcrumb threads.
  12. In the Menu bar, click Terminate icon to terminate the Camel debugger. This will cause the Console to terminate, but you will have to manually clear the output.
    Note
    With a thread or endpoint selected under the Camel Context node in Debug view, you need to click Terminate icon twice—first to terminate the thread or endpoint and second to terminate the Camel Context, thus the session.
  13. In the Menu bar, right-click to open the context menu, and then select Close to close Debug perspective.
    Doing so automatically returns you to JBoss perspective.
  14. In Project Explorer, open the project's context menu, and select Refresh to refresh the display.
    Note
    If you terminated the session prematurely, before all messages transited the routing context, you might see, under the CBRroute/CamelContexts/ folder, a file that looks like this:target/.CamelContextInDebug_xxxxxxxxxxxxxxxxxx_temp/camelContext.xml. To remove it, open Project Explorer's context menu and click Refresh.
  15. Expand the CBRroute/target/messages/* directories to check that the messages were delivered to their expected destinations:
  16. Leave the routing context as is, with all previous breakpoints set and enabled.

Changing the value of a variable

In this session, you will add variables to a watch list to easily check how their values change as messages pass through the routing context. You will also change the value of a variable in the body of two messages and observe how the change affects each message's route through the routing context.
Follow Step 1 through Step 3 in the section called “Stepping through the CBRroute routing context” to rerun the Camel debugger on the CBRroute project.
  1. With message1 stopped at the first breakpoint, choice1 in Route1 [camelContext.xml], add the variables NodeId and RouteId (in the Exchange category) and MessageBody and CamelFileName (in the Message category) to the watch list.
    For each of the four variables:
    1. In Variables view, expand the appropriate category to expose the target variable:
    2. Right-click the variable (in this case, NodeId in the Exchange category) to open the context menu and select Watch:
      The Expressions tab opens, listing the variable you selected to watch:
    3. Repeat Step 1.b for each of the three remaining variables.
    4. Switch back to Variables view.
  2. Step message1 through the routing context until it reaches the fourth breakpoint, toFulfill in Route1 [camelContext.xml].
  3. In Variables view, expand the Message category.
  4. Repeat Step 1.b to add the variable Destination to the watch list.
    Expressions view should now contain these variables:
    Note
    The pane below the list of variables displays the value of the selected variable.
    Note
    Expressions view retains all variables you add to the list until you explicitly remove them.
  5. Step message1 through the rest of the routing context.
  6. Stop message2 at choice1 in Route1 [camelContext.xml].
  7. In Variables view, expand the Message category to expose the MessageBody variable.
  8. Right-click MessageBody to open its context menu, and select Change Value....
  9. Change the value of quantity from 3 to 2.
    This changes the in-memory value only.
  10. Click OK.
  11. Switch to Expressions view, and select the MessageBody variable.
    The pane below the list of variables displays the entire body of message2, making it easy to check the current value of order items:
    Note
    Creating a watch list makes it easy for you to quickly check the current value of multiple variables of interest.
  12. Click Resume icon to step to the next breakpoint.
    Instead of following the branch leading to InvalidOrders, message2 now follows the branch leading to toFulfill.
  13. Step message2 through the routing context, checking Debug view, Variables view, and Console output at each step.
  14. Stop message3 at choice1 in Route1 [camelContext.xml].
  15. Switch to Breakpoints view, and disable all breakpoints (12) between choice1 and toFulfill:
  16. Switch back to Variables view.
  17. Click Resume icon to step to the next breakpoint.
    The debugger jumps to toFulFill in Route1 [camelContext.xml].
  18. Click Resume icon again to step to the next breakpoint.
    The debugger jumps to toUK in Route2 [camelContext.xml].
  19. Switch to Breakpoints view, and re-enable all disabled breakpoints.
  20. Switch back to Variables view.
  21. Click Resume icon to step to the next breakpoint, and stop message4 at choice1 in Route1 [camelContext.xml].
  22. Right-click MessageBody to open its context menu, and select Change Value....
  23. Change the value of quantity from 5 to 4.
  24. Click OK.
  25. Switch to Expressions view, and select the MessageBody variable to check the value of quantity in the body of message4.
  26. Repeat Step 12 and Step 13 to step message4 through the routing context.
  27. Click Resume icon repeatedly to quickly step message5 and message6 through the routing context.
  28. In the Menu bar, click Terminate icon to terminate the Camel debugger.
    This will also cause the Console to terminate, but you will have to click its Clear output icon button to clear the output.
  29. In the Menu bar, right-click to open the context menu, and then select Close to close Debug perspective.
    Doing so automatically returns you to JBoss perspective.
  30. In Project Explorer, open the project's context menu, and select Refresh to refresh the display.
  31. Expand the CBRroute/target/messages/* directories to check whether the messages were delivered as expected:
    You should see that no messages were sent to the invalidOrders folder. Instead, message2.xml should appear in the USA folder, and message4.xml should appear the GreatBritain folder.

Next steps

Next you will trace messages through your routing context to see where you can optimize and fine tune your routing context's performance, as described in Chapter 7, To Trace a Message Through a Route.
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.