About the Error Handler sample

The Error Handler sample is based on a scenario where a business that is involved in processing transactions wants to develop a routine for handling errors. The sample demonstrates how to use some of the features that are provided in IBM Integration Bus.

Businesses such as banks want to make sure that transactions are processed once and only once, and that there is a record of any errors. In the Error Handler sample, you put messages about staff numbers through a message flow that updates a database with this information. By putting in a message with an invalid staff number, you can observe how the error handling routine works.

The Error Handler sample demonstrates the following tasks:

The messages

Two input messages are supplied for running the Error Handler sample:

The valid staff message is shown in the following XML code:

<Staff>
   <StaffNumber>1</StaffNumber>
   <NameInfo>
      <LastName>Smith</LastName>
      <FirstName>Jack</FirstName>
   </NameInfo>
</Staff>

The invalid staff message is shown in following XML code:

<Staff>
   <StaffNumber>99</StaffNumber>
   <NameInfo>
      <LastName>Doe</LastName>
      <FirstName>Jane</FirstName>
   </NameInfo>
</Staff>

 

The message flows

The following diagram shows the main message flow.

A screen capture of the main message flow.

The following diagram shows the error handling subflow.

A screen capture of the error handling subflow.

The following table lists the types of nodes that are used in the Error Handler sample. The Subflow node is not technically a node and it is not available in the node palette; the Subflow node represents where the subflow, Error_Handler.msgflow, is called within the main message flow.

Node type Node name
MQInput STAFF_IN
MQOutput STAFF_FAIL; STAFF_OUT; STAFF_UPDATE_ERROR
Compute Copy Message; Create Message for Output
Filter Check Valid Staff Number; Check Backout Count
Throw Throw; Throw to Complete Rollback
TryCatch TryCatch
Subflow Error_Handler

For more information, see Built-in nodes and Using subflows in the IBM Integration Bus documentation.

The route taken by the valid staff message

When you put the valid staff message on the input queue the message travels through the nodes. If any of the queues are disabled, the message cannot follow this path.

  1. In the main message flow:
    1. STAFF_IN node. This node gets the input message from the input queue.
    2. Error_Handler node. This node represents the error handling subflow. The message leaves the main message flow, and enters the subflow.
  2. In the subflow:
    1. Start Subflow node. The message is passed to the next node in the flow.
    2. Check Backout Count node. This node checks whether the backout count is zero. Because it is the first time that the input message is passed through this node, the count is currently zero, and the message is passed to the next node. If the count is more than zero, the message is discarded.
    3. TryCatch node. Because the staff number is valid, the message is passed to the Back To Main Flow node. If the staff number in the message is invalid and the processing of the message has discovered this invalid staff number, the message is passed to the Copy Message and STAFF_UPDATE_ERROR node.
    4. Back To Main Flow node. The message leaves the subflow and returns to the main message flow.
  3. In the main message flow:
    1. Check Valid Staff Number node. Because the staff number is valid, the message is passed to the Update Staff Database node.
    2. Update Staff Database node. The STAFFDB database is updated with the staff details in the message.
    3. STAFF_OUT node. This node puts the message on the STAFF_OUT queue.
    4. If you do not have a database, the message is still put onto the queue through the Failure terminal. This scenario demonstrates this sample without the use of a database and must not be used in production.

The route taken by the invalid staff message

When you put the invalid staff message on the input queue, the message travels through the nodes as described later in this section. If any of the queues are disabled, the message cannot follow this path. For more information about what each node does, see Built-in nodes in the IBM Integration Bus documentation.

  1. In the main message flow:
    1. STAFF_IN node. This node gets the input message from the input queue.
    2. Error_Handler node. This node represents the error handling subflow. The message leaves the main message flow, and enters the subflow.
  2. In the subflow:
    1. Start Subflow node. The message is passed to the next node in the flow.
    2. Check Backout Count node. This node checks whether the backout count is zero. Because this is the first time that the input message passes through this node, the count is currently zero, and the message is passed to the next node. Compare to step 5b later in this section.
    3. TryCatch node. The staff number in the message is invalid, but this has not yet been discovered. The input message is passed to the Back To Main Flow node, rather than to the STAFF_UPDATE_ERROR node.
    4. Back To Main Flow node. The message leaves the subflow, and returns to the main message flow.
  3. In the main message flow:
    1. Check Valid Staff Number node. Because the staff number is invalid, the message is passed to the Throw node, rather than to the Update Staff Database node.
    2. Throw node. This node stops the processing of the message and records the error number "3001", and the text "Invalid staff number" to the message content. The message is rolled back, that is, the message is propagated back to the point of control, which in this case is the TryCatch node in the subflow.
  4. In the subflow:
    1. TryCatch node. This node recognises that an exception has occurred in the processing of the message and passes the message to the STAFF_UPDATE_ERROR node.
    2. STAFF_UPDATE_ERROR node. The message is sent to the STAFF_UPDATE_ERROR queue.
    3. Throw To Complete Rollback node. This node stops the processing of the message and records the number "3002", and the text "From Error_Handler message flow." to the message content. The message is now propagated back to the point of control, which is the STAFF_IN node in the main message flow.
      This stage is the final stage of the catch processing. It has the effect of rolling back the entire transaction, including the database updates under transactional control (that is, the STAFFDB update in the main flow) on the original try path. However, the STAFF_UPDATE_ERROR update, which is outside transactional control, is still committed.
  5. In the main message flow:
    1. STAFF_IN node. Because an exception has occurred in the processing of the message, the message is passed to the STAFF_FAIL node, rather than passed through the message flow again.
    2. STAFF_FAIL node. The node puts the message on the STAFF_FAIL queue. Alternatively, if the STAFF_FAIL queue is disabled, the message does not stop here. The message is passed back to the STAFF_IN node, and then to the subflow. The message then reaches the Check Backout Count node, which checks whether the backout count is zero. Because the message has passed through this node previously the backout count is no longer zero, and the message is discarded. Discarding the message prevents a situation where, if the STAFF_FAIL queue is disabled, the message keeps traveling through the flow. Compare this step to step 2b.

When you use a TryCatch node, or when you attach nodes to the Catch terminal of an MQInput node, you might assume that any processing that has occurred on the try path is not, if the nodes are correctly configured, committed if the catch path is invoked. This assumption is not correct. For example, if a database is updated on the try path under transactional control and the catch path is invoked and completes normally, the database update is still committed.

The requirement in this sample is to read a message, update a database, and write the message on another queue. If an error occurs, the database update is rolled back. Catch processing updates the error database with the details of the error and writes the original message to the failure queue. In programming terms, the following example shows what happens:

BEGIN (Start 'outer' unit-of-work.)
MQGET (Get message from the input queue.)
TRY
   BEGIN (Start 'inner' unit-of-work.)
   Update database
   MQPUT (Put message onto the output queue.)
   IF ERROR
      ROLLBACK inner unit-of-work GO TO CATCH
   ELSE
      COMMIT inner and outer unit-of-work as one unit-of-work
   END IF
CATCH
   Send message to STAFF_UPDATE_ERROR queue
   MQPUT (Put message onto the failure queue.)
   COMMIT outer unit-of-work

However, XA Transaction Manager and WebSphere MQ do not support two levels of unit-of-work in the same application. Therefore, the Error Handler sample uses the following structures:

For more information, see Message flow transactions in the IBM Integration Bus documentation.

Back to sample home