You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by buchnerm <ma...@gmx.at> on 2008/07/21 14:31:04 UTC

how to use xpath in spring xml and query header fields

Hi 

I have the following issue:  I created a processor which sets depending on
the sessionid a header value in the exchange 
When I configure now a "choice" in spring xml which routes based on this
header field I get an exception:

how should the route look like? 
My code now: (xml)

<route>
			<from ref="input1" />
			<to uri="createProcessor" />
			<to uri="realtimeProcessor" />
			<to uri="queueSwitchProcessor" />
			<choice>
		      <when>
		        <xpath>$queueid = "0"</xpath>
		        <to ref="output1"/>
		      </when>
		      <when>
		        <xpath>$queueid = "1"</xpath>
		        <to ref="output2"/>
		      </when>
		      <otherwise>
		        <to ref="output3"/>
		      </otherwise>
		    </choice>
</route>

My code of the processor (queueSwitchProcessor):

public void process(Exchange exchange) throws Exception {
		
		ObjectImpressionTransportBean transportBean =
(ObjectImpressionTransportBean)(exchange.getIn().getBody());
		String sessionid =
transportBean.getSensorCall().getParameter().get(PARAMETERKEY_SESSIONID);
		if (sessionid != null) {
			int queueid = sessionid.hashCode();
			if (queueid < 0) {queueid = queueid * -1;}
			queueid = queueid%numberOfConsumers;
			exchange.getIn().setHeader("queueid", Integer.toString(queueid));
		} else {
			exchange.getIn().setHeader("queueid", "0");
		}		
	}

how do I access the header field "queueid" via xpath???

now i get this exception: (because its trying to access the body!!!)  

2008-07-21 14:09:39,170 ERROR [DefaultMessageListenerContainer-17]
org.apache.camel.processor.DeadLetterChannel: Failed delivery for
exchangeId: ID-power/2826-1216642175780/0-8. On delivery attempt: 2 caught:
java.lang.ClassCastException:
com.fatwire.analytics.domain.ObjectImpressionTransportBean
java.lang.ClassCastException:
com.fatwire.analytics.domain.ObjectImpressionTransportBean
	at
com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(XPathExpressionImpl.java:115)
	at
com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(XPathExpressionImpl.java:97)
	at
com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:178)
	at
org.apache.camel.builder.xml.XPathBuilder.evaluateAs(XPathBuilder.java:429)
	at org.apache.camel.builder.xml.XPathBuilder.matches(XPathBuilder.java:98)
	at org.apache.camel.builder.xml.XPathBuilder.matches(XPathBuilder.java:63)
	at
org.apache.camel.processor.ChoiceProcessor.process(ChoiceProcessor.java:47)
-- 
View this message in context: http://www.nabble.com/how-to-use-xpath-in-spring-xml-and-query-header-fields-tp18567110s22882p18567110.html
Sent from the Camel - Users mailing list archive at Nabble.com.


RE: how to use xpath in spring xml and query header fields

Posted by Claus Ibsen <ci...@silverbullet.dk>.
Hi

I have added a spring xpath header sample to the xpath wiki page. Will take a little while to get updated:
http://activemq.apache.org/camel/xpath.html

Or if you are impatient check the dynamic cwiki page:
http://cwiki.apache.org/confluence/display/CAMEL/XPath 


The unit test is also in the code. You can browse it online:
http://activemq.apache.org/camel/source.html

Look in the components/camel-spring for the SpringXPathHederTest.java in test/java and the SpringXPathHederTest-context.xml in the test/resources folder.


Basically using <xpath>$queueid = '0'</xpath> should do the trick



Med venlig hilsen
 
Claus Ibsen
......................................
Silverbullet
Skovsgårdsvænget 21
8362 Hørning
Tlf. +45 2962 7576
Web: www.silverbullet.dk

-----Original Message-----
From: buchnerm [mailto:markus.buchner@gmx.at] 
Sent: 21. juli 2008 16:54
To: camel-user@activemq.apache.org
Subject: Re: how to use xpath in spring xml and query header fields


