Which Camel construct is suited for transforming?

user1768830 picture user1768830 · Jan 24, 2013 · Viewed 8.5k times · Source

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.

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) {
        // ...
    }
}

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!

Answer

Jakub Korab picture Jakub Korab · Jan 25, 2013

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

  • 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.