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

一个人想着一个人 提交于 2019-12-23 09:59:11

问题


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); 
    }

}

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.


回答1:


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.



来源:https://stackoverflow.com/questions/17469762/apache-camel-can-message-have-multiple-objects-in-body-with-different-classes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!