10.8. 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");

您还可以验证消息 header>_<- the the example:

from("jms:queue:incoming")
  .validate(header("bar").isGreaterThan(100))
  .to("bean:MyServiceBean.processLine");

您可以将 validate 与 简单表达式语言 一起使用:

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"/>

您还可以验证消息 header>_<- the the example:

<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"/>