10.8. validate
概述
validate 模式提供了一种方便的语法,用于检查消息的内容是否有效。validate DSL 命令将 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");您还可以验证消息标题为确保信息标题为将以下内容:
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"/>您还可以验证消息标题为确保信息标题为将以下内容:
<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"/>