Message Translator

Camel supports the Message Translator from the EIP patterns.

image

The Message Translator can be done in different ways in Camel:

  • Using Transform or Set Body in the DSL

  • Calling a Processor or bean to perform the transformation

  • Using template-based Components, with the template being the source for how the message is translated

  • Messages can also be transformed using Data Format to marshal and unmarshal messages in different encodings.

Example

Each of the approaches above is documented in the following examples:

Message Translator with Transform EIP

You can use a Transform which uses an Expression to do the transformation:

In the example below, we prepend Hello to the message body using the Simple language:

  • Java

  • XML

  • YAML

from("direct:cheese")
    .setBody(simple("Hello ${body}"))
    .to("log:hello");
<route>
    <from uri="activemq:cheese"/>
    <transform>
        <simple>Hello ${body}</simple>
    </transform>
    <to uri="activemq:wine"/>
</route>
- route:
    from:
      uri: activemq
      parameters:
        destinationName: cheese
      steps:
        - transform:
            simple:
              expression: Hello ${body}
        - to:
            uri: activemq
            parameters:
              destinationName: wine

Message Translator with Bean

You can transform a message using Camel’s Bean Integration to call any method on a bean that performs the message translation:

  • Java

  • XML

  • YAML

from("activemq:cheese")
  .bean("myTransformerBean", "doTransform")
  .to("activemq:wine");
<route>
    <from uri="activemq:cheese"/>
    <bean ref="myTransformerBean" method="doTransform"/>
    <to uri="activemq:wine"/>
</route>
- route:
    from:
      uri: activemq
      parameters:
        destinationName: cheese
      steps:
        - bean:
            ref: myTransformerBean
            method: doTransform
        - to:
            uri: activemq
            parameters:
              destinationName: wine

Message Translator with Processor

You can also use a Processor to do the transformation:

  • Java

  • XML

  • YAML

from("activemq:cheese")
  .process(new MyTransformerProcessor())
  .to("activemq:wine");
<route>
    <from uri="activemq:cheese"/>
    <process ref="myTransformerProcessor"/>
    <to uri="activemq:wine"/>
</route>
- route:
    from:
      uri: activemq
      parameters:
        destinationName: cheese
      steps:
        - process:
            ref: myTransformerProcessor
        - to:
            uri: activemq
            parameters:
              destinationName: wine

Message Translator using Templating Components

You can also consume a message from one destination, transform it with something like Velocity or XQuery, and then send it on to another destination.

  • Java

  • XML

  • YAML

from("activemq:cheese")
    .to("velocity:com/acme/MyResponse.vm")
    .to("activemq:wine");
<route>
    <from uri="activemq:cheese"/>
    <to uri="velocity:com/acme/MyResponse.vm"/>
    <to uri="activemq:wine"/>
</route>
- route:
    from:
      uri: activemq
      parameters:
        destinationName: cheese
      steps:
        - to:
            uri: velocity
            parameters:
              resourceUri: com/acme/MyResponse.vm
        - to:
            uri: activemq
            parameters:
              destinationName: wine

Message Translator using Data Format

See Marshal EIP for more details and examples.