You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Nikhil Shambhu <ni...@gmail.com> on 2012/02/22 17:41:03 UTC

How to create a dynamic route based on some properties

Hello



I am using camel 2.8.1



I am trying to create a route dynamically based on some properties in a
properties file which looks like this



Subscribers = s1,s2,s3   // A list of subscribers



// Following properties are repeated for each subscriber. I am posting only
one subscriber properties for brevity



S1.fromEndpoint=activemq:s1QueueName // The queue from which this route
would subscriber

S1.processor.bean = s1Processor // The processor that is invoked as step 1

S1.postProcess.steps=2 // The number of post process steps. This can be
zero or more



// The following properties are repeated for every post process step



S1.postprocess1.processor = s11Processor // Processor for step 1

S1.postprocess1.url = direct:s11URL // URL to which the processed request
is to be sent. Step 1



S1.postprocess2.processor = s1Processor (I can re-use an existing processor
if I want) // Step 2 of post processing. This processor would have access
to the result of Step3. Does more processing based on the result

S1.postprocess1.url = direct:s12URL // Step 2 of post processing. Send the
processed request to this url





I am creating routes dynamically by iterating through the list of
subscribers.



Subscribers.each { subscriber ->



from(subscriber.fromEndpoint)

.processRef(subscriber.processor.bean)

*.processRef(subscriber.postprocess1.processor) // Step 1 processor*

*.to(subscriber.postprocess1.url) // Step 1 URL*

*.processRef(subscriber.postprocess2.processor) // Step 2 processor*

*.to(subscriber.postprocess2.url) // Step2 URL*

* *

}

* *

I wish I could write a “for” loop and reproduce the “processRef” and “to”
combinations based on the number of steps specified in the properties file.
The “loop” construct in camel is capable of sending the exchange to the
same processor+url combination everytime



Is there a way to do something like this





Subscribers.each { subscriber ->



Int i = 1



from(subscriber.fromEndpoint)

.processRef(subscriber.processor.bean)

*.loop(subscriber. postProcess.steps)      // <##################  Such
looping the missing piece in my scenario*

*.processRef(subscriber.postprocess${i++}.processor)*

*.to(subscriber.postprocess${i++}.url)*

* *

}



I have tried to be as descriptive as possible. Please let me know if there
is any more information you need.

* *

Thanks in advance
Nikhil

Re: How to create a dynamic route based on some properties

Posted by nikhil <ni...@gmail.com>.
Got this response in IPF group which worked for me.

Subscribers.each { subscriber -> 
    def node = from(subscriber.fromEndpoint) 
        .processRef(subscriber.processor.bean) 
    for (int i = 1; i <= subscriber.postProcess.steps; ++i) { 
        node = node.processRef(subscriber.postprocess${i}.processor) 
                   .to(subscriber.postprocess${i}.url) 
    } 
} 

--
View this message in context: http://camel.465427.n5.nabble.com/How-to-create-a-dynamic-route-based-on-some-properties-tp5505605p5512463.html
Sent from the Camel - Users mailing list archive at Nabble.com.