camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Hadrian Zbarcea <hzbar...@gmail.com>
Subject Re: Camel Pojo Routing
Date Thu, 02 Aug 2007 02:47:54 GMT
Hi Roshnan,

You are right that the root of the problem is that the "body() function 
only returns a PojoInvocation and not the actual argument".  However 
this is what you asked camel to do.  As you are dealing with a POJO, you 
are not interested in the whole PojoInvocation, but a particular 
argument (could have been more than one) or you could have been 
interested in the method name to make the choice.  So you might want to 
write your code like this:

        context.addRoutes(new RouteBuilder(context) {
            public void configure() {
                
from("direct:hello").choice().when(argument(0).isEqualTo("One"))
                    .to("pojo:one").otherwise().to("pojo:two");
            }

            private ValueBuilder<PojoExchange> argument(int index) {
                Expression<PojoExchange> expression = 
PojoExpressionBuilder.argumentExpression(index);
                return new ValueBuilder<PojoExchange>(expression);
            }
        });

Please note that the Predicate for your choice is now "when the 1st 
argument of my pojo method call is equal to 'One'".  You would need 
though an Expression that evaluates to the n-th argument of a pojo 
method call to implement the argument method as above.  Camel does not 
provide such as Expression, but here is some code that does the job (a 
method that creates an Expression that evaluates to the method name is 
also provided).

import org.apache.camel.Expression;

public class PojoExpressionBuilder {

    public static Expression<PojoExchange> methodExpression() {
        return new Expression<PojoExchange>() {
            public Object evaluate(PojoExchange exchange) {
                PojoInvocation body = 
(PojoInvocation)exchange.getIn().getBody();
                if (body != null) {
                    return body.getMethod().getName();
                }
                return null;
            }


            @Override
            public String toString() {
                return "method";
            }
        };
    }
   
    public static Expression<PojoExchange> argumentExpression(final int 
index) {
        return new Expression<PojoExchange>() {
            public Object evaluate(PojoExchange exchange) {
                PojoInvocation body = 
(PojoInvocation)exchange.getIn().getBody();
                if (body != null) {
                    return body.getArgs()[index];
                }
                return null;
            }


            @Override
            public String toString() {
                return "arg(" + index + ")";
            }
        };
    }
}


I hope this helps,
Hadrian


Punnoose, Roshan wrote:
> I couldn't get the forums to come up, it keeps timing out, so I have to
> ask this question on the mailing list.
>
>  
>
> I have two Pojo endpoints, and I want to route based on a String input.
> How do I do that? This is my code currently:
>
>  
>
> CamelContext context = new DefaultCamelContext();
>
>                         PojoComponent component1 = (PojoComponent)
> context.getComponent("pojo");
>
>                         component1.addService("one", new PojoOne());
>
>                         PojoComponent component2 = (PojoComponent)
> context.getComponent("pojo");
>
>                         component2.addService("two", new PojoTwo());
>
>  
>
>                         DirectComponent directComponent =
> (DirectComponent) context
>
>                                                 .getComponent("direct");
>
>                         directComponent.createEndpoint("hello");
>
>  
>
>                         context.addRoutes(new RouteBuilder() {
>
>                                     public void configure() {
>
>                                                 FromBuilder from =
> from("direct:hello");
>
>                                                 ChoiceBuilder choice =
> from.choice();
>
>                                                 WhenBuilder when =
> choice
>
>  
> .when(body().isEqualTo("One"));
>
>                                                 when.to("pojo:one")
>
>  
> .otherwise().to("pojo:two");
>
>                                     }
>
>                         });
>
>  
>
>                         context.start();
>
>  
>
>                         Endpoint endpoint =
> context.getEndpoint("direct:hello");
>
>                         PojoNumInterface pojo = (PojoNumInterface)
> PojoComponent.createProxy(
>
>                                                 endpoint,
> PojoNumInterface.class);
>
>                         pojo.execute("One");
>
>  
>
>  
>
> I'm guessing the body() function only returns a PojoInvocation and not
> the actual argument "One".
>
>  
>
> Roshan
>
>
>   

Mime
View raw message