Choice
The Choice EIP routes messages based on conditions, similar to an "if-else" structure. It evaluates the specified conditions and directs the message to the corresponding route.
Configuring the Choice EIP in the Diagram
To configure a Choice in an integration in the Diagram tab, follow these steps:
-
Click +Add Step on the canvas.
-
Select the EIPs tab.
-
Select the Choice EIP. You can use the search tool to locate it.
When adding a "Choice", two branches will be automatically created on the canvas: When and Otherwise.
You can continue adding empty steps after the branches, with no limit on the number of subsequent steps.
It is possible to add new conditions to the "When" and "Otherwise" branches, but each Choice can only have one "Otherwise" branch. |
The animation below shows the addition of the Choice EIP to the canvas and the "when" branch being configured to check if the message body contains the word "success":
Choice EIP parameters
The parameters below should be provided when configuring the component:
Parameter | Description | Type |
---|---|---|
When |
Defines a condition for message processing, acting as the "if" block. |
List |
Otherwise |
Defines the default route, used when none of the conditions specified in the "When" parameter are met (acts as the "else" block). |
OtherwiseDefinition |
Precondition |
Allows the conditions in the branches ("When" and "Otherwise") to be evaluated during route initialization instead of dynamically at runtime. |
Boolean |
Deleting the Choice EIP or its branches
There are three scenarios for deleting the Choice EIP and its branches:
-
Deleting a "When" branch: all subsequent steps will be removed from the integration.
-
Deleting an "Otherwise" branch: all subsequent steps will be removed from the integration.
-
Deleting the Choice EIP: both the "When" and "Otherwise" branches, as well as all subsequent steps will be removed from the integration.
Example
Consider the integration below:
See the same integration in code format:
- from:
id: rest-2204314941
uri: rest:get:hello
steps:
- choice:
id: choice-3079603680
when:
- id: when-4223458332
simple: ${body} contains 'success'
steps:
- log:
id: log-4287304427
message: 'The call was been successful: ${body}'
- id: when-1122729813
simple: ${body} contains 'warning'
steps:
- log:
id: log-3011575176
message: 'The call returned a warning: ${body}'
- id: when-1140746842
simple: ${body} contains 'error'
steps:
- log:
id: log-2721128918
message: 'The call found an error: ${body}'
otherwise:
id: otherwise-3980042695
steps:
- log:
id: log-498737456
message: 'The call returned an unknown response: ${body}'
Now, check the detailed explanation:
-
The flow starts with a
GET
request to thehello
endpoint. The URI is the entry point of the flow, receiving the call’s response in the message body (${body
}).
Choice
-
The next step is a Choice, used to make decisions based on conditions. Camel evaluates the conditions and executes the steps corresponding to the condition that is true.
When
-
First condition (when): checks if the response body (
${body}
) contains the word "success." If true, it executes the following:-
Log: logs a message indicating that the call was successful, including the response body.
-
when:
- id: when-4223458332
simple: ${body} contains 'success'
steps:
- log:
id: log-4287304427
message: 'The call has been successful: ${body}'
-
Second condition (when): checks if the response body contains the word "warning." If true, it executes the following:
-
Log: logs a message indicating that the call returned a warning, including the response body.
-
- id: when-1122729813
simple: ${body} contains 'warning'
steps:
- log:
id: log-3011575176
message: 'The call returned a warning: ${body}'
-
Third condition (when): checks if the response body contains the word "error." If true, it executes the following:
-
Log: logs a message indicating that the call encountered an error, including the response body.
-
- id: when-1140746842
simple: ${body} contains 'error'
steps:
- log:
id: log-2721128918
message: 'The call found an error: ${body}'
Otherwise
-
If none of the previous conditions are met, the Otherwise step is executed, meaning the response body does not contain "success," "warning," or "error."
-
Log: logs a message indicating that the call returned an unknown response, including the response body.
-
otherwise:
id: otherwise-3980042695
steps:
- log:
id: log-498737456
message: 'The call returned an unknown response: ${body}'
Share your suggestions with us!
Click here and then [+ Submit idea]