Search code examples
springapache-camel

How to ensure that Camel's ProducerTemplate waits for CamelContext to start


I am trying to use the @EndpointInject annotation in order to create a ProducerTemplate to bridge my POJO to the CamelContext (as described here http://camel.apache.org/pojo-producing.html).

The problem that I am running into is that the ProducerTemplate is injected into my POJO before all routes in the camel context are started. So when I call producerTemplate.send(...), I get a DirectConsumerNotAvailableException: No consumers available on endpoint... error.

Is there anything that I need to do in order to ensure the CamelContext is started before trying to send to a route?


Solution

  • You can use block=true as an option on the direct endpoint and it will wait until the consumer is active and running. This should help.

    Otherwise you would need to write some code yourself to wait until the CamelContext is in started state. You can get access to it from the injected ProducerTemplate which has a getCamelContext.

    Also another alternative is to have the dependency injection framework, if possible, setup your bean after Camel. If you are using spring xml, then it has the depends-on attribute you can set on the <bean> tags.