You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by mamouros <cs...@yahoo.gr> on 2013/10/01 17:58:17 UTC

Creating routes dynamically like a for()

I would like to create a route for every query my sql route returns.
>From this:

from("sql:" + fromUri + "?dataSource=dataSource")
.to("bean:SmsReceiver?method=creatingRoutes");

I want to make something hypothetically like this:

for( from("sql:" + fromUri + "?dataSource=dataSource") )  {
    
     .to("bean:SmsReceiver?method=creatingRoutes");

}

so if I have 3 entries in my table, I would create 3 routes.

How is that possible. Is there any EIP that does that?



--
View this message in context: http://camel.465427.n5.nabble.com/Creating-routes-dynamically-like-a-for-tp5740703.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Creating routes dynamically like a for()

Posted by gudiseashok <gu...@gmail.com>.
Now if you check my reply I have added all routes dynamically by specifying camel context and route id for each route, if you do so and keep track of ids you have created for routes,

You can have another bean to do this
Get camelcontext (camelcontextid)
Get routes to stop by I'd,
Stop route


That's it. I have snippet too if you want further information.

"mamouros [via Camel]" <ml...@n5.nabble.com> wrote:

>	I haven't checked gudiseashok's solution but I ended up with the solution of multiple threads created after a from("sql:...").to("bean:...) where inside the bean's calling function is a for loop to create threads where the run() method of the Runnable is something like: 
>
>run() { 
>         camelContext.addRoutes(new RouteBuilder() { 
>                        public void configure() { 
>                                 from("smpp://....") 
>                                 to(....) 
>                        } 
>        }); 
>} 
>
>
>thus creating my smpp receivers dynamically. 
>
>Now I only have to figure out a way to stop those routes and threads.. 	 	 	 	
>	
>	 	
>
>		
>
>If you reply to this email, your message will be added to the discussion below:
>
>		http://camel.465427.n5.nabble.com/Creating-routes-dynamically-like-a-for-tp5740703p5741217.html 	
>
>	
>
>		 		To unsubscribe from Creating routes dynamically like a for(), click here.
>		NAML 	
>




--
View this message in context: http://camel.465427.n5.nabble.com/Creating-routes-dynamically-like-a-for-tp5740703p5741231.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Creating routes dynamically like a for()

Posted by mamouros <cs...@yahoo.gr>.
I haven't checked gudiseashok's solution but I ended up with the solution of
multiple threads created after a from("sql:...").to("bean:...) where inside
the bean's calling function is a for loop to create threads where the run()
method of the Runnable is something like:

run() {
         camelContext.addRoutes(new RouteBuilder() {
			public void configure() {
                                 from("smpp://....")
                                 to(....)
                        }
        });
}


thus creating my smpp receivers dynamically.

Now I only have to figure out a way to stop those routes and threads..



--
View this message in context: http://camel.465427.n5.nabble.com/Creating-routes-dynamically-like-a-for-tp5740703p5741217.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Creating routes dynamically like a for()

Posted by gudiseashok <gu...@gmail.com>.
See if this helps you ...

//Calling to create a router - or loop this 

AppsRouteBuilder.getInstance().addRouter(camelContext, "{add router id}",
true,
				"{add your "*from*" string here}",	new String[] {"***One or ***","more
"To" strings here"});

Note#// You can loop above one, if you have control/can maintain routeIDs,
because you need that incase you want to stop or delete a particuar route by
id.

//Dynamic RouteBuilder to add Routers at run-time (method)
public void addRouter(CamelContext camelContext, final String routeId,final
boolean multiCast, 
				final String from, final String[] to){
		try{
			if(camelContext == null){
				camelContext = new DefaultCamelContext();
			}
			RouteBuilder routeBuilder = new RouteBuilder() {
				public void configure() {
					RouteDefinition routeDefinition = from(from).routeId(routeId);
			    	for(String currentTo:to){
			    		log.debug("Added To "+currentTo);
			    		if(multiCast){
							routeDefinition.multicast().to(currentTo);
						}else{
							routeDefinition.to(currentTo);
						}
			    	}
			    }
			};
			routeBuilder.addRoutesToCamelContext(camelContext);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

-------------------
Hope this works for you, or my apologies if I mis-guided you because I am
also a co-learner :-)




--
View this message in context: http://camel.465427.n5.nabble.com/Creating-routes-dynamically-like-a-for-tp5740703p5740908.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Creating routes dynamically like a for()

Posted by "kraythe ." <kr...@gmail.com>.
So why not use the for loop to create as many routes as you want? I use
that method frequently.

*Robert Simmons Jr. MSc. - Lead Java Architect @ EA*
*Author of: Hardcore Java (2003) and Maintainable Java (2012)*
*LinkedIn: **http://www.linkedin.com/pub/robert-simmons/40/852/a39*


On Wed, Oct 2, 2013 at 10:02 AM, mamouros <cs...@yahoo.gr> wrote:

> I can't see how this can help me. I don't want to listen for one exchange
> only and move on. I want to listen forever.
> When I say "create routes dynamically" I mean I would like to open as many
> smpp receivers as accounts are in my database.
> After the from(sql:) I want the routes to be a to(bean:) that have a
> while(true) loop with a consumer.receive() method inside making me able to
> listen to incoming deliver_sm all the time (which is what the consumer will
> receive) but I get the account credentials from the database and I would
> also like to check for any new accounts in the database every now and then
> so I can create another route receiving to that account.
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Creating-routes-dynamically-like-a-for-tp5740703p5740764.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>

Re: Creating routes dynamically like a for()

Posted by mamouros <cs...@yahoo.gr>.
I can't see how this can help me. I don't want to listen for one exchange
only and move on. I want to listen forever. 
When I say "create routes dynamically" I mean I would like to open as many
smpp receivers as accounts are in my database.
After the from(sql:) I want the routes to be a to(bean:) that have a
while(true) loop with a consumer.receive() method inside making me able to
listen to incoming deliver_sm all the time (which is what the consumer will
receive) but I get the account credentials from the database and I would
also like to check for any new accounts in the database every now and then
so I can create another route receiving to that account.



--
View this message in context: http://camel.465427.n5.nabble.com/Creating-routes-dynamically-like-a-for-tp5740703p5740764.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Creating routes dynamically like a for()

Posted by Dale King <da...@jadabeauty.com>.
The confusion is whether you really mean that you want to CREATE new routes dynamically (which would be very unusual) or whether you really mean that you have some processing steps and you want to RUN those steps once for each record returned (which is much more common).

In the later case, a splitter is what you want. For example:

from("sql://yourQuery")
.split().body()
    .log("This sub-route is run once per record from your query")
    .log("The body of the exchange will be one record")
    // Other processing steps ran on each record
.end()
.log("This is ran after all records have been ran through")
.log("The exchange will be the original unless you do something")
.log("different with an aggregation strategy on the splitter")

On Oct 2, 2013, at 4:49 AM, mamouros <cs...@yahoo.gr> wrote:

> To further clarify, I have a database with smpp accounts. I want to query at
> runtime the database every predefined time, get the username and password
> and then open a consumer for each account either with separate from() routes
> passing the credentials or creating separate consumer templates.
> 
> At  runtime:
> query database ->
>     returns 3 accounts
> create 3 routes ->
>    from( account 1 )
>    from( account 2 )
>    from( account 3 )
> go again from start
> 
> 
> 
> --
> View this message in context: http://camel.465427.n5.nabble.com/Creating-routes-dynamically-like-a-for-tp5740703p5740746.html
> Sent from the Camel - Users mailing list archive at Nabble.com.


Re: AW: Creating routes dynamically like a for()

Posted by mamouros <cs...@yahoo.gr>.
To further clarify, I have a database with smpp accounts. I want to query at
runtime the database every predefined time, get the username and password
and then open a consumer for each account either with separate from() routes
passing the credentials or creating separate consumer templates.

At  runtime:
query database ->
     returns 3 accounts
create 3 routes ->
    from( account 1 )
    from( account 2 )
    from( account 3 )
go again from start



--
View this message in context: http://camel.465427.n5.nabble.com/Creating-routes-dynamically-like-a-for-tp5740703p5740746.html
Sent from the Camel - Users mailing list archive at Nabble.com.

AW: Creating routes dynamically like a for()

Posted by "Jan Matèrne (jhm)" <ap...@materne.de>.
Sure, RouteBuilders are just Java objects and can be configured in Java -
Camel DSL is just Java and loops are available ;)

But you have to distinguish when you get your path-list: during startup time
or during runtime?

Startup time:
Use a DB connection / EntityManager for loading the infos from the database
and loop over the datasets and create the routes as usual.

Runtime:
- You have to react on an event (incoming exchange ...) (new)
- load the data (as before, maybe getting from the exchange)
- create the RouteBuilder (as before)
- add to the camel context (new, implicitly done before)

Or am I wrong here?


Jan	

> -----Ursprüngliche Nachricht-----
> Von: kraythe . [mailto:kraythe@gmail.com]
> Gesendet: Mittwoch, 2. Oktober 2013 07:42
> An: Camel Users List
> Betreff: Re: Creating routes dynamically like a for()
> 
> Just put the for outside. I do this all the time.
> 
> for (final String path : paths) {
>   from("file://" + path +"?readlock=changed").to(...) ...
> }
> 
> This will create four identical routes other than the file paths. if
> there are four elements in paths.
> 
> *Robert Simmons Jr. MSc. - Lead Java Architect @ EA* *Author of:
> Hardcore Java (2003) and Maintainable Java (2012)*
> *LinkedIn: **http://www.linkedin.com/pub/robert-simmons/40/852/a39*
> 
> 
> On Tue, Oct 1, 2013 at 4:20 PM, Jan Matèrne (jhm) <ap...@materne.de>
> wrote:
> 
> > What you could do is generating a xml file with the new route
> > definitions and load that.
> > http://camel.apache.org/loading-routes-from-xml-files.html
> >
> > from("sql ...")
> >     .process( generateRoutesXmlProcessor )
> >     .doTypeConversionFromXmlToInputStream
> >     .process( loadRoutesProcessor )
> >
> > Jan
> >
> > > -----Ursprüngliche Nachricht-----
> > > Von: mamouros [mailto:csst0211@yahoo.gr]
> > > Gesendet: Dienstag, 1. Oktober 2013 17:58
> > > An: users@camel.apache.org
> > > Betreff: Creating routes dynamically like a for()
> > >
> > > I would like to create a route for every query my sql route
> returns.
> > > From this:
> > >
> > > from("sql:" + fromUri + "?dataSource=dataSource")
> > > .to("bean:SmsReceiver?method=creatingRoutes");
> > >
> > > I want to make something hypothetically like this:
> > >
> > > for( from("sql:" + fromUri + "?dataSource=dataSource") )  {
> > >
> > >      .to("bean:SmsReceiver?method=creatingRoutes");
> > >
> > > }
> > >
> > > so if I have 3 entries in my table, I would create 3 routes.
> > >
> > > How is that possible. Is there any EIP that does that?
> > >
> > >
> > >
> > > --
> > > View this message in context:
> > > http://camel.465427.n5.nabble.com/Creating-routes-dynamically-like-
> a
> > > -
> > > for-tp5740703.html
> > > Sent from the Camel - Users mailing list archive at Nabble.com.
> >
> >


Re: Creating routes dynamically like a for()

Posted by "kraythe ." <kr...@gmail.com>.
Just put the for outside. I do this all the time.

for (final String path : paths) {
  from("file://" + path +"?readlock=changed").to(...) ...
}

This will create four identical routes other than the file paths. if there
are four elements in paths.

*Robert Simmons Jr. MSc. - Lead Java Architect @ EA*
*Author of: Hardcore Java (2003) and Maintainable Java (2012)*
*LinkedIn: **http://www.linkedin.com/pub/robert-simmons/40/852/a39*


On Tue, Oct 1, 2013 at 4:20 PM, Jan Matèrne (jhm) <ap...@materne.de> wrote:

> What you could do is generating a xml file with the new route definitions
> and load that.
> http://camel.apache.org/loading-routes-from-xml-files.html
>
> from("sql ...")
>     .process( generateRoutesXmlProcessor )
>     .doTypeConversionFromXmlToInputStream
>     .process( loadRoutesProcessor )
>
> Jan
>
> > -----Ursprüngliche Nachricht-----
> > Von: mamouros [mailto:csst0211@yahoo.gr]
> > Gesendet: Dienstag, 1. Oktober 2013 17:58
> > An: users@camel.apache.org
> > Betreff: Creating routes dynamically like a for()
> >
> > I would like to create a route for every query my sql route returns.
> > From this:
> >
> > from("sql:" + fromUri + "?dataSource=dataSource")
> > .to("bean:SmsReceiver?method=creatingRoutes");
> >
> > I want to make something hypothetically like this:
> >
> > for( from("sql:" + fromUri + "?dataSource=dataSource") )  {
> >
> >      .to("bean:SmsReceiver?method=creatingRoutes");
> >
> > }
> >
> > so if I have 3 entries in my table, I would create 3 routes.
> >
> > How is that possible. Is there any EIP that does that?
> >
> >
> >
> > --
> > View this message in context:
> > http://camel.465427.n5.nabble.com/Creating-routes-dynamically-like-a-
> > for-tp5740703.html
> > Sent from the Camel - Users mailing list archive at Nabble.com.
>
>

AW: Creating routes dynamically like a for()

Posted by "Jan Matèrne (jhm)" <ap...@materne.de>.
What you could do is generating a xml file with the new route definitions
and load that.
http://camel.apache.org/loading-routes-from-xml-files.html

from("sql ...")
    .process( generateRoutesXmlProcessor )
    .doTypeConversionFromXmlToInputStream
    .process( loadRoutesProcessor )

Jan

> -----Ursprüngliche Nachricht-----
> Von: mamouros [mailto:csst0211@yahoo.gr]
> Gesendet: Dienstag, 1. Oktober 2013 17:58
> An: users@camel.apache.org
> Betreff: Creating routes dynamically like a for()
> 
> I would like to create a route for every query my sql route returns.
> From this:
> 
> from("sql:" + fromUri + "?dataSource=dataSource")
> .to("bean:SmsReceiver?method=creatingRoutes");
> 
> I want to make something hypothetically like this:
> 
> for( from("sql:" + fromUri + "?dataSource=dataSource") )  {
> 
>      .to("bean:SmsReceiver?method=creatingRoutes");
> 
> }
> 
> so if I have 3 entries in my table, I would create 3 routes.
> 
> How is that possible. Is there any EIP that does that?
> 
> 
> 
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Creating-routes-dynamically-like-a-
> for-tp5740703.html
> Sent from the Camel - Users mailing list archive at Nabble.com.


Re: Creating routes dynamically like a for()

Posted by Dale King <da...@jadabeauty.com>.
Not clear what you are trying to achieve exactly, but I think what you want is the splitter which in this case you would use to execute the route for each record. Something like the following will call the method for each record returned from the SQL.

from("sql:" + fromUri + "?dataSource=dataSource")
.split(body())
.to("bean:SmsReceiver?method=creatingRoutes");

On Oct 1, 2013, at 11:58 AM, mamouros <cs...@yahoo.gr> wrote:

> I would like to create a route for every query my sql route returns.
> From this:
> 
> from("sql:" + fromUri + "?dataSource=dataSource")
> .to("bean:SmsReceiver?method=creatingRoutes");
> 
> I want to make something hypothetically like this:
> 
> for( from("sql:" + fromUri + "?dataSource=dataSource") )  {
> 
>     .to("bean:SmsReceiver?method=creatingRoutes");
> 
> }
> 
> so if I have 3 entries in my table, I would create 3 routes.
> 
> How is that possible. Is there any EIP that does that?
> 
> 
> 
> --
> View this message in context: http://camel.465427.n5.nabble.com/Creating-routes-dynamically-like-a-for-tp5740703.html
> Sent from the Camel - Users mailing list archive at Nabble.com.