You can implement the IsSingleton interface on your bean

On Mon, Jul 6, 2015 at 3:50 PM, Benjamin Legendre <[email protected]> wrote:
>> If you refer to the bean just by the classname, then Camel creates an
>> instance and does the IoC (eg also @Produce) etc.
>
> Is there a way to set a bean in the registry and telling Camel doing the IoC ?
> Something like camelContext.addBean("name", class) ...
>
>> There is a boolean option afair to cache it, you can maybe do
>>
>>.bean(class, method, true)
>
> It should be definitively the answer. Morevover it avoids doing registry 
> bindings manually.
>
> Unfortunately I tried this before posting as it sounded but i can't figured 
> how to make it to work.
> More precisely i tried .bean(class, method, false, true) because the third 
> parameter is "multiParameterArray" but
> everytime i have a .bean(class, method, false, true) statement in my route it 
> recreates an instance.
>
> In the Javadoc, for the ProcessorDefinition::bean method, it is mentionned
> "[...] Cache can be enabled if the bean in the Registry is defined as a 
> singleton scope."
> I don't really understand. Is there something to do related to this ?
>
> So, as it is not a big issue i'll do the "manual way".
>
> Thanks
>
> ----- Mail original -----
> De: "Claus Ibsen" <[email protected]>
> À: [email protected]
> Envoyé: Lundi 6 Juillet 2015 14:59:16
> Objet: Re: How to instanciate a bean in camel registry for use with @produce 
> annotation
>
> On Mon, Jul 6, 2015 at 2:23 PM, Benjamin Legendre <[email protected]> wrote:
>> Thanks Hans and Claus for answer.
>>
>> Hans,
>>
>> I don't use Spring so i don't know what @Autowire is about.
>>
>> What i try to achieve is simply use a ProducerTemplate instance inside the 
>> "normalize" method to
>> retrieve data from a jdbc endpoint using 
>> producerTemplate.requestBodyAndHeader("jdbc:")
>>
>> I can get it to work by setting it manually:
>>
>>     SimpleRegistry registry = new SimpleRegistry();
>>     CamelContext context = new DefaultCamelContext(registry);
>>
>>     DigitalTimeSeriesNormalizer dtsn = new DigitalTimeSeriesNormalizer();
>>     dgtsn.producerTemplate = context.createProducerTemplate();
>>     registry.put("dtsn", dtsn);
>>
>
> Here you create the bean yourself with the new constructor. Camel just
> use this instance as-is.
>
>> But i though it was cleaner using @Produce (and more handy in my case). 
>> Maybe i miss the point here ?
>>
>>
>
> If you refer to the bean just by the classname, then Camel creates an
> instance and does the IoC (eg also @Produce) etc.
>
> There is a boolean option afair to cache it, you can maybe do
>
> .bean(class, method, true)
>
>
>
>> Claus,
>>
>> Using .beanRef("beanName") is already what i do.
>> But i noticed that using .bean(MyBean.class) makes the @Produce
>> to works but has the drawback of creating many instances.
>>
>> I'm sorry if wasn't clear.
>>
>> Thanks
>>
>>
>> ----- Mail original -----
>> De: "Claus Ibsen" <[email protected]>
>> À: [email protected]
>> Envoyé: Lundi 6 Juillet 2015 13:26:43
>> Objet: Re: How to instanciate a bean in camel registry for use with @produce 
>> annotation
>>
>> Refer to it by its name, and use beanRef
>>
>> .beanRef("dtsm", "normalize")
>>
>> On Mon, Jul 6, 2015 at 12:36 PM, Benjamin Legendre <[email protected]> wrote:
>>> Hi,
>>> I'm using Camel 2.15.2 inside a stand alone java application.
>>>
>>> I do not figure how to add a bean in the Camel context in order to inject a 
>>> ProducerTemplate using @Produce on a property of my bean.
>>> I my scenario The producer is not injected.
>>>
>>>
>>> Here is the bean binding:
>>>
>>>         SimpleRegistry registry = new SimpleRegistry();
>>>         registry.put("dtsm", new DigitalTimeSeriesNormalizer());
>>>         CamelContext context = new DefaultCamelContext(registry);
>>>
>>>
>>> The bean is then called like this is the route:
>>>
>>>         .beanRef("dtsm", "normalize")
>>>
>>>
>>> Here is the code inside the bean:
>>>
>>> public class DigitalTimeSeriesNormalizer {
>>>
>>>     @Produce
>>>     public ProducerTemplate producerTemplate;
>>>
>>>     [...]
>>>
>>>     public ArrayList<TimeSerie> normalize(ArrayList<TimeSerie> timeSeries) {
>>>
>>>          this.producerTemplate // is null here
>>>
>>>     [...]
>>> }
>>>
>>> It works when i use this syntax:
>>>
>>> .bean(DigitalTimeSeriesNormalizer.class, "normalize")
>>>
>>> But it is not what i want because a new instance of the bean is created 
>>> each time it is called on the route.
>>> If this is the only way to make it to work, is there a way to make camel 
>>> instanciate it only one time and use a reference of the bean the next time ?
>>>
>>> Thanks in advance.
>>> Benjamin
>>
>>
>>
>> --
>> Claus Ibsen
>> -----------------
>> Red Hat, Inc.
>> Email: [email protected]
>> Twitter: davsclaus
>> Blog: http://davsclaus.com
>> Author of Camel in Action: http://www.manning.com/ibsen
>> hawtio: http://hawt.io/
>> fabric8: http://fabric8.io/
>
>
>
> --
> Claus Ibsen
> -----------------
> Red Hat, Inc.
> Email: [email protected]
> Twitter: davsclaus
> Blog: http://davsclaus.com
> Author of Camel in Action: http://www.manning.com/ibsen
> hawtio: http://hawt.io/
> fabric8: http://fabric8.io/



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: [email protected]
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
hawtio: http://hawt.io/
fabric8: http://fabric8.io/

Reply via email to