Search code examples
apache-camelspring-camel

Route level scope for bean in camel blueprint


I'm developing one camel application in which I want to keep scope of beans at route level. Meaning that, if I call one bean from two different routes two instances should get created.But within that route same single instance should be used for that bean. Following is my code:

<bean id="testbean" class="testClass">  </bean>
<camelContext id="test"
        xmlns="http://camel.apache.org/schema/blueprint">
    <route id="1">
        <from uri="timer"/>
        <to uri="bean:test"/>
    </route>
    <route id="2">
       <from uri="timer"/>
       <to uri="bean:test"/>  
    </route>
</camelContext>

Here in route 2 separate bean instance should get created. Please suggest if have any idea.


Solution

  • There is no support for route scoped beans in Apache Camel or the likes. You can either have shared singleton beans or prototype beans (new instance per call). Those are the scopes that comes from Spring XML or Blueprint XML.

    To use prototype scope you need to both:

    • declare the bean as prototype in Spring/Blueprint XML
    • set cache=false option in the bean:xxx endpoint in Camel

    You could also consider having two beans, eg

    <bean id="testbean" class="testClass">  </bean>
    <bean id="testbean2" class="testClass">  </bean>
    

    And then use testbean in the first route, and testbean2 in the second route.