0

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");
  • 1
    well in short the choice will route the exchange according to the given condition, while the filter removes certain elements from the exchange http://camel.apache.org/content-based-router.html http://camel.apache.org/message-filter.html – Aku Nour May 15 '18 at 13:12

2 Answers2

5

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.

Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65
0

Moreover Choice and filter do same operation, where in Filter have additional property Exchange which will states it is filtered or not.

  1. Choice is available from version 2.0
  2. Filter available from version 2.5
Lucifer
  • 784
  • 2
  • 7
  • 20