Apache Camel:消息体中可以有多个对象(具有不同的类)吗? [英] Apache Camel: can message have multiple objects in body (with different classes)?
问题描述
我几乎已经准备好在 Java 中使用 jms 和 Camel 的应用程序.弹出我们必须在交换/消息中添加其他信息.假设这些附加信息实际上是新的 java 对象.添加要交换的新对象的最佳方法是什么?
I have almost ready application in java that use jms with Camel. Pop up that we I have to add additional infomations in exchange/message. Lets say that those additional infomations are in fact new java object. What is the best way to add my new object to exchange?
我有很多 Camel 处理器处理看起来像这样的消息:
I have a lot of Camel processors processing the message that look like this:
public class MyProcessor implements Processor {
@Override
public void process(Exchange exchange) throws Exception {
String s = exchange.getIn().getBody(String.class);
s = magicalTransform(s);
exchange.getIn().setBody(s, String.class);
//Now I have to add object of some Info.cass:
Info info = new Info( new Date() );
//Can I add it like this? :
exchange.getIn().setBody(info, Info.class);
}
}
问题是如果我可以向Message中添加很多对象,我找不到信息.Message 方法:setBody(Object body, Class type) 表明这是可能的,但也有方法:getBody() 表明只有一个主体类.
The problem is that I can't find information if I can add many objects to Message. The Message method: setBody(Object body, Class type) suggest that it is possible, but there is also method: getBody() that sugesst that there is only one body class.
如果我不能这样做,那么最好的方法是什么?我可以尝试将我转换的字符串和信息包装到一个类中,并将该新类放入消息中,但这会导致改变在每个处理器中获取字符串的方式.我想避免这种情况.
If I can't do it in this way, then what's the best way? I could try to wrap String that I transform and info in to one class, and put that new class in to message, but It will cause change the way obtaining String in every Processor. I want to avoid that.
推荐答案
Exchange
的主体是单个 Object
.如果您想将多个对象添加到您的 Exchange 主体中,您需要将 Exchange 主体设为地图、列表或 pojo,其中包含您在其中设置所有对象的字段.
The body of an Exchange
is a single Object
. If you want to add multiple objects to the body of your exchange you need to make the body of the exchange a map, list, or pojo with fields that you set all of your objects within.
这篇关于Apache Camel:消息体中可以有多个对象(具有不同的类)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!