搜索

10.3. 规范化程序

download PDF

概述

规范化程序 模式用于处理语义等效的消息,但到达不同的格式。规范化程序将传入的信息转换为通用格式。

在 Apache Camel 中,您可以通过组合使用 第 8.1 节 “基于内容的路由器” 来实现规范化程序模式,该 第 8.1 节 “基于内容的路由器” 检测到传入消息的格式,以及不同的 第 5.6 节 “消息 Translator” 的集合,它将不同的传入的格式转换为通用格式。

图 10.3. 规范化程序模式

规范化程序模式

Java DSL 示例

本例演示了一个 Message Normalizer,它将两种类型的 XML 消息转换为通用格式。然后会过滤采用这种通用格式的消息。

使用 Fluent Builders

// we need to normalize two types of incoming messages
from("direct:start")
    .choice()
        .when().xpath("/employee").to("bean:normalizer?method=employeeToPerson")
        .when().xpath("/customer").to("bean:normalizer?method=customerToPerson")
    .end()
    .to("mock:result");

在这种情况下,我们使用 Java bean 作为规范化程序。类类似如下

// Java
public class MyNormalizer {
    public void employeeToPerson(Exchange exchange, @XPath("/employee/name/text()") String name) {
        exchange.getOut().setBody(createPerson(name));
    }

    public void customerToPerson(Exchange exchange, @XPath("/customer/@name") String name) {
        exchange.getOut().setBody(createPerson(name));
    }

    private String createPerson(String name) {
        return "<person name=\"" + name + "\"/>";
    }
}

XML 配置示例

XML DSL 中的同一示例

<camelContext xmlns="http://camel.apache.org/schema/spring">
  <route>
    <from uri="direct:start"/>
    <choice>
      <when>
        <xpath>/employee</xpath>
        <to uri="bean:normalizer?method=employeeToPerson"/>
      </when>
      <when>
        <xpath>/customer</xpath>
        <to uri="bean:normalizer?method=customerToPerson"/>
      </when>
    </choice>
    <to uri="mock:result"/>
  </route>
</camelContext>

<bean id="normalizer" class="org.apache.camel.processor.MyNormalizer"/>
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.