You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by GSegel <gs...@itsfac.com> on 2010/07/29 22:36:15 UTC

Help with filter behavior

I am trying to build a route using Java DSL and I'm getting a confusing error
in my IDE (IntelliJ).  Here's what I'm trying to do:
1. Receive a message from a JMS Queue
2. Enrich the message by querying a database to retrieve additional content
(claim check pattern)
3. Filter the messages to exclude any 'test' messages
4. Print to the console

Here's my Java DSL:

public class MyRouteBuilder extends RouteBuilder {

  public void configure() throws Exception {
    from("jms:xmlOrders")
      .bean(enricher, "enrichData")
      .filter().xpath("/order[not(@test)]")
      .to("stream:out");   // ERROR on this line in IDE
  }
}


The IDE highlights the last line (.to("stream:out");) as an error and I'm
having a hard time understanding why.  If I remove the bean doing the
enrichment it works fine.  ie:

from("jms:xmlOrders")
      //.bean(enricher, "enrichData")  COMMENTED OUT
      .filter().xpath("/order[not(@test)]")
      .to("stream:out");   // NOW everything is fine

I also tried replacing the bean with a new Processor that does the same
thing in case the issue has to do with keeping the Exchange in tact but it
still didn't make the error go away.


I'm sure this is how it's supposed to work and is due to a lack of my
understanding.  I would be grateful if anyone could help me understand why
this is happening!

-- 
View this message in context: http://camel.465427.n5.nabble.com/Help-with-filter-behavior-tp2256745p2256745.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Help with filter behavior

Posted by GSegel <gs...@itsfac.com>.
I was using Camel 2.3.  I switched to version 2.4 as you recommended and the
error highlighting went away.

With version 2.4, it now works for '.filter().xpath("/order[not(@test)]")'
and also for Claus's suggestion of '.filter(xpath("/order[not(@test)]"))'.

Before trying the 2.4 version, I put the xpath logic in a custom Processor
which updates the header based on the xpath expression evaluations (of which
there could be many).  Based on the header, the route will send it
accordingly.  

I wanted to use the built-in filtering/evaluation support in Camel so I may
come back and put the filtering in the route itself (instead of using my
Processor).  Camel is so flexible that I find it difficult to decide when to
use one approach over another!

I really appreciate the help!
-- 
View this message in context: http://camel.465427.n5.nabble.com/Help-with-filter-behavior-tp2256745p2261890.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Help with filter behavior

Posted by Willem Jiang <wi...@gmail.com>.
Hi

Which version of Camel are you using ?
I tried you DSL within Camel trunk code, the IDE (Eclipse) didn't 
complain anything.

Can you try the latest released Camel 2.4.0 for it?

Willem
----------------------------------
Apache Camel, Apache CXF committer
Open Source Integration http://www.fusesource.com
Blog http://willemjiang.blogspot.com
Tiwtter http://twitter.com/willemjiang

GSegel wrote:
> I am trying to build a route using Java DSL and I'm getting a confusing error
> in my IDE (IntelliJ).  Here's what I'm trying to do:
> 1. Receive a message from a JMS Queue
> 2. Enrich the message by querying a database to retrieve additional content
> (claim check pattern)
> 3. Filter the messages to exclude any 'test' messages
> 4. Print to the console
> 
> Here's my Java DSL:
> 
> public class MyRouteBuilder extends RouteBuilder {
> 
>   public void configure() throws Exception {
>     from("jms:xmlOrders")
>       .bean(enricher, "enrichData")
>       .filter().xpath("/order[not(@test)]")
>       .to("stream:out");   // ERROR on this line in IDE
>   }
> }
> 
> 
> The IDE highlights the last line (.to("stream:out");) as an error and I'm
> having a hard time understanding why.  If I remove the bean doing the
> enrichment it works fine.  ie:
> 
> from("jms:xmlOrders")
>       //.bean(enricher, "enrichData")  COMMENTED OUT
>       .filter().xpath("/order[not(@test)]")
>       .to("stream:out");   // NOW everything is fine
> 
> I also tried replacing the bean with a new Processor that does the same
> thing in case the issue has to do with keeping the Exchange in tact but it
> still didn't make the error go away.
> 
> 
> I'm sure this is how it's supposed to work and is due to a lack of my
> understanding.  I would be grateful if anyone could help me understand why
> this is happening!
> 


