You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Mike Pilone <MP...@npr.org> on 2012/01/11 17:23:43 UTC

SpringCamelContext afterPropertiesSet never called

I'm debugging an issue where my Spring initialized code attempts to use a Camel route before the route is initialized. I attempted to fix this by adding "depends-on" in my Spring configuration to make my code depend on the camelContext but it didn't help. I could see that the context was being created, then my code created (which starts executing in another thread), then the context was started.

I debugged the SpringCamelContext and found that the afterPropertiesMethod is never called which means the context isn't started until onApplicationEvent is fired with a ContextRefreshedEvent. A breakpoint in afterPropertiesMethod is never triggered so maybeStart() is never called. So the "depends-on" doesn't help because Spring considers the SprintCamelContext initialized and allows bean creation to continue even though the context is not yet started.

I'm using Spring 3.1.0 and Camel 2.9.0. Is this a known issue? Any suggestions for a work-around?

-mike

[cid:image001.jpg@01CCD052.D7C4B3F0] | Mike Pilone | Software Architect, Distribution | mpilone@npr.org<ma...@npr.org> | o: 202-513-2679  m: 703-969-7493


RE: SpringCamelContext afterPropertiesSet never called

Posted by Mike Pilone <MP...@npr.org>.
Hadrian,

I put a sample application that shows the problem up at http://dl.dropbox.com/u/3337852/camel-init-debug.zip

It is a maven project so you can compile it with "mvn compile" and run it with "mvn exec:java". You can also open it up in Eclipse.

Let me know if you need any more information. Note that I'm also using Java 7 but I don't see how that could be involved.

-mike

 | Mike Pilone | Software Architect, Distribution | mpilone@npr.org | o: 202-513-2679  m: 703-969-7493


-----Original Message-----
From: Hadrian Zbarcea [mailto:hzbarcea@gmail.com] 
Sent: Wednesday, January 11, 2012 12:50 PM
To: users@camel.apache.org
Subject: Re: SpringCamelContext afterPropertiesSet never called

Mike, do you have a piece of (xml) code that reproduces this?
Hadrian

On 01/11/2012 11:23 AM, Mike Pilone wrote:
> I'm debugging an issue where my Spring initialized code attempts to use
> a Camel route before the route is initialized. I attempted to fix this
> by adding "depends-on" in my Spring configuration to make my code depend
> on the camelContext but it didn't help. I could see that the context was
> being created, then my code created (which starts executing in another
> thread), then the context was started.
>
> I debugged the SpringCamelContext and found that the
> afterPropertiesMethod is never called which means the context isn't
> started until onApplicationEvent is fired with a ContextRefreshedEvent.
> A breakpoint in afterPropertiesMethod is never triggered so maybeStart()
> is never called. So the "depends-on" doesn't help because Spring
> considers the SprintCamelContext initialized and allows bean creation to
> continue even though the context is not yet started.
>
> I'm using Spring 3.1.0 and Camel 2.9.0. Is this a known issue? Any
> suggestions for a work-around?
>
> -mike
>
> _* | Mike Pilone | Software Architect, Distribution | mpilone@npr.org
> <ma...@npr.org> | o: 202-513-2679 m: 703-969-7493
>

-- 
Hadrian Zbarcea
Principal Software Architect
Talend, Inc
http://coders.talend.com/
http://camelbot.blogspot.com/



Re: SpringCamelContext afterPropertiesSet never called

Posted by Hadrian Zbarcea <hz...@gmail.com>.
Mike, do you have a piece of (xml) code that reproduces this?
Hadrian

On 01/11/2012 11:23 AM, Mike Pilone wrote:
> I'm debugging an issue where my Spring initialized code attempts to use
> a Camel route before the route is initialized. I attempted to fix this
> by adding "depends-on" in my Spring configuration to make my code depend
> on the camelContext but it didn't help. I could see that the context was
> being created, then my code created (which starts executing in another
> thread), then the context was started.
>
> I debugged the SpringCamelContext and found that the
> afterPropertiesMethod is never called which means the context isn't
> started until onApplicationEvent is fired with a ContextRefreshedEvent.
> A breakpoint in afterPropertiesMethod is never triggered so maybeStart()
> is never called. So the "depends-on" doesn't help because Spring
> considers the SprintCamelContext initialized and allows bean creation to
> continue even though the context is not yet started.
>
> I'm using Spring 3.1.0 and Camel 2.9.0. Is this a known issue? Any
> suggestions for a work-around?
>
> -mike
>
> _* | Mike Pilone | Software Architect, Distribution | mpilone@npr.org
> <ma...@npr.org> | o: 202-513-2679 m: 703-969-7493
>

-- 
Hadrian Zbarcea
Principal Software Architect
Talend, Inc
http://coders.talend.com/
http://camelbot.blogspot.com/

RE: SpringCamelContext afterPropertiesSet never called

Posted by Mike Pilone <MP...@npr.org>.
I was able to work around the issue by writing my own bean implementing InitializingBean which takes a reference to the camel context and manually calls afterPropertiesSet. This forces the Camel context to start before Spring's application context initialization continues. In theory this should happen automatically because SpringCamelContext is an InitializingBean but it appears to be a bug someplace.

Here's my code that works around it in a class I called SpringCamelContextInitializer:

/*
   * (non-Javadoc)
   *
   * @see
   * org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
   */
  public void afterPropertiesSet() throws Exception {
    if (camelContext instanceof SpringCamelContext) {
      SpringCamelContext springCamelContext = (SpringCamelContext) camelContext;

      log.info("Camel context is a SpringCamelContext so it will be initialized if needed.");

      if (!springCamelContext.isStarting() && !springCamelContext.isStarted()) {
        log.info("Camel context is not started yet. It will be manually initialized.");
        springCamelContext.afterPropertiesSet();
      }
    }
    else {
      log.info("Camel context is not a SpringCamelContext so it will be ignored.");
    }
  }

Now setting depends-on="springCamelContextInitializer" on my other code causes everything to start up and initialize in the right order. It would be nice to know why SpringCamelContext doesn't initialize properly itself.

-mike


[cid:image001.jpg@01CCD056.2CA57EB0] | Mike Pilone | Software Architect, Distribution | mpilone@npr.org<ma...@npr.org> | o: 202-513-2679  m: 703-969-7493

From: Mike Pilone [mailto:MPilone@npr.org]
Sent: Wednesday, January 11, 2012 11:24 AM
To: users@camel.apache.org
Subject: SpringCamelContext afterPropertiesSet never called

I'm debugging an issue where my Spring initialized code attempts to use a Camel route before the route is initialized. I attempted to fix this by adding "depends-on" in my Spring configuration to make my code depend on the camelContext but it didn't help. I could see that the context was being created, then my code created (which starts executing in another thread), then the context was started.

I debugged the SpringCamelContext and found that the afterPropertiesMethod is never called which means the context isn't started until onApplicationEvent is fired with a ContextRefreshedEvent. A breakpoint in afterPropertiesMethod is never triggered so maybeStart() is never called. So the "depends-on" doesn't help because Spring considers the SprintCamelContext initialized and allows bean creation to continue even though the context is not yet started.

I'm using Spring 3.1.0 and Camel 2.9.0. Is this a known issue? Any suggestions for a work-around?

-mike

[cid:image001.jpg@01CCD052.D7C4B3F0] | Mike Pilone | Software Architect, Distribution | mpilone@npr.org<ma...@npr.org> | o: 202-513-2679  m: 703-969-7493