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/

Reply via email to