0

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.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
nik
  • 1,464
  • 4
  • 18
  • 32
  • Why do you want to do that, what is the reason for such a thing? – Claus Ibsen Apr 17 '18 at 14:47
  • @ClausIbsen - Because if route 1 is doing some processing on one test class instance,I don't want route 2 should use the same instance of that class.It should have its own instance of the bean.Is there any way so that I can trigger route 2 only after route 1 finishes its task.Thanks – nik Apr 17 '18 at 15:38
  • @nik, you are not really answering the question. What is the requirement that makes you think that you need the two instances per route as a solution that implements that requirement? – Ralf Apr 17 '18 at 16:13
  • @Ralf -Thanks for your query.I need to have one instance per route not two instances per route. – nik Apr 18 '18 at 03:24
  • @Ralf - I just read about bean scopes, can defining bean with the prototype provide me such functionality? – nik Apr 18 '18 at 04:33

1 Answers1

4

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.

Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65
  • Thanks a lot.This was the info I needed. – nik Apr 18 '18 at 06:05
  • There is no indication in the online doc of the [bean component](http://camel.apache.org/bean.html) that `cache=false` is necessary for prototype scoped beans. Maybe it should be added? – Ralf Apr 18 '18 at 06:52
  • The updated docs is at: https://github.com/apache/camel/blob/master/camel-core/src/main/docs/bean-component.adoc - And cache is default false so you dont need to set it – Claus Ibsen Apr 18 '18 at 07:44