You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by npa <np...@gmail.com> on 2014/04/24 18:18:15 UTC

Splitting list of elements in a message body..

Iam trying to write a camel route which would send different elements of a
list to different queues.

The message body would be a list with 2 xml's. For example:

    <Cat>
    <Name>Cat1</Name>
    </Cat>,
    <Dog>
    <Name>Dog1</Name>
    </Dog>

Now, I need to send the 'Cat' part of the message i.e.
<Cat><Name>Cat1</Name></Cat> part to queue1 and the 'Dog' part of xml i.e.
<Dog><Name>Dog1</Name></Dog> to a different queue?

This is the route that I have, but is not working:

    <route>
        <from uri="jms:queue:InQueue" />		
        <choice>
            <when>
                <simple>${in.body} regex 'Cat'</simple>
                <to uri="jms:queue:CatQueue" />
            </when>
            <when>
                <simple>${in.body} regex 'Dog'</simple>
                <to uri="jms:queue:DogQueue" />
            </when>
        </choice>
    </route>

Any ideas on what am i doing wrong here?



--
View this message in context: http://camel.465427.n5.nabble.com/Splitting-list-of-elements-in-a-message-body-tp5750556.html
Sent from the Camel - Users mailing list archive at Nabble.com.

AW: AW: Splitting list of elements in a message body..

Posted by "Jan Matèrne (jhm)" <ap...@materne.de>.
> Yes..there is a comma...
> 
> The message that would be returned from a processor Call in the route
> would be like this:
> [<Cat xmlns="http://www.openapplications.org/oagis/9"
> xmlns:lw="http://www.org/oagis/9">
>     <name>Cat1</name>
> </Cat>,
> <Dog xmlns="http://www.openapplications.org/oagis/9"
> xmlns:lw="http://www.org/oagis/9">
>     <name>Dog1</name>
> </Dog>]

Maybe this helps ...


Jan


public class Split2Test extends CamelTestSupport {

    @Test
    public void animals() throws InterruptedException {
        String xml = "<Cat xmlns=\"http://www.openapplications.org/oagis/9\"
xmlns:lw=\"http://www.org/oagis/9\">"
                   + "    <name>Cat1</name>"
                   + "</Cat>,"
                   + "<Dog xmlns=\"http://www.openapplications.org/oagis/9\"
xmlns:lw=\"http://www.org/oagis/9\">"
                   + "    <name>Dog1</name>"
                   + "</Dog>";
        getMockEndpoint("mock:dog").expectedMessageCount(1);
        getMockEndpoint("mock:dog").expectedMessagesMatches(nameIs("Dog1"));
        getMockEndpoint("mock:cat").expectedMessageCount(1);
        getMockEndpoint("mock:cat").expectedMessagesMatches(nameIs("Cat1"));
        
        template.sendBody("direct:in", xml);
        
        assertMockEndpointsSatisfied();
    }
    
    private Predicate nameIs(final String name) {
        return new Predicate() {
            @Override
            public boolean matches(Exchange exchange) {
                return
exchange.getIn().getBody(String.class).contains("<name>" + name +
"</name>");
            }
        };
    }

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:in")
                    .split(body(String.class).tokenize(","))
                    .choice()
                        .when(simple("${body} contains
'Cat'")).to("mock:cat")
                        .when(simple("${body} contains
'Dog'")).to("mock:dog")
                        .otherwise().to("mock:unknown");
            }
        };
    }
}


Re: AW: Splitting list of elements in a message body..

Posted by npa <np...@gmail.com>.
Yes..there is a comma...

The message that would be returned from a processor Call in the route would
be like this:
[<Cat xmlns="http://www.openapplications.org/oagis/9"
xmlns:lw="http://www.org/oagis/9">
    <name>Cat1</name>
</Cat>,
<Dog xmlns="http://www.openapplications.org/oagis/9"
xmlns:lw="http://www.org/oagis/9">
    <name>Dog1</name>
</Dog>]



--
View this message in context: http://camel.465427.n5.nabble.com/Splitting-list-of-elements-in-a-message-body-tp5750556p5750573.html
Sent from the Camel - Users mailing list archive at Nabble.com.

AW: Splitting list of elements in a message body..

Posted by "Jan Matèrne (jhm)" <ap...@materne.de>.
> The message body would be a list with 2 xml's. For example:
> 
>     <Cat>
>     <Name>Cat1</Name>
>     </Cat>,
>     <Dog>
>     <Name>Dog1</Name>
>     </Dog>
> 
> Now, I need to send the 'Cat' part of the message i.e.
> <Cat><Name>Cat1</Name></Cat> part to queue1 and the 'Dog' part of xml
> i.e.
> <Dog><Name>Dog1</Name></Dog> to a different queue?
> 
> This is the route that I have, but is not working:
> 
>     <route>
>         <from uri="jms:queue:InQueue" />
>         <choice>
>             <when>
>                 <simple>${in.body} regex 'Cat'</simple>
>                 <to uri="jms:queue:CatQueue" />
>             </when>
>             <when>
>                 <simple>${in.body} regex 'Dog'</simple>
>                 <to uri="jms:queue:DogQueue" />
>             </when>
>         </choice>
>     </route>
> 
> Any ideas on what am i doing wrong here?


First you have to split the message (is there a comma between </Cat> and
<Dog>?)

Jan