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.

Parameters

Parameter Description Default value 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

Example

Consider the following snippet:

- 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}'

Here is the same snippet represented as a diagram:

eip choice example

Detailed explanation:

  • The flow starts with a GET request to the hello 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}'
Thanks for your feedback!
EDIT

Share your suggestions with us!
Click here and then [+ Submit idea]