当前位置:   article > 正文

Message Filter

Message Filter

Message Filter

The Message Filter from the EIP patterns allows you to filter messages

The following example shows how to create a Message Filter route consuming messages from an endpoint called queue:a which if the Predicate is true will be dispatched to queue:b

Using the Fluent Builders

  1. RouteBuilder builder = new
  2. RouteBuilder() {
  3. public
  4. void configure() {
  5. errorHandler(deadLetterChannel("mock:error"
  6. ));
  7. from("seda:a"
  8. )
  9. .filter(header("foo"
  10. ).isEqualTo("bar"
  11. ))
  12. .to("seda:b"
  13. );
  14. }
  15. };

You can of course use many different Predicate languages such as XPath , XQuery , SQL or various Scripting Languages . Here is an XPath example

  1. from("direct:start"
  2. ).
  3. filter().xpath("/person[@name='James']"
  4. ).
  5. to("mock:result"
  6. );

Using the Spring XML Extensions

<camelContext errorHandlerRef="errorHandler"
 xmlns="http://camel.apache.org/schema/spring"
>

<route>
<from uri="seda:a" />
<filter>
<xpath> $foo = 'bar'</xpath>
<to uri="seda:b" />
</filter>
</route>
</camelContext>

For further examples of this pattern in use you could look at the junit test case

Using stop

Available as of Camel 2.0

Stop is a bit different than a message filter as it will filter out all messages. Stop is convenient to use in a Content Based Router when you for example need to stop further processing in one of the predicates.

In the example below we do not want to route messages any further that has the word Bye in the message body. Notice how we prevent this in the when predicate by using the .stop() .

  1. from("direct:start"
  2. )
  3. .choice()
  4. .when(body().contains("Hello"
  5. )).to("mock:hello"
  6. )
  7. .when(body().contains("Bye"
  8. )).to("mock:bye"
  9. ).stop()
  10. .otherwise().to("mock:other"
  11. )
  12. .end()
  13. .to("mock:result"
  14. );
Knowing if Exchange was filtered or not

Available as of Camel 2.5

The Message Filter EIP will add a property on the Exchange which states if it was filtered or not.

The property has the key Exchannge.FILTER_MATCHED which has the String value of CamelFilterMatched . Its value is a boolean indicating true or false . If the value is true then the Exchange was routed in the filter block.

Using This Pattern

If you would like to use this EIP Pattern then please read the Getting Started , you may also find the Architecture useful particularly the description of Endpoint and URIs . Then you could try out some of the Examples first before trying this pattern out.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/813941
推荐阅读
相关标签
  

闽ICP备14008679号