You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Injinius <re...@yahoo.com> on 2012/04/23 19:49:04 UTC

Re-initializing route builders at runtime under Spring?

All,
I have a Camel/Spring configuration. For example:
BEGIN_CODE_SNIPPET
<?xml version="1.0" encoding="UTF-8"?>
[SNIP]
	<camel:camelContext id="camelContext">
		<camel:routeBuilder ref="RouteBuilder1" />
		<camel:routeBuilder ref="RouteBuilder2" />
	</camel:camelContext>
</beans>
END_CODE_SNIPPET
The RouteBuilders, above, dynamically create the query portion of the "from"
endpoints (e.g. from an SQL query). Eventually, a "change event" occurs
(handled by an existing route) and I'd like to essentially re-initialize the
CamelContext/RouteBuilders (so that they can re-build their query
parameters) without having to take down the application. I'm relatively new,
so I might be missing something obvious, but it looks like my options are
(for 2.7.X camel):

1 - Use camelcontext to get all the routes and edit the "from" URIs in
place. This solution would be similar to:
http://camel.465427.n5.nabble.com/starting-stopping-creating-amp-editing-routes-at-runtime-now-works-CAMEL-1004-td491958.html.
The downsides are a) that getting RouteDefinitions is deprecated in future
version of camel (so it would probably break upon upgrading) and b) I may
not want to do a blanket search-and-replace because the original
RouteBuilder might have some "extra" logic in it.

2 - Get a handle to the camelContext and RouteBuilders from a Spring-aware
bean, stop/remove all existing routes via the camelContext, and then
re-initialize the camelContext with "new", uninitialized instances of the
routeBuilder classes found via Spring's ApplicationContext[1]. 

I suspect I could get either option to work, but they seem non-optimal. I
have a sneaking suspicion that I'm perhaps missing a more elegant solution.
Is there a camel "way" to accomplish this effect?

Notes:
[1] There is the question of excluding the route handling the "change" event
from being stopped/removed/re-initialized



--
View this message in context: http://camel.465427.n5.nabble.com/Re-initializing-route-builders-at-runtime-under-Spring-tp5660040p5660040.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Re-initializing route builders at runtime under Spring?

Posted by Injinius <re...@yahoo.com>.
Claus,
Thanks for the quick reply. I too came across the link you provided during
my previous searches. 

Unfortunately, I don't think that solution helps me out very much in my
case. Presumably, to use the above solution, I'd have to dynamically update
the xml file after receiving a "change" event. In my case, I can't have the
route "logic" centrally located and there's no omniscient object that would
know how to (re-)generate the routes in any case.

I ended up with the following solution, in case anyone cares:
- Created a an I/F "X" that "restartable" Routebuilders would implement that
has "Set<String> getRouteIds()" method. That method returns the route ids of
the routes created by this RouteBuilder.
- Created a "RestartBean" (Application  and Camel context aware) that's part
of the route listening for the "change event", which does the following:
  - Uses the spring application context to find all "restartable"
Routebuilders instances. [They need to scoped as prototype beans in order to
get "new", uninitialized RouteBuilder instances.]
  -  Then for each  "restartable" Routebuilder:
    - Use the getRouteIds() methods to find out which routes those
RouteBuilders created
    - Stop/Remove those routes
    - Pass the "new" Routebuilder instance to camelContext.addRoutes(...)

This has the desirable effect, for me, to keep the route building logic
local to the RouteBuilders and allowing me to re-initialize those
RouteBuilders (and the dynamic routing info they create) at runtime.

Regards

--
View this message in context: http://camel.465427.n5.nabble.com/Re-initializing-route-builders-at-runtime-under-Spring-tp5660040p5662597.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Re-initializing route builders at runtime under Spring?

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

Maybe this little cookbook can be of inspiration
http://camel.apache.org/loading-routes-from-xml-files.html


On Mon, Apr 23, 2012 at 7:49 PM, Injinius <re...@yahoo.com> wrote:
> All,
> I have a Camel/Spring configuration. For example:
> BEGIN_CODE_SNIPPET
> <?xml version="1.0" encoding="UTF-8"?>
> [SNIP]
>        <camel:camelContext id="camelContext">
>                <camel:routeBuilder ref="RouteBuilder1" />
>                <camel:routeBuilder ref="RouteBuilder2" />
>        </camel:camelContext>
> </beans>
> END_CODE_SNIPPET
> The RouteBuilders, above, dynamically create the query portion of the "from"
> endpoints (e.g. from an SQL query). Eventually, a "change event" occurs
> (handled by an existing route) and I'd like to essentially re-initialize the
> CamelContext/RouteBuilders (so that they can re-build their query
> parameters) without having to take down the application. I'm relatively new,
> so I might be missing something obvious, but it looks like my options are
> (for 2.7.X camel):
>
> 1 - Use camelcontext to get all the routes and edit the "from" URIs in
> place. This solution would be similar to:
> http://camel.465427.n5.nabble.com/starting-stopping-creating-amp-editing-routes-at-runtime-now-works-CAMEL-1004-td491958.html.
> The downsides are a) that getting RouteDefinitions is deprecated in future
> version of camel (so it would probably break upon upgrading) and b) I may
> not want to do a blanket search-and-replace because the original
> RouteBuilder might have some "extra" logic in it.
>
> 2 - Get a handle to the camelContext and RouteBuilders from a Spring-aware
> bean, stop/remove all existing routes via the camelContext, and then
> re-initialize the camelContext with "new", uninitialized instances of the
> routeBuilder classes found via Spring's ApplicationContext[1].
>
> I suspect I could get either option to work, but they seem non-optimal. I
> have a sneaking suspicion that I'm perhaps missing a more elegant solution.
> Is there a camel "way" to accomplish this effect?
>
> Notes:
> [1] There is the question of excluding the route handling the "change" event
> from being stopped/removed/re-initialized
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Re-initializing-route-builders-at-runtime-under-Spring-tp5660040p5660040.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
CamelOne 2012 Conference, May 15-16, 2012: http://camelone.com
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/