Sorry but I cant get it working with xpath - I could get it working now with
jxpath - I found a test case:

http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jxpath/src/test/java/org/apache/camel/language/jxpath/JXPathTest.java?view=markup

this route works for me now: 

		<route>
			<from ref="input1" />
			<to uri="counter" />
			<to uri="createProcessor" />
			<to uri="realtimeProcessor" />
			<to uri="queueSwitchProcessor" />
			<choice>
		      <when>
		        <jxpath>in/headers/@queueid = '0'</jxpath>
		        <to ref="output1"/>
		      </when>
		      <when>
		        <jxpath>in/headers/@queueid = '1'</jxpath>
		        <to ref="output2"/>
		      </when>
		      <otherwise>
		        <to ref="output3"/>
		      </otherwise>
		    </choice>
		</route>

although I think jxpath is much slower than xpath or??? so I am looking
forward to solve it another way 

Thanks for the fast reply - greetings max
-- 
View this message in context: http://www.nabble.com/how-to-use-xpath-in-spring-xml-and-query-header-fields-tp18567110s22882p18570337.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: how to use xpath in spring xml and query header fields

Posted by buchnerm <ma...@gmx.at>.
Sorry but I cant get it working with xpath - I could get it working now with
jxpath - I found a test case:

http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jxpath/src/test/java/org/apache/camel/language/jxpath/JXPathTest.java?view=markup

this route works for me now: 

		<route>
			<from ref="input1" />
			<to uri="counter" />
			<to uri="createProcessor" />
			<to uri="realtimeProcessor" />
			<to uri="queueSwitchProcessor" />
			<choice>
		      <when>
		        <jxpath>in/headers/@queueid = '0'</jxpath>
		        <to ref="output1"/>
		      </when>
		      <when>
		        <jxpath>in/headers/@queueid = '1'</jxpath>
		        <to ref="output2"/>
		      </when>
		      <otherwise>
		        <to ref="output3"/>
		      </otherwise>
		    </choice>
		</route>

although I think jxpath is much slower than xpath or??? so I am looking
forward to solve it another way 

Thanks for the fast reply - greetings max
-- 
View this message in context: http://www.nabble.com/how-to-use-xpath-in-spring-xml-and-query-header-fields-tp18567110s22882p18570337.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: how to use xpath in spring xml and query header fields

Posted by Jonathan Anstey <ja...@gmail.com>.
buchnerm wrote:
> Sorry but none of your methods worked - any other suggestions? Links to
> tutorials? 
>
> greetings max
>
>   

Max,

Did you take a look at the following test cases?

http://svn.apache.org/repos/asf/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ChoiceTest.java
http://svn.apache.org/repos/asf/activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringChoiceTest.java
http://svn.apache.org/repos/asf/activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/choice.xml

 From these it looks like this

                from("direct:start").choice()
                        
.when(header("foo").isEqualTo("bar")).setHeader("name", 
constant("a")).to("mock:x")
                        
.when(header("foo").isEqualTo("cheese")).to("mock:y")
                        .otherwise().to("mock:z");

is equivalent to

    <route>
      <from uri="direct:start"/>
      <choice>
        <when>
          <xpath>$foo = 'bar'</xpath>
          <to uri="mock:x"/>
        </when>
        <when>
          <xpath>$foo = 'cheese'</xpath>
          <to uri="mock:y"/>
        </when>
        <otherwise>
          <to uri="mock:z"/>
        </otherwise>
      </choice>
    </route>

and since this test case is passing, Camel seems to be picking out the 
headers properly and not the body. Would you mind creating a ticket here 
http://issues.apache.org/activemq/browse/CAMEL and attaching a test case 
so we can investigate this case further?

Cheers,
Jon

Re: how to use xpath in spring xml and query header fields

Posted by buchnerm <ma...@gmx.at>.
Sorry but none of your methods worked - any other suggestions? Links to
tutorials? 

greetings max

