10.8. validate
概述
验证模式提供了便捷的语法,以检查消息的内容是否有效。validate DSL 命令采用 predicate 表达式作为其唯一参数:如果 predicate 评估为 true,则路由会继续正常处理;如果 predicate 评估为 false,则会抛出 PredicateValidationException。
Java DSL 示例
以下路由使用正则表达式验证当前消息的正文:
from("jms:queue:incoming")
.validate(body(String.class).regex("^\\w{10}\\,\\d{2}\\,\\w{24}$"))
.to("bean:MyServiceBean.processLine");您还可以验证消息标头 iwl-wagon 例如:
from("jms:queue:incoming")
.validate(header("bar").isGreaterThan(100))
.to("bean:MyServiceBean.processLine");您可以使用 简单 表达式语言验证:
from("jms:queue:incoming")
.validate(simple("${in.header.bar} == 100"))
.to("bean:MyServiceBean.processLine");XML DSL 示例
要在 XML DSL 中使用验证,推荐的方法是使用 简单 表达式语言:
<route>
<from uri="jms:queue:incoming"/>
<validate>
<simple>${body} regex ^\\w{10}\\,\\d{2}\\,\\w{24}$</simple>
</validate>
<beanRef ref="myServiceBean" method="processLine"/>
</route>
<bean id="myServiceBean" class="com.mycompany.MyServiceBean"/>您还可以验证消息标头 iwl-wagon 例如:
<route>
<from uri="jms:queue:incoming"/>
<validate>
<simple>${in.header.bar} == 100</simple>
</validate>
<beanRef ref="myServiceBean" method="processLine"/>
</route>
<bean id="myServiceBean" class="com.mycompany.MyServiceBean"/>