Search code examples
jbossapache-camelenterprise-integration

Difference between filter and choice in camel


What is the difference between filter and choice in apache Camel?

    from("direct:a")
        .choice()
            .when(header("foo").isEqualTo("bar"))
                .to("direct:b")
            .when(header("foo").isEqualTo("cheese"))
                .to("direct:c")
            .otherwise()
                .to("direct:d");

Solution

  • In short a filter is like a single java if statement, eg

    if x = 2 {
      ...
    }
    

    And in Camel:

    .filter(header("foo").isEqualTo("bar"))
      ...
    .end()
    

    And choice is like a java if ... elseif ... elseif ... else statement,

    if x = 2 {
      ...
    } else if x = 3 {
      ...
    }
    

    And in Camel:

    .choice()
      .when(header("foo").isEqualTo("bar"))
        ...
      .when(header("foo").isEqualTo("chese"))
        ...
      .otherwise()
        ....
    .end()
    

    Note that otherwise is optional in the choice.