You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@camel.apache.org by "Daniel Cook (JIRA)" <ji...@apache.org> on 2013/06/20 10:42:23 UTC

[jira] [Commented] (CAMEL-6463) Camel routes added to context at wrong point of Spring lifecycle

    [ https://issues.apache.org/jira/browse/CAMEL-6463?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13689021#comment-13689021 ] 

Daniel Cook commented on CAMEL-6463:
------------------------------------

I resolved this issue, unfortunately the source code is on a work machine and attaching is an issue.  Summary is as follows:

In AbstractCamelContextFactoryBean move initRouteRefs() down to installRoutes() into a separate method called setupRoutes() and make protected.
In SpringCamelContext add a private boolean member variable routesInstalled set to false initially, provide getter and setter.
In CamelContextFactoyBean in the onApplicationEvent() method add the following code after the call to getContext(false):
if (context != null) {
    if (!context.getRoutesInstalled() && event instanceof ContextRefreshedEvent) {
        context.setRoutesInstalled(true);
        try {
            setupRoutes();
        } catch (Exception e) {
            throw WrapRuntimeCamelException(e);
        }
    }
}

                
> Camel routes added to context at wrong point of Spring lifecycle
> ----------------------------------------------------------------
>
>                 Key: CAMEL-6463
>                 URL: https://issues.apache.org/jira/browse/CAMEL-6463
>             Project: Camel
>          Issue Type: Bug
>          Components: camel-core
>    Affects Versions: 2.9.2
>         Environment: Not relevant.
>            Reporter: Daniel Cook
>            Priority: Minor
>
> We use camel with Java DSL and a lot of spring dependency injection, we context scan to find the Route Builders.
> We have an intermittent problem where camel cannot start due to a null reference being thrown by a route.  Example code (for illustrative purposes) is:
> public class MyRouteBuilder extends RouteBuilder {
> @Autowired
> private MyService myService;
> @Override
> public void configure() throws Exception {
>     from(ENDPOINT)
>     .process(myService)
> }
> }
> In the example above occassionally there is a .process(NULL) reference error thrown at runtime; usually only on quick machines.  I stress this is not a problem with our Spring Dependency injection config.
> After looking at the camel code, including on master, the problem arrises because the AbstractCamelContextFactoryBean (which the Spring CamelContextFactoryBean extends) installs the routes (calling the route builder's configure() method) into the context in the afterPropertiesSet() method.  There is no guarantee that spring will have initialised the service as the Spring Camel Context does not depend on the bean.  The routes should be installed when spring indicates everything has been initialised, a ContextRefreshed event.
> The workaround is not to autowire services used in routes but 'new' them.  

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira