gpt4 book ai didi

apache-camel - Apache Camel : can message have multiple objects in body (with different classes)?

转载 作者:行者123 更新时间:2023-12-04 18:09:40 24 4
gpt4 key购买 nike

我几乎已经准备好在 Java 中使用 jms 和 Camel 的应用程序。弹出我们必须在交换/消息中添加其他信息。假设这些附加信息实际上是新的 java 对象。添加要交换的新对象的最佳方法是什么?

我有很多 Camel 处理器处理看起来像这样的消息:

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() 表明只有一个主体类。

如果我不能这样做,那么最好的方法是什么?我可以尝试将我转换的字符串和信息包装到一个类中,并将该新类放入消息中,但这会导致改变在每个处理器中获取字符串的方式。我想避免这种情况。

最佳答案

Exchange的 body 是单例Object .如果您想将多个对象添加到您的交换的主体中,您需要使交换的主体成为 map 、列表或 pojo,其中包含您在其中设置所有对象的字段。

关于apache-camel - Apache Camel : can message have multiple objects in body (with different classes)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17469762/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com