Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

apache-camel

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.

like image 579
M314 Avatar asked Jul 04 '13 12:07

M314


1 Answers

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.

like image 111
gregwhitaker Avatar answered Oct 05 '22 13:10

gregwhitaker