Message Translator

36 篇文章 1 订阅

Message Translator

Camel supports the Message Translator from the EIP patterns by using an arbitrary Processor in the routing logic, by using a bean to perform the transformation, or by using transform() in the DSL. You can also use a Data Format to marshal and unmarshal messages in different encodings.

Using the Fluent Builders

You can transform a message using Camel's Bean Integration to call any method on a bean in your Registry such as your Spring XML configuration file as follows

from("activemq:SomeQueue"
).
beanRef("myTransformerBean" , "myMethodName" ).
to("mqseries:AnotherQueue" );

Where the "myTransformerBean" would be defined in a Spring XML file or defined in JNDI etc. You can omit the method name parameter from beanRef() and the Bean Integration will try to deduce the method to invoke from the message exchange.

or you can add your own explicit Processor to do the transformation

from("direct:start"
).process(new
 Processor() {
public void process(Exchange exchange) {
Message in = exchange.getIn();
in.setBody(in.getBody(String .class) + " World!" );
}
}).to("mock:result" );

or you can use the DSL to explicitly configure the transformation

from("direct:start"
).transform(body().append(" World!"
)).to("mock:result"
);

Use Spring XML

You can also use Spring XML Extensions to do a transformation. Basically any Expression language can be substituted inside the transform element as shown below

<camelContext xmlns="http://camel.apache.org/schema/spring"
>

<route>
<from uri="direct:start" />
<transform>
<simple> ${in.body} extra data!</simple>
</transform>
<to uri="mock:end" />
</route>
</camelContext>

Or you can use the Bean Integration to invoke a bean

<route>
<from uri="activemq:Input" />
<bean ref="myBeanName" method="doTransform" />
<to uri="activemq:Output" />
</route>

You can also use Templating to consume a message from one destination, transform it with something like Velocity or XQuery and then send it on to another destination. For example using InOnly (one way messaging)

from("activemq:My.Queue"
).
to("velocity:com/acme/MyResponse.vm" ).
to("activemq:Another.Queue" );

If you want to use InOut (request-reply) semantics to process requests on the My.Queue queue on ActiveMQ with a template generated response, then sending responses back to the JMSReplyTo Destination you could use this.

from("activemq:My.Queue"
).
to("velocity:com/acme/MyResponse.vm" );

For further examples of this pattern in use you could look at one of the JUnit tests

Using This Pattern

If you would like to use this EIP Pattern then please read the Getting Started , you may also find the Architecture useful particularly the description of Endpoint and URIs . Then you could try out some of the Examples first before trying this pattern out.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值