哪种 Camel 构造适合转换?

2022-01-19 00:00:00 java apache-camel esb

Apache Camel 提供了几种执行数据转换的方法:其转换 EIP 的概念、自定义数据格式以及允许自定义类型转换器.

Apache Camel offers several ways of performing data transforms: its concept of the Transform EIP, custom DataFormats, as well as its allowance for custom Type Converters.

我有一种情况,我需要从 Camel 路线内部进行非常复杂的转换.我应该实现我自己的类型转换器、我自己的 DataFormat,还是应该实现 org.apache.camel.Expression 并将所有转换的东西放在那里:

I have a situation where I need to do a very complex transform from inside a Camel route. Should I be implementing my own Type Converter, my own DataFormat, or should I implement org.apache.camel.Expression and put all the transform stuff in there:

public class MyTransformer implements Expression {
    @Override
    public <T> T evaluate(Exchange arg0, Class<T> arg1) {
        // ...
    }
}

我想我对何时/何时使用您自己的类型转换器、何时使用 .transform(myTransformer) 处理器或何时使用自定义 DataFormat 感到困惑.提前致谢!

I guess I'm confused about where/when it's appropriate to use your own Type Converter, when to use the .transform(myTransformer) processor, or when to use a custom DataFormat. Thanks in advance!

推荐答案

虽然它们都用于不同的事情,但它们的区别是微妙的.你应该使用:

The differences are subtle, though they are all used for different things. You should use:

  • a transformer 当您将业务负载"从一种形状转换为另一种形状时.例如,当您将从 DAO 中提取的值对象转换为您将用于调用 Web 服务的 JAXB 注释对象时.
  • a 数据格式,当您想要编组高级表示时,例如某种类型的对象,转换为较低级别的表示 - 您将通过电线发送的东西.数据格式包括序列化、Google 协议缓冲区、JSON、JAXB 等.
  • a 类型转换器,当您更改访问消息表示的方式时.例如.一个字符串和一个字节数组或一个 InputStream 仍然读取相同的字符,因此您可以在那里编写(尽管实际上有内置的)转换器来在其中任何两个之间进行转换.
  • a transformer when you are converting a "business payload" from one shape to another. For example, when you are converting value objects that you pulled from a DAO into JAXB annotated objects that you are going to use to invoke a webservice.
  • a data format when you want to marshall an high-level representation, such as some type of Object, into a lower level representation - something that you would send over a wire. Data formats include serialization, Google protocol buffers, JSON, JAXB etc.
  • a type converter when you are changing the way you access a representation of a message. E.g. a String and a byte array or an InputStream still read the same characters, so there you might write (though there actually are built-in) converters that convert between any two of these.

相关文章