You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Chris Wolf <cw...@gmail.com> on 2013/04/01 19:59:52 UTC

Content-based routing with Expressions and Predicates

(I took the liberty to change the Subject since the message history has both my
 FTP issue and Prabu's routing issue - my fault, sorry)

Prabu, I strongly recommend getting and reading Claus's book "Camel In Action",
like Thomas Walzer suggested upon your first post.  You also should be
familiar with the concepts on these pages:

http://camel.apache.org/content-based-router.html
http://camel.apache.org/predicate.html
http://camel.apache.org/expression.html
http://camel.apache.org/languages.html
(and this is the page I mentioned before, but couldn't find at the time...)
http://camel.apache.org/why-can-i-not-use-when-or-otherwise-in-a-java-camel-route.html


...but, really, everything you're asking for is answered on this one page:
http://camel.apache.org/predicate.html

...in any case, I think your example 1 & 2 would be
implemented something as follows:

(Be advised, Gmail forces lines wraps around column 72 or so,
 so the code formatting will be mangled)


Don't forget to statically import the predicates you will be using in
compound expressions..

import static org.apache.camel.builder.PredicateBuilder.and;
import static org.apache.camel.builder.PredicateBuilder.or;

Predicate cardEvent = PredicateBuilder.regex(header("Type"), "^card\\.event.*");

from("jms:Inbound.Test.DDD").choice()
  // Your "Example 1"
.when(and(property("PAST_EVENT_INTERVAL").isEqualTo("0"),
                or(header("Type").isEqualTo("card.test"),
                    header("Type").isEqualTo("card.expiry.test")
         ))).to("jms:SOUTHQueueName")
  // Your "Example 2"
.when(and(property("PAST_EVENT_INTERVAL").isEqualTo("0"), cardEvent))
.to("jms:WESTQueueName")
.otherwise().to("jms:dead.letter.queue")
.end();

I also recommend that you not use a header named "Type", since that's
easily confused with
data type or type parameter.  Maybe some like CARD_OP...

Regards,

Chris


On Mon, Apr 1, 2013 at 11:36 AM, prabumca06@gmail.com
<pr...@gmail.com> wrote:
>
> Hi *Chris / **Claus*,
>
> I have one *Inbound* queue,based on header and content type,I want forward
> to outbound queue.
> *
> Example 1:
> *
> *
> Inbound.queue name : **Inbound.Test.DDD
> Outbound.queue name : SOURHQueueName
>
> *
> *Criteria or Condition :
> *
> Message property *PAST_EVENT_INTERVAL type* is : *0*
>   *AND*
> Message *Type* value is : *card.test *or* card.expiry.test*
>
> *Example 2:*
>
> *Inbound.queue name : **Inbound.Test.SSS
> Outbound.queue name : WESTQueueName
> *
> *Criteria or Condition :*
> Message property *PAST_EVENT_INTERVAL type* is : *0*
>  *AND
> *
> Message *Type *value *start with* :*card.event*
>
> I want achieve this using camel routing java DSL.
>
> Please help me.How can i use AND,OR and regular expression in camel routing
>
> *Regards*
> Prabu.N
>
> > >> >> On Sun, Mar 31, 2013 at 5:22 AM, [hidden email]<http://user/SendEmail.jtp?type=node&node=5730166&i=4>
> > >> >> <[hidden email]<http://user/SendEmail.jtp?type=node&node=5730166&i=5>>wrote:
> >
> > >> >>
> > >> >>> Thanks Walzer..
> > >> >>> *
> > >> >>> *
> > >> >>> *Queston1:
> > >> >>>
> > >> >>> *
> > >> >>> *Example*
> > >> >>>
> > >> >>> .when(header("foo").isEqualTo("bar"))
> > >> >>>                     .to("direct:b")
> > >> >>>
> > >> >>> In my case i have to add more than 80 condition and forwards to 80
> > >> >>> different queues.
> > >> >>>
> > >> >>> Above example i can only hard-code queue name,But what i want is
> > below.
> > >> >>>
> > >> >>> * .when(header("Type").isNotNull())
> > >> >>>     .to("activemq.Inbound."+header("Type"))
> > >> >>> *
> > >> >>>
> > >> >>> Example:If queue type is test,It should forward to *Inbound.test*
> > >> queue.
> > >> >>>
> > >> >>> Above example i tried,but it did not worked,created queue something
> > >> >>> like this *Inbound.header("type")*
> > >> >>>
> > >> >>> *Question2*:
> > >> >>>
> > >> >>> from("direct:a")
> > >> >>>             .multicast().to("direct:b", "direct:c", "direct:d");
> > >> >>>
> > >> >>> Based on messages header and content type i want forward to
> > different
> > >> >>> queue.
> > >> >>>
> > >> >>> condition will be *OR *and *AND*.How can i do that like above
> > example.*
> > >> >>> *
> > >> >>>
> > >> >>> Thanks in advance*
> > >> >>> *
> > >> >>>
> > >> >>> *Regards*
> > >> >>>
> > >> >>> Prabu.N
> > >> >>>
> > >> >>>
> > >> >>>
> > >> >>>
> > >> >>>
> > >> >>>
> > >> >>>
> > >> >>>
> > >> >>>
> > >> >>>
> > >> >>>
> > >> >>>
> > >> >>>
> > >> >>> On Sun, Mar 31, 2013 at 2:43 AM, Walzer, Thomas [via Camel] <
> > >> >>> [hidden email]<http://user/SendEmail.jtp?type=node&node=5730166&i=6>>
> > wrote:
> > >> >>>
> > >> >>> > Hi,
> > >> >>> >
> > >> >>> > you could start by reading Claus´ book (p. 44ff) which has a
> > perfect
> > >> >>> > example or http://camel.apache.org/content-based-router.html (just
> >
> > >> >>> > replace the direct: with your queues).
> > >> >>> > If you need to fire your messages to multiple queues:
> > >> >>> > http://camel.apache.org/publish-subscribe-channel.html shows you
> > the
> > >> >>> > various ways.
> > >> >>> > When you get the concepts you can just "lego" them together.
> > >> >>> >
> > >> >>> > Regards, Thomas.
> > >> >>> >
> > >> >>> > Am 30.03.2013 um 19:05 schrieb "[hidden email]<
> > >> >>> http://user/SendEmail.jtp?type=node&node=5730116&i=0>"
> > >> >>> > <[hidden email] <
> > >> http://user/SendEmail.jtp?type=node&node=5730116&i=1>>:
> > >> >>> >
> > >> >>> > > Hi Claus,
> > >> >>> > >
> > >> >>> > > Thanks so much Clus for help.It is working now.
> > >> >>> > >
> > >> >>> > > I need your guidance for the following requirement.
> > >> >>> > >
> > >> >>> > > 1.I have one *inbound queue* it will receive message frequently
> > >> from my
> > >> >>> > > application.
> > >> >>> > > 2.From *inbound queue* i need camel routing to move message
> > from
> > >> >>> > > another *message
> > >> >>> > > based inbound queue* based on message header value.
> > >> >>> > > 3.From *message based inbound queue *i* *need camel routing to
> > move
> > >> >>> > message
> > >> >>> > > to *one or more outbound queue *based on message content*
> > >> >>> > > *
> > >> >>> > >
> > >> >>> > > I need *camel java DSL*sample code for above requirement.*
> > >> >>> > > *
> > >> >>> > > *
> > >> >>> > > *
> > >> >>> > > *Thanks in advance*
> > >> >>> > >
> > >> >>> > >
> > >> >>> > > On Sat, Mar 30, 2013 at 3:10 PM, Claus Ibsen-2 [via Camel] <
> > >> >>> > > [hidden email] <
> > >> http://user/SendEmail.jtp?type=node&node=5730116&i=2>>
> > >> >>> > wrote:
> > >> >>> > >
> > >> >>> > >> Hi
> > >> >>> > >>
> > >> >>> > >> Do as the exception tells you. Check route 2. You route from
> > >> >>> > >> "Inbound.SSS.TestEvent". And that is now know to Camel.
> > >> >>> > >>
> > >> >>> > >> Maybe you need to add "activemq:" as prefix so its
> > >> >>> > >> "activemq:Inbound.SSS.TestEvent".
> > >> >>> > >>
> > >> >>> > >>
> > >> >>> > >> On Fri, Mar 29, 2013 at 7:01 PM, [hidden email]<
> > >> >>> > http://user/SendEmail.jtp?type=node&node=5730109&i=0>
> > >> >>> > >> <[hidden email] <
> > >> http://user/SendEmail.jtp?type=node&node=5730109&i=1
> > >> >>> >>
> > >> >>> >
> > >> >>> > >> wrote:
> > >> >>> > >>
> > >> >>> > >>>
> > >> >>> > >>> I have activemmq  Project in that i have following
> > requirement
> > >> >>> > >>>
> > >> >>> > >>> 1.Move message from Inbound queue to type based inbound
> > queue.
> > >> >>> > >>> 2.Move message from Message type inbound queue to outbound
> > queue
> > >> >>> > >>>
> > >> >>> > >>> I have created two bean for to achieve this.
> > >> >>> > >>>
> > >> >>> > >>> 1.one for move from inbound to message type  inbound queue
> > >> >>> > >>> 2.one for move from inbound to outbound queue.
> > >> >>> > >>>
> > >> >>> > >>> I have following entry in camel.xml
> > >> >>> > >>>
> > >> >>> > >>>   <camelContext id="camel" xmlns="
> > >> >>> > http://camel.apache.org/schema/spring">
> > >> >>> > >>
> > >> >>> > >>>
> > >> >>> > >>>                <routeBuilder ref="routeBuilder" />
> > >> >>> > >>>            <routeBuilder ref="routeBuilder1" />
> > >> >>> > >>>
> > >> >>> > >>>    </camelContext>
> > >> >>> > >>>
> > >> >>> > >>>    <bean id="routeBuilder"
> > >> >>> > >>> class="com.camel.routes.SinglecastRouteBuilder"/>
> > >> >>> > >>>    <bean id="recipientsGenerator"
> > >> >>> > >>> class="com.camel.routes.TypeSpecificListBean"/>
> > >> >>> > >>>    <bean id="routeBuilder1"
> > >> >>> > >>> class="com.camel.routes.MulticastRouteBuilder"/>
> > >> >>> > >>>    <bean id="recipientsGenerator1"
> > >> >>> > >>> class="com.camel.routes.RecipientListBean"/>
> > >> >>> > >>>
> > >> >>> > >>> When i run application i am getting following error.
> > >> >>> > >>>
> > >> >>> > >>> * java.lang.Exception:
> > org.apache.camel.RuntimeCamelException:
> > >> >>> > >>> org.apache.camel.Fa
> > >> >>> > >>> iledToCreateRouteException: Failed to create route route2:
> > >> >>> > >>> Route[[From[Inbound.G
> > >> >>> > >>> MD.TestEvent]] -> [Multicast[[Bean[ref:... because of No
> > endpoint
> > >> >>> > could
> > >> >>> > >> be
> > >> >>> > >>> found
> > >> >>> > >>> for: Inbound.SSS.TestEvent, please check your classpath
> > contains
> > >> the
> > >> >>> > >> needed
> > >> >>> > >>> Cam
> > >> >>> > >>> el component jar.*
> > >> >>> > >>>
> > >> >>> > >>> I am not sure i am missing any configuration,Please kindly
> > help
> > >> me
> > >> >>> > >>>
> > >> >>> > >>>
> > >> >>> > >>>
> > >> >>> > >>>
> > >> >>> > >>> --
> > >> >>> > >>> View this message in context:
> > >> >>> > >>
> > >> http://camel.465427.n5.nabble.com/Camel-routing-issue-tp5730094.html
> > >> >>> > >>> Sent from the Camel - Users mailing list archive at
> > Nabble.com.
> > >> >>> > >>
> > >> >>> > >>
> > >> >>> > >>
> > >> >>> > >> --
> > >> >>> > >> Claus Ibsen
> > >> >>> > >> -----------------
> > >> >>> > >> Red Hat, Inc.
> > >> >>> > >> FuseSource is now part of Red Hat
> > >> >>> > >> Email: [hidden email]<
> > >> >>> > http://user/SendEmail.jtp?type=node&node=5730109&i=2>
> > >> >>> > >> Web: http://fusesource.com
> > >> >>> > >> Twitter: davsclaus
> > >> >>> > >> Blog: http://davsclaus.com
> > >> >>> > >> Author of Camel in Action: http://www.manning.com/ibsen
> > >> >>> > >>
> > >> >>> > >>
> > >> >>> > >> ------------------------------
> > >> >>> > >> If you reply to this email, your message will be added to the
> > >> >>> > discussion
> > >> >>> > >> below:
> > >> >>> > >>
> > >> >>> > >>
> > >> >>> >
> > >> >>> > >> .
> > >> >>> > >> NAML<
> > >> >>> >
> > >> >>>
> > >>
> > http://camel.465427.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml
> > >> >>> >
> > >> >>> >
> > >> >>> > >>
> > >> >>> > >
> > >> >>> > >
> > >> >>> > >
> > >> >>> > > --
> > >> >>> > > Thanks & Regards
> > >> >>> > > Prabu.N
> > >> >>> > >
> > >> >>> > >
> > >> >>> > >
> > >> >>> > >
> > >> >>> > > --
> > >> >>> > > View this message in context:
> > >> >>> >
> > >> >>>
> > >>
> > http://camel.465427.n5.nabble.com/Camel-routing-issue-tp5730094p5730115.html
> > >> >>> >
> > >> >>> > > Sent from the Camel - Users mailing list archive at Nabble.com.
> > >> >>> >
> > >> >>> >
> > >> >>> >
> > >> >>> > ------------------------------
> > >> >>> >  If you reply to this email, your message will be added to the
> > >> discussion
> > >> >>> > below:
> > >> >>> >
> > >> >>> >
> > >> >>>
> > >>
> > http://camel.465427.n5.nabble.com/Camel-routing-issue-tp5730094p5730116.html
> > >> >>> >  To unsubscribe from Camel routing issue, click here<
> > >> >>>
> > >> >>> >
> > >> >>> > .
> > >> >>> > NAML<
> > >> >>>
> > >>
> > http://camel.465427.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml
> > >> >>> >
> > >> >>> >
> > >> >>>
> > >> >>>
> > >> >>>
> > >> >>> --
> > >> >>> Thanks & Regards
> > >> >>>  Prabu.N
> > >> >>>
> > >> >>>
> > >> >>>
> > >> >>>
> > >> >>> --
> > >> >>> View this message in context:
> > >> >>>
> > >>
> > http://camel.465427.n5.nabble.com/Camel-routing-issue-tp5730094p5730118.html
> > >> >>> Sent from the Camel - Users mailing list archive at Nabble.com.
> > >> >>>
> > >> >>
> > >> >>
> > >> >>
> > >> >> --
> > >> >> Claus Ibsen
> > >> >> -----------------
> > >> >> Red Hat, Inc.
> > >> >> FuseSource is now part of Red Hat
> > >> >> Email: [hidden email]<http://user/SendEmail.jtp?type=node&node=5730166&i=7>
> > >> >> Web: http://fusesource.com
> > >> >> Twitter: davsclaus
> > >> >> Blog: http://davsclaus.com
> > >> >> Author of Camel in Action: http://www.manning.com/ibsen
> > >>
> > >
> > >
> > >
> > > --
> > > Claus Ibsen
> > > -----------------
> > > Red Hat, Inc.
> > > FuseSource is now part of Red Hat
> > > Email: [hidden email]<http://user/SendEmail.jtp?type=node&node=5730166&i=8>
> > > Web: http://fusesource.com
> > > Twitter: davsclaus
> > > Blog: http://davsclaus.com
> > > Author of Camel in Action: http://www.manning.com/ibsen
> >
> >
> > ------------------------------
> >  If you reply to this email, your message will be added to the discussion
> > below:
> >
> > http://camel.465427.n5.nabble.com/Camel-routing-issue-tp5730094p5730166.html
> >  To unsubscribe from Camel routing issue, click here<http://camel.465427.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=5730094&code=cHJhYnVtY2EwNkBnbWFpbC5jb218NTczMDA5NHw4OTY0MTU3ODg=>
> > .
> > NAML<http://camel.465427.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
> >
>
>
>
> --
> Thanks & Regards
>  Prabu.N
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Camel-routing-issue-tp5730094p5730168.html
> Sent from the Camel - Users mailing list archive at Nabble.com.