FAQ
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.

Search Discussions

  • Dale King at Oct 1, 2013 at 4:06 pm
    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 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.
  • Jan Matèrne (jhm) at Oct 1, 2013 at 9:21 pm
    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
    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.
  • Kraythe . at Oct 2, 2013 at 5:42 am
    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) 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
    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.
  • Jan Matèrne (jhm) at Oct 2, 2013 at 6:30 am
    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 .
    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) 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
    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.
  • Mamouros at Oct 2, 2013 at 8:50 am
    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.
  • Dale King at Oct 2, 2013 at 2:23 pm
    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 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.
  • Mamouros at Oct 2, 2013 at 3:02 pm
    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.
  • Kraythe . at Oct 4, 2013 at 4:38 am
    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 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.
  • Gudiseashok at Oct 5, 2013 at 3:55 am
    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.
  • Mamouros at Oct 9, 2013 at 7:22 am
    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.
  • Gudiseashok at Oct 9, 2013 at 12:22 pm
    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]" 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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupusers @
categoriescamel
postedOct 1, '13 at 3:58p
activeOct 9, '13 at 12:22p
posts12
users5
websitecamel.apache.org

People

Translate

site design / logo © 2021 Grokbase