You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by "peacock.snowdrift" <ma...@arcinnovations.co.nz> on 2013/08/19 04:00:51 UTC

Conditional routing and local variables

Hello,

Complete Camel newbie here, so apologies if this is a silly question.

I have a piece of code along the lines of:

if (localVar == value) {
    from(uri)
    ... stuff here...
    .to(destination);
    .to(additionalDestination);
} else {
    from(uri)
    ... stuff here...
    .to(destination)
}

I've tried using a Predicate on the localVar value to tidy up this
definition - as I'm duplicating code with 2 virtually identical routes apart
from small differences otherwise - but can't seem to access it:

Predicate predicate = {what goes here?};

from(uri)
... stuff here...
.to(destination)
.choice()
.when(predicate).to(additionalDestination);

Can I do so, or is there a better way of making this statement more
graceful?

Thanks for any assistance.



--
View this message in context: http://camel.465427.n5.nabble.com/Conditional-routing-and-local-variables-tp5737490.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Conditional routing and local variables

Posted by Willem jiang <wi...@gmail.com>.
I think you need to fill the gap of design time and run time.
Usually, Camel predicate just check the run time exchange to decide to return true or false.
But you can write a custom Predicate just check the localVar value and ignore the exchange parameter to implement the route feature that you want.  


--  
Willem Jiang

Red Hat, Inc.
Web: http://www.redhat.com
Blog: http://willemjiang.blogspot.com (http://willemjiang.blogspot.com/) (English)
          http://jnn.iteye.com (http://jnn.javaeye.com/) (Chinese)
Twitter: willemjiang  
Weibo: 姜宁willem





On Monday, August 19, 2013 at 10:00 AM, peacock.snowdrift wrote:

> Hello,
>  
> Complete Camel newbie here, so apologies if this is a silly question.
>  
> I have a piece of code along the lines of:
>  
> if (localVar == value) {
> from(uri)
> ... stuff here...
> .to(destination);
> .to(additionalDestination);
> } else {
> from(uri)
> ... stuff here...
> .to(destination)
> }
>  
> I've tried using a Predicate on the localVar value to tidy up this
> definition - as I'm duplicating code with 2 virtually identical routes apart
> from small differences otherwise - but can't seem to access it:
>  
> Predicate predicate = {what goes here?};
>  
> from(uri)
> ... stuff here...
> .to(destination)
> .choice()
> .when(predicate).to(additionalDestination);
>  
> Can I do so, or is there a better way of making this statement more
> graceful?
>  
> Thanks for any assistance.
>  
>  
>  
> --
> View this message in context: http://camel.465427.n5.nabble.com/Conditional-routing-and-local-variables-tp5737490.html
> Sent from the Camel - Users mailing list archive at Nabble.com (http://Nabble.com).




Re: Conditional routing and local variables

Posted by "peacock.snowdrift" <ma...@arcinnovations.co.nz>.
pontus.ullgren wrote
> from(uri)
> ... stuff here...
> .to(destination)
> .choice()
> .when(constant(localVar).isEqualTo("value")).to(additionalDestination);

Thank you.



--
View this message in context: http://camel.465427.n5.nabble.com/Conditional-routing-and-local-variables-tp5737490p5737542.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Conditional routing and local variables

Posted by Pontus Ullgren <ul...@gmail.com>.
On Mon, Aug 19, 2013 at 10:41 PM, peacock.snowdrift <
mark.harris@arcinnovations.co.nz> wrote:

> Claus Ibsen-2 wrote
> > A good idea is to check the docs
> > http://camel.apache.org/predicate.html
> >
> > For example you can use the front page of Apache Camel
> > http://camel.apache.org/
> >
> > And type in keywords in the search box.
> >
> > As well read some of the intro guides and whatnot.
> > There is also a lot of links to external Camel intro and resources at
> > http://camel.apache.org/articles
>
> Hi,
>
> I have looked through the documentation as you suggest but still cannot
> find
> the syntax for referring to local variables with predicates - all the
> examples I have found look at the exchange's headers.
>
> Thanks.
>

Hello,

As Willem points out your mixing design time and runtime.

Predicates is mostly intended for runtime hence most examples works on
exchange headers.

However if you want to use predicts I believe you can use the constant()
method in your predicate.
---
from(uri)
... stuff here...
.to(destination)
.choice()
.when(constant(localVar).isEqualTo("value")).to(additionalDestination);
---
Or since you are doing all this in design time you can also build the route
normal if statements (as you do) but use a local route definition variable
to avoid having to duplicating code .
---
RouteDefinition myRoute = from(uri)
 .... stuff here...
.to(destination):

if ( localVar == value) {
  myRoute.to(additionalDestination);
} else {
  myRoute.to(someotherDestination);
}

myRoute.to(commonFinalDestination);
---

Best regards
Pontus Ullgren

Re: Conditional routing and local variables

Posted by "peacock.snowdrift" <ma...@arcinnovations.co.nz>.
Claus Ibsen-2 wrote
> A good idea is to check the docs
> http://camel.apache.org/predicate.html
> 
> For example you can use the front page of Apache Camel
> http://camel.apache.org/
> 
> And type in keywords in the search box.
> 
> As well read some of the intro guides and whatnot.
> There is also a lot of links to external Camel intro and resources at
> http://camel.apache.org/articles

Hi,

I have looked through the documentation as you suggest but still cannot find
the syntax for referring to local variables with predicates - all the
examples I have found look at the exchange's headers.

Thanks.



--
View this message in context: http://camel.465427.n5.nabble.com/Conditional-routing-and-local-variables-tp5737490p5737535.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Conditional routing and local variables

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

A good idea is to check the docs
http://camel.apache.org/predicate.html

For example you can use the front page of Apache Camel
http://camel.apache.org/

And type in keywords in the search box.

As well read some of the intro guides and whatnot.
There is also a lot of links to external Camel intro and resources at
http://camel.apache.org/articles



On Mon, Aug 19, 2013 at 4:00 AM, peacock.snowdrift
<ma...@arcinnovations.co.nz> wrote:
> Hello,
>
> Complete Camel newbie here, so apologies if this is a silly question.
>
> I have a piece of code along the lines of:
>
> if (localVar == value) {
>     from(uri)
>     ... stuff here...
>     .to(destination);
>     .to(additionalDestination);
> } else {
>     from(uri)
>     ... stuff here...
>     .to(destination)
> }
>
> I've tried using a Predicate on the localVar value to tidy up this
> definition - as I'm duplicating code with 2 virtually identical routes apart
> from small differences otherwise - but can't seem to access it:
>
> Predicate predicate = {what goes here?};
>
> from(uri)
> ... stuff here...
> .to(destination)
> .choice()
> .when(predicate).to(additionalDestination);
>
> Can I do so, or is there a better way of making this statement more
> graceful?
>
> Thanks for any assistance.
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Conditional-routing-and-local-variables-tp5737490.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen