You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by ee7arh <an...@2e-systems.com> on 2009/02/27 17:22:30 UTC

Camel Routing based on bean return object

Hi,

I'm getting quite into using the Camel DSL router and got quite far but now
have this situation:

I have a bean method (myBeanMethod1()) which returns not a boolean but a
real object (MyReturnObj1). This object contains an Enum return code
(myReturnCode) and I want to do some routing based on this returncode.

e.g.

// Bean Method
public MyReturnObj1 myBeanMethod1();

The definition of MyReturnObj1 is

public class MyReturnObj1 {

protected Enum myReturnCode;

}

I want to do some routing like this:

from("jms:queue:queue1").to("bean:myBeanMethod1").choice()
.when(myErrorCode=myReturnCode.ERRROR1).to("jms:queue:error1Queue")
.when(myErrorCode=myReturnCode.ERROR2).to("jms:queue:error2Queue")
.otherwise().to("jms:queue:unknownErrorQueue");

Is it possible to somehow do routing based on bean which returns an object?
If I set my "returnCode" into the JMS Header field I could use the header()
method to get the values as seen in many examples on Camel website.

Advice greatly appreciated.

Andrew

-- 
View this message in context: http://www.nabble.com/Camel-Routing-based-on-bean-return-object-tp22249110p22249110.html
Sent from the Camel - Users (activemq) mailing list archive at Nabble.com.


Re: Camel Routing based on bean return object

Posted by "willem.jiang" <wi...@gmail.com>.
Hi Markus

I think we have some problems with this forum and the camel-user mail list.
The reply letter to the mail list come to this forum[1].

[1]http://www.nabble.com/Camel---Users-f36428.html

Willem


Markus Reil wrote:
> 
> Hi Andrew,
> 
> what I do is set the target queue as a header field in the bean:
> from(queue).bean(customRouterBean).recipientList(HEADER_TARGET_ROUTE); 
> 
> I know it is not that elegant to use queue name in the bean but this was
> the quickest way for me to make it work.
> Hope it helps.
> 
> Cheers,
> Markus
> 
> 

-- 
View this message in context: http://www.nabble.com/Camel-Routing-based-on-bean-return-object-tp22249110p22256561.html
Sent from the Camel - Users (activemq) mailing list archive at Nabble.com.


Re: Camel Routing based on bean return object

Posted by Markus Reil <gi...@gmx.de>.
Hi Andrew,

what I do is set the target queue as a header field in the bean:
from(queue).bean(customRouterBean).recipientList(HEADER_TARGET_ROUTE); 

I know it is not that elegant to use queue name in the bean but this was the
quickest way for me to make it work.
Hope it helps.

Cheers,
Markus

-- 
View this message in context: http://www.nabble.com/Camel-Routing-based-on-bean-return-object-tp22249110p22253813.html
Sent from the Camel - Users (activemq) mailing list archive at Nabble.com.


Re: Camel Routing based on bean return object

Posted by davsclaus <ci...@yahoo.dk>.
Hi

Just a quick updated on this one. 
I have commited a enhancment in CAMEL-1419 that allows predicates to use
type coercion for its binary operators. This allows you to test eg String
containing a number with an Integer and so on. It work with enum as well.

This allows to do content based routing a bit easier as the when() can test
even if the types dont match up. So you can test a boolean against "true"
etc.

This will be in Camel 2.0


Claus Ibsen-2 wrote:
> 
> On Fri, Feb 27, 2009 at 5:22 PM, ee7arh <an...@2e-systems.com>
> wrote:
>>
>> Hi,
>>
>> I'm getting quite into using the Camel DSL router and got quite far but
>> now
>> have this situation:
> Welcome onboard the ride.
> 
>>
>> I have a bean method (myBeanMethod1()) which returns not a boolean but a
>> real object (MyReturnObj1). This object contains an Enum return code
>> (myReturnCode) and I want to do some routing based on this returncode.
>>
>> e.g.
>>
>> // Bean Method
>> public MyReturnObj1 myBeanMethod1();
>>
>> The definition of MyReturnObj1 is
>>
>> public class MyReturnObj1 {
>>
>> protected Enum myReturnCode;
>>
>> }
>>
>> I want to do some routing like this:
>>
>> from("jms:queue:queue1").to("bean:myBeanMethod1").choice()
>> .when(myErrorCode=myReturnCode.ERRROR1).to("jms:queue:error1Queue")
>> .when(myErrorCode=myReturnCode.ERROR2).to("jms:queue:error2Queue")
>> .otherwise().to("jms:queue:unknownErrorQueue");
>>
>> Is it possible to somehow do routing based on bean which returns an
>> object?
>> If I set my "returnCode" into the JMS Header field I could use the
>> header()
>> method to get the values as seen in many examples on Camel website.
> Yes when you do routing with the content based router EIP pattern, you
> use the choice/when/otherwise as you do above.
> when and otherwise uses Predicate to evaluate true|false, if it should
> route it this way or continue down the other predicates.
> 
> Camel have build in nice DSL sugar for these predicates, aka predicate
> builders and others so you can build fluent syntax.
> 
> You can use a method call predicate to invoke a method on a bean, and
> then chain the sub predicate isEqualTo.
> 
> when(method(MyPredicateBean.class,
> "thismethod").isEqualTo(MyEnumConstant)).to("jms:xxx")
> 
> But this will invoke your bean on each predicate test and if you think
> this operation is very expensive, then you can do as above
> to route it to your bean prior the choice and set the result in a header.
> 
> Then the testing is just based using header("key") instead of the
> method call predicate.
> 
> 
> 
> 
>>
>> Advice greatly appreciated.
> 
> 
>>
>> Andrew
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Camel-Routing-based-on-bean-return-object-tp22249110p22249110.html
>> Sent from the Camel - Users (activemq) mailing list archive at
>> Nabble.com.
>>
>>
> 
> 
> 
> -- 
> Claus Ibsen
> Apache Camel Committer
> 
> Open Source Integration: http://fusesource.com
> Blog: http://davsclaus.blogspot.com/
> 
> 

-- 
View this message in context: http://www.nabble.com/Re%3A-Camel-Routing-based-on-bean-return-object-tp22249549p23610268.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: Camel Routing based on bean return object

Posted by ee7arh <an...@2e-systems.com>.
Hi,

Thanks for the reply. Can you pls check the syntax you wrote as it doesn't
look like it's allowed to have a "method()" function within a "when()". I
can only find a way to use it after a filter e.g.
filter().method(MyClass.class, "methodCall")

Regards
Andrew


Claus Ibsen-2 wrote:
> 
> On Fri, Feb 27, 2009 at 5:22 PM, ee7arh <an...@2e-systems.com>
> wrote:
>>
>> Hi,
>>
>> I'm getting quite into using the Camel DSL router and got quite far but
>> now
>> have this situation:
> Welcome onboard the ride.
> 
>>
>> I have a bean method (myBeanMethod1()) which returns not a boolean but a
>> real object (MyReturnObj1). This object contains an Enum return code
>> (myReturnCode) and I want to do some routing based on this returncode.
>>
>> e.g.
>>
>> // Bean Method
>> public MyReturnObj1 myBeanMethod1();
>>
>> The definition of MyReturnObj1 is
>>
>> public class MyReturnObj1 {
>>
>> protected Enum myReturnCode;
>>
>> }
>>
>> I want to do some routing like this:
>>
>> from("jms:queue:queue1").to("bean:myBeanMethod1").choice()
>> .when(myErrorCode=myReturnCode.ERRROR1).to("jms:queue:error1Queue")
>> .when(myErrorCode=myReturnCode.ERROR2).to("jms:queue:error2Queue")
>> .otherwise().to("jms:queue:unknownErrorQueue");
>>
>> Is it possible to somehow do routing based on bean which returns an
>> object?
>> If I set my "returnCode" into the JMS Header field I could use the
>> header()
>> method to get the values as seen in many examples on Camel website.
> Yes when you do routing with the content based router EIP pattern, you
> use the choice/when/otherwise as you do above.
> when and otherwise uses Predicate to evaluate true|false, if it should
> route it this way or continue down the other predicates.
> 
> Camel have build in nice DSL sugar for these predicates, aka predicate
> builders and others so you can build fluent syntax.
> 
> You can use a method call predicate to invoke a method on a bean, and
> then chain the sub predicate isEqualTo.
> 
> when(method(MyPredicateBean.class,
> "thismethod").isEqualTo(MyEnumConstant)).to("jms:xxx")
> 
> But this will invoke your bean on each predicate test and if you think
> this operation is very expensive, then you can do as above
> to route it to your bean prior the choice and set the result in a header.
> 
> Then the testing is just based using header("key") instead of the
> method call predicate.
> 
> 
> 
> 
>>
>> Advice greatly appreciated.
> 
> 
>>
>> Andrew
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Camel-Routing-based-on-bean-return-object-tp22249110p22249110.html
>> Sent from the Camel - Users (activemq) mailing list archive at
>> Nabble.com.
>>
>>
> 
> 
> 
> -- 
> Claus Ibsen
> Apache Camel Committer
> 
> Open Source Integration: http://fusesource.com
> Blog: http://davsclaus.blogspot.com/
> 
> 

-- 
View this message in context: http://www.nabble.com/Re%3A-Camel-Routing-based-on-bean-return-object-tp22249549p22284782.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: Camel Routing based on bean return object

Posted by Claus Ibsen <cl...@gmail.com>.
On Fri, Feb 27, 2009 at 5:22 PM, ee7arh <an...@2e-systems.com> wrote:
>
> Hi,
>
> I'm getting quite into using the Camel DSL router and got quite far but now
> have this situation:
Welcome onboard the ride.

>
> I have a bean method (myBeanMethod1()) which returns not a boolean but a
> real object (MyReturnObj1). This object contains an Enum return code
> (myReturnCode) and I want to do some routing based on this returncode.
>
> e.g.
>
> // Bean Method
> public MyReturnObj1 myBeanMethod1();
>
> The definition of MyReturnObj1 is
>
> public class MyReturnObj1 {
>
> protected Enum myReturnCode;
>
> }
>
> I want to do some routing like this:
>
> from("jms:queue:queue1").to("bean:myBeanMethod1").choice()
> .when(myErrorCode=myReturnCode.ERRROR1).to("jms:queue:error1Queue")
> .when(myErrorCode=myReturnCode.ERROR2).to("jms:queue:error2Queue")
> .otherwise().to("jms:queue:unknownErrorQueue");
>
> Is it possible to somehow do routing based on bean which returns an object?
> If I set my "returnCode" into the JMS Header field I could use the header()
> method to get the values as seen in many examples on Camel website.
Yes when you do routing with the content based router EIP pattern, you
use the choice/when/otherwise as you do above.
when and otherwise uses Predicate to evaluate true|false, if it should
route it this way or continue down the other predicates.

Camel have build in nice DSL sugar for these predicates, aka predicate
builders and others so you can build fluent syntax.

You can use a method call predicate to invoke a method on a bean, and
then chain the sub predicate isEqualTo.

when(method(MyPredicateBean.class,
"thismethod").isEqualTo(MyEnumConstant)).to("jms:xxx")

But this will invoke your bean on each predicate test and if you think
this operation is very expensive, then you can do as above
to route it to your bean prior the choice and set the result in a header.

Then the testing is just based using header("key") instead of the
method call predicate.




>
> Advice greatly appreciated.


>
> Andrew
>
> --
> View this message in context: http://www.nabble.com/Camel-Routing-based-on-bean-return-object-tp22249110p22249110.html
> Sent from the Camel - Users (activemq) mailing list archive at Nabble.com.
>
>



-- 
Claus Ibsen
Apache Camel Committer

Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/

Re: Camel Routing based on bean return object

Posted by Claus Ibsen <cl...@gmail.com>.
On Mon, Mar 2, 2009 at 9:47 AM, ee7arh <an...@2e-systems.com> wrote:
>
> Hi Markus,
>
> Thanks for advice. Yes that would work but as you pointed out it's not ideal
> since it's then not possible to see where the message will end up just by
> reading the Routing definition. I would like to make my RouteBuilder as
> readable as possible.
>
> Does you know whether it's possible to cast the "body()" of a message to
> it's base object?
>
> Perhaps this would work?
>
> from("jms:queue:queue1").to("bean:myBeanMethod1").choice()
> .when(body().convertTo(MyReturnCodeEnum.class).isEqualTo(MyReturnCodeEnum.ERROR_CODE_1))
> .to("jms:queue:error1Queue")
> .when(body().convertTo(MyReturnCodeEnum.class).isEqualTo(MyReturnCodeEnum.ERROR_CODE_2))
> .to("jms:queue:error2Queue")
>
> I assumed in above example that the actual object on the "from" queue is the
> MyReturnCodeEnum object now rather than the wrapping object MyReturnObj1
Ah now I got the issue. The predicate being tested using isEqualTo
does not automatic convert to same types.
I worked on that a bit, but then realized we cant make a safe
assumption which side of the operator RIGHT or LEFT should be
converter to the other type.

How should camel know which types is correct.

An example:
The left type (= body) can be an Integer with a value of 1. And the
left side is the enum with the ERROR_CODE_1 value.

The two types are not directly comparable so we need to convert it.
But which type is the best?
- Integer
- Enum

In this case it should be enum, but there could be other situations
where the predicate should test for reverse.

Maybe its possible to add tests for all combinations and return true
if there is a match in one of them?



So you can do the body stuff you do, but I think there is a shorthand
when(body(MyReturnCodeEnum.class).isEqualTo(ERROR_CODE_1)

And do a static import of your enum so its shorter to write.



>
> Thanks
> Andrew
>
>
> ee7arh wrote:
>>
>> Hi,
>>
>> I'm getting quite into using the Camel DSL router and got quite far but
>> now have this situation:
>>
>> I have a bean method (myBeanMethod1()) which returns not a boolean but a
>> real object (MyReturnObj1). This object contains an Enum return code
>> (myReturnCode) and I want to do some routing based on this returncode.
>>
>> e.g.
>>
>> // Bean Method
>> public MyReturnObj1 myBeanMethod1();
>>
>> The definition of MyReturnObj1 is
>>
>> public class MyReturnObj1 {
>>
>> protected Enum myReturnCode;
>>
>> }
>>
>> I want to do some routing like this:
>>
>> from("jms:queue:queue1").to("bean:myBeanMethod1").choice()
>> .when(myErrorCode=myReturnCode.ERRROR1).to("jms:queue:error1Queue")
>> .when(myErrorCode=myReturnCode.ERROR2).to("jms:queue:error2Queue")
>> .otherwise().to("jms:queue:unknownErrorQueue");
>>
>> Is it possible to somehow do routing based on bean which returns an
>> object? If I set my "returnCode" into the JMS Header field I could use the
>> header() method to get the values as seen in many examples on Camel
>> website.
>>
>> Advice greatly appreciated.
>>
>> Andrew
>>
>>
>
> --
> View this message in context: http://www.nabble.com/Camel-Routing-based-on-bean-return-object-tp22249110p22283427.html
> Sent from the Camel - Users (activemq) mailing list archive at Nabble.com.
>
>



-- 
Claus Ibsen
Apache Camel Committer

Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/

Re: Camel Routing based on bean return object

Posted by ee7arh <an...@2e-systems.com>.
Hi Markus,

Thanks for advice. Yes that would work but as you pointed out it's not ideal
since it's then not possible to see where the message will end up just by
reading the Routing definition. I would like to make my RouteBuilder as
readable as possible.

Does you know whether it's possible to cast the "body()" of a message to
it's base object?

Perhaps this would work?

from("jms:queue:queue1").to("bean:myBeanMethod1").choice()
.when(body().convertTo(MyReturnCodeEnum.class).isEqualTo(MyReturnCodeEnum.ERROR_CODE_1))
.to("jms:queue:error1Queue")
.when(body().convertTo(MyReturnCodeEnum.class).isEqualTo(MyReturnCodeEnum.ERROR_CODE_2))
.to("jms:queue:error2Queue")

I assumed in above example that the actual object on the "from" queue is the
MyReturnCodeEnum object now rather than the wrapping object MyReturnObj1

Thanks
Andrew


ee7arh wrote:
> 
> Hi,
> 
> I'm getting quite into using the Camel DSL router and got quite far but
> now have this situation:
> 
> I have a bean method (myBeanMethod1()) which returns not a boolean but a
> real object (MyReturnObj1). This object contains an Enum return code
> (myReturnCode) and I want to do some routing based on this returncode.
> 
> e.g.
> 
> // Bean Method
> public MyReturnObj1 myBeanMethod1();
> 
> The definition of MyReturnObj1 is
> 
> public class MyReturnObj1 {
> 
> protected Enum myReturnCode;
> 
> }
> 
> I want to do some routing like this:
> 
> from("jms:queue:queue1").to("bean:myBeanMethod1").choice()
> .when(myErrorCode=myReturnCode.ERRROR1).to("jms:queue:error1Queue")
> .when(myErrorCode=myReturnCode.ERROR2).to("jms:queue:error2Queue")
> .otherwise().to("jms:queue:unknownErrorQueue");
> 
> Is it possible to somehow do routing based on bean which returns an
> object? If I set my "returnCode" into the JMS Header field I could use the
> header() method to get the values as seen in many examples on Camel
> website.
> 
> Advice greatly appreciated.
> 
> Andrew
> 
> 

-- 
View this message in context: http://www.nabble.com/Camel-Routing-based-on-bean-return-object-tp22249110p22283427.html
Sent from the Camel - Users (activemq) mailing list archive at Nabble.com.