Camel routing based on an xpath value in an XML message.

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Camel routing based on an xpath value in an XML message.

mtod09
I would like to perform a Camel route based on an xpath value in an XML message.

The XML Message example:

<xmlMessage>
        <header>
                <version>1</version>               
                <msgType>Event</msgType>       
                <source>App1<source>               
                <route>topic.A</route>
                <destination>AppA</destination>
        </header>
        <payload/>
</xmlMessage>

ActiveMQ example:

<camelContext id="TrackingRoute" xmlns="http://camel.apache.org/schema/spring">
        <route>
                <from uri="activemq:queue:queue.incoming"/>                       
                <filter>
                        <to uri="activemq:topic:topic.${topicUri}"/>       
                </filter>
    </route>
</camelContext>

Is it possible to query the XML using an xpath expression to return the destination value to a variable then use that to define the to uri?

Thanks

Mike
Reply | Threaded
Open this post in threaded view
|

Re: Camel routing based on an xpath value in an XML message.

ceposta
why, yes it is possible :)

You can use an xpath expression to get the value of the node you
like.. then use a recipient list or dynamic router to get it to the
queue/topic you like:

http://camel.apache.org/recipient-list.html


On Mon, Mar 31, 2014 at 2:33 PM, mtod09 <[hidden email]> wrote:

> I would like to perform a Camel route based on an xpath value in an XML
> message.
>
> The XML Message example:
>
> <xmlMessage>
>         <header>
>                 <version>1</version>
>                 <msgType>Event</msgType>
>                 <source>App1<source>
>                 <route>topic.A</route>
>                 <destination>AppA</destination>
>         </header>
>         <payload/>
> </xmlMessage>
>
> ActiveMQ example:
>
> <camelContext id="TrackingRoute"
> xmlns="http://camel.apache.org/schema/spring">
>         <route>
>                 <from uri="activemq:queue:queue.incoming"/>
>                 <filter>
>                         <to uri="activemq:topic:topic.${topicUri}"/>
>                 </filter>
>     </route>
> </camelContext>
>
> Is it possible to query the XML using an xpath expression to return the
> destination value to a variable then use that to define the to uri?
>
> Thanks
>
> Mike
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Camel-routing-based-on-an-xpath-value-in-an-XML-message-tp5749578.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



--
Christian Posta
http://www.christianposta.com/blog
twitter: @christianposta
Reply | Threaded
Open this post in threaded view
|

Re: Camel routing based on an xpath value in an XML message.

mtod09
I'm having a problem getting the value from the Dest Element and using it in the recipient list.
I don't get any errors but the message does not show up in the topics.

I'm using an input queue "activemq:queue:queue.ESB.incoming" then it should route to 2 topics based on the Dest values.

XML Message example:

<FMIMessage version="1.0" MsgID="1804343574">
        <Header1>
                <Source>Pipeline</Source>
                <AppVersion>1.1</AppVersion>
                <Dest>topic:trftracking, topic:test</Dest>
        </Header1>
        <Event/>
</FMTMessage>
       
       
ActiveMQ.xml :
       
<camelContext id="TrackingRoute"  xmlns="http://camel.apache.org/schema/spring">
        <route>
                <from uri="activemq:queue:queue.ESB.incoming"/>
                <recipientList delimiter=",">
                        <xpath>/Header1/Dest</xpath>
                </recipientList>
        </route>
</camelContext>

Note: I'm using ActiveMQ 5.9
Reply | Threaded
Open this post in threaded view
|

Re: Camel routing based on an xpath value in an XML message.

kraythe
I think your xpath is wrong, try: /Header1/Dest/text()
The easiest way to diagnose issues like these is split into two steps.
First store the value from the xpath in a header and then pass the header
to recipient list. That will allow you to use tracer to follow the flow.



*Robert Simmons Jr. MSc. - Lead Java Architect @ EA*
*Author of: Hardcore Java (2003) and Maintainable Java (2012)*
*LinkedIn: **http://www.linkedin.com/pub/robert-simmons/40/852/a39
<http://www.linkedin.com/pub/robert-simmons/40/852/a39>*


On Tue, Apr 1, 2014 at 9:05 AM, mtod09 <[hidden email]> wrote:

> I'm having a problem getting the value from the Dest Element and using it
> in
> the recipient list.
> I don't get any errors but the message does not show up in the topics.
>
> I'm using an input queue "activemq:queue:queue.ESB.incoming" then it should
> route to 2 topics based on the Dest values.
>
> XML Message example:
>
> <FMIMessage version="1.0" MsgID="1804343574">
>         <Header1>
>                 <Source>Pipeline</Source>
>                 <AppVersion>1.1</AppVersion>
>                 <Dest>topic:trftracking, topic:test</Dest>
>         </Header1>
>         <Event/>
> </FMTMessage>
>
>
> ActiveMQ.xml :
>
> <camelContext id="TrackingRoute"
> xmlns="http://camel.apache.org/schema/spring">
>         <route>
>                 <from uri="activemq:queue:queue.ESB.incoming"/>
>                 <recipientList delimiter=",">
>                         <xpath>/Header1/Dest</xpath>
>                 </recipientList>
>         </route>
> </camelContext>
>
> Note: I'm using ActiveMQ 5.9
>
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Camel-routing-based-on-an-xpath-value-in-an-XML-message-tp5749578p5749629.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
Reply | Threaded
Open this post in threaded view
|

Re: Camel routing based on an xpath value in an XML message.

mtod09
Thanks for the update.

I'm still have issues updating the header.

        <camelContext id="TRFTrackingRoute"  xmlns="http://camel.apache.org/schema/spring">
                <route>
                        <from uri="activemq:queue:queue.ESB.incoming"/>
                        <setHeader headerName="JMSDestinationName">                               
                                <xpath>/Header1/Dest/text()</xpath>
                        </setHeader>
                        <to uri="activemq:queue:trftracking"/>
                </route>
        </camelContext>

I tried setting the header but when I run this I don't get the correct value.
Only the to uri is set.

I tried using a different header name "Route1" but that did not work.

Thanks

Mike
Reply | Threaded
Open this post in threaded view
|

Re: Camel routing based on an xpath value in an XML message.

mtod09
I was able to get this to work using the following:

ActiveMQ XMl:

        <camelContext id="TRFTrackingRoute"  xmlns="http://camel.apache.org/schema/spring">
                <route>
                        <from uri="activemq:queue:queue.ESB.incoming"/>
                        <setHeader headerName="Route1">                               
                                <xpath resultType="String">/FMIMessage/Header1/Dest/text()</xpath>
                        </setHeader>
                        <recipientList delimiter="false">
                                <header>Route1</header>
                        </recipientList>                       
                </route>
        </camelContext>

Message:

<FMIMessage version="1.0" MsgID="1804343574">
        <Header1>
                <Source>Pipeline</Source>
                <AppVersion>1.1</AppVersion>
                <Dest>activemq:topic:trftracking</Dest>
        </Header1>
        <Event/>
</FMIMessage>

Thanks for the insight.

Mike