问题描述
我在Java中已经准备好将骆驼与jms一起使用的应用程序.弹出我们必须在交换/消息中添加其他信息.可以说这些附加信息实际上是新的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?
我有很多骆驼处理器处理如下消息:
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.
如果我不能以这种方式做到这一点,那么最好的方法是什么?我可以尝试将转换后的字符串和信息包装到一个类中,然后将该新类放入消息中,但这将导致更改在每个Processor中获取String的方式.我想避免这种情况.
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
.如果要将多个对象添加到交换的主体,则需要使交换的主体成为具有在其中设置所有对象的字段的地图,列表或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:消息中可以有多个对象(具有不同的类)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!