-- 
View this message in context: http://www.nabble.com/how-to-use-xpath-in-spring-xml-and-query-header-fields-tp18567110s22882p18568633.html
Sent from the Camel - Users mailing list archive at Nabble.com.


RE: how to use xpath in spring xml and query header fields

Posted by Claus Ibsen <ci...@silverbullet.dk>.
Hi

You have somehow to indicate its the header and not the body.
        <xpath>$queueid = "0"</xpath>

Should be something like:
        <xpath>in/header/queueid = "0"</xpath>
Or:
        <xpath>/header/queueid = "0"</xpath>

http://activemq.apache.org/camel/xpath.html
or 
http://activemq.apache.org/camel/jxpath.html

A good place to look is also the unit test in the camel-core, there are some xpath related tests.

Let us know what works for you and we will be able to updated the wiki documentation with a header example:
http://activemq.apache.org/camel/xpath.html


Med venlig hilsen
 
Claus Ibsen
......................................
Silverbullet
Skovsgårdsvænget 21
8362 Hørning
Tlf. +45 2962 7576
Web: www.silverbullet.dk

-----Original Message-----
From: buchnerm [mailto:markus.buchner@gmx.at] 
Sent: 21. juli 2008 14:31
To: camel-user@activemq.apache.org
Subject: how to use xpath in spring xml and query header fields


Hi 

I have the following issue:  I created a processor which sets depending on
the sessionid a header value in the exchange 
When I configure now a "choice" in spring xml which routes based on this
header field I get an exception:

how should the route look like? 
My code now: (xml)

<route>
			<from ref="input1" />
			<to uri="createProcessor" />
			<to uri="realtimeProcessor" />
			<to uri="queueSwitchProcessor" />
			<choice>
		      <when>
		        <xpath>$queueid = "0"</xpath>
		        <to ref="output1"/>
		      </when>
		      <when>
		        <xpath>$queueid = "1"</xpath>
		        <to ref="output2"/>
		      </when>
		      <otherwise>
		        <to ref="output3"/>
		      </otherwise>
		    </choice>
</route>

My code of the processor (queueSwitchProcessor):

public void process(Exchange exchange) throws Exception {
		
		ObjectImpressionTransportBean transportBean =
(ObjectImpressionTransportBean)(exchange.getIn().getBody());
		String sessionid =
transportBean.getSensorCall().getParameter().get(PARAMETERKEY_SESSIONID);
		if (sessionid != null) {
			int queueid = sessionid.hashCode();
			if (queueid < 0) {queueid = queueid * -1;}
			queueid = queueid%numberOfConsumers;
			exchange.getIn().setHeader("queueid", Integer.toString(queueid));
		} else {
			exchange.getIn().setHeader("queueid", "0");
		}		
	}

how do I access the header field "queueid" via xpath???

now i get this exception: (because its trying to access the body!!!)  

2008-07-21 14:09:39,170 ERROR [DefaultMessageListenerContainer-17]
org.apache.camel.processor.DeadLetterChannel: Failed delivery for
exchangeId: ID-power/2826-1216642175780/0-8. On delivery attempt: 2 caught:
java.lang.ClassCastException:
com.fatwire.analytics.domain.ObjectImpressionTransportBean
java.lang.ClassCastException:
com.fatwire.analytics.domain.ObjectImpressionTransportBean
	at
com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(XPathExpressionImpl.java:115)
	at
com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(XPathExpressionImpl.java:97)
	at
com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:178)
	at
org.apache.camel.builder.xml.XPathBuilder.evaluateAs(XPathBuilder.java:429)
	at org.apache.camel.builder.xml.XPathBuilder.matches(XPathBuilder.java:98)
	at org.apache.camel.builder.xml.XPathBuilder.matches(XPathBuilder.java:63)
	at
org.apache.camel.processor.ChoiceProcessor.process(ChoiceProcessor.java:47)
-- 
View this message in context: http://www.nabble.com/how-to-use-xpath-in-spring-xml-and-query-header-fields-tp18567110s22882p18567110.html
Sent from the Camel - Users mailing list archive at Nabble.com.