Re: Help with filter behavior

Posted by Claus Ibsen <cl...@gmail.com>.
On Mon, Aug 2, 2010 at 5:50 PM, GSegel <gs...@itsfac.com> wrote:
>
> Claus- Thanks for taking the time to respond!
>
> I upgraded to 2.4 and my issue is resolved but just to follow-up, I tried
> your suggestion of using '.filter(xpath("/order[not(@test)]"))' but it still
> didn't work with 2.3.
>

In Camel 2.3 you need to static import that xpath method from
org.apache.camel.builder.xml.XPathBuilder
In Camel 2.4 xpath is provided out of the box in RouteBuilder class so
no need for the static import.



> I'm seeing that using Java to define the routes is limiting but the
> application we're creating allows on-the-fly route building/configuration
> so, maybe mistakenly, I saw this as the best approach.  The only alternative
> I see is to create the routes manually (building routes using
> RouteDefinition, ProcessorDefinition, etc).  Would love to hear any thoughts
> you may have on this...
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Help-with-filter-behavior-tp2256745p2261902.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



-- 
Claus Ibsen
Apache Camel Committer

Author of Camel in Action: http://www.manning.com/ibsen/
Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus

Re: Help with filter behavior

Posted by GSegel <gs...@itsfac.com>.
Claus- Thanks for taking the time to respond!

I upgraded to 2.4 and my issue is resolved but just to follow-up, I tried
your suggestion of using '.filter(xpath("/order[not(@test)]"))' but it still
didn't work with 2.3. 

I'm seeing that using Java to define the routes is limiting but the
application we're creating allows on-the-fly route building/configuration
so, maybe mistakenly, I saw this as the best approach.  The only alternative
I see is to create the routes manually (building routes using
RouteDefinition, ProcessorDefinition, etc).  Would love to hear any thoughts
you may have on this...


-- 
View this message in context: http://camel.465427.n5.nabble.com/Help-with-filter-behavior-tp2256745p2261902.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Help with filter behavior

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

On Thu, Jul 29, 2010 at 10:36 PM, GSegel <gs...@itsfac.com> wrote:
>
> I am trying to build a route using Java DSL and I'm getting a confusing error
> in my IDE (IntelliJ).  Here's what I'm trying to do:
> 1. Receive a message from a JMS Queue
> 2. Enrich the message by querying a database to retrieve additional content
> (claim check pattern)
> 3. Filter the messages to exclude any 'test' messages
> 4. Print to the console
>
> Here's my Java DSL:
>
> public class MyRouteBuilder extends RouteBuilder {
>
>  public void configure() throws Exception {
>    from("jms:xmlOrders")
>      .bean(enricher, "enrichData")
>      .filter().xpath("/order[not(@test)]")
>      .to("stream:out");   // ERROR on this line in IDE
>  }
> }
>
>
> The IDE highlights the last line (.to("stream:out");) as an error and I'm
> having a hard time understanding why.  If I remove the bean doing the
> enrichment it works fine.  ie:
>
> from("jms:xmlOrders")
>      //.bean(enricher, "enrichData")  COMMENTED OUT
>      .filter().xpath("/order[not(@test)]")
>      .to("stream:out");   // NOW everything is fine
>
> I also tried replacing the bean with a new Processor that does the same
> thing in case the issue has to do with keeping the Exchange in tact but it
> still didn't make the error go away.
>

The issue is Java is generally not a good language to do DSL. And we
have stretched out how far you can go with using generics under the
hood as the DSL.
So there are situations where the java compiler goes "nuts".

The problem is centered when you use predicates/expressions as stacked
method calls. Instead use predicates/expressions as parameters.

      .filter(xpath("/order[not(@test)]"))

Notice how xpath predicate is now a parameter to the filter method.
Then the Java DSL and generics should play well.



>
> I'm sure this is how it's supposed to work and is due to a lack of my
> understanding.  I would be grateful if anyone could help me understand why
> this is happening!
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Help-with-filter-behavior-tp2256745p2256745.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



-- 
Claus Ibsen
Apache Camel Committer

Author of Camel in Action: http://www.manning.com/ibsen/
Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus