You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Nick Chistyakov <ch...@gmail.com> on 2009/12/10 15:44:11 UTC
question: best practice to use camel's Producer class
Hi guys,
What is actually the best practice to use camel's Producer class?
Is it expensive to create, for example, to create it every time when message
is processed.
Or is it better to store a reference to it somewhere in a class scope?
Best regards,
Nick
Re: question: best practice to use camel's Producer class
Posted by James Strachan <ja...@gmail.com>.
2009/12/10 Nick Chistyakov <ch...@gmail.com>:
> Hi guys,
>
> What is actually the best practice to use camel's Producer class?
> Is it expensive to create, for example, to create it every time when message
> is processed.
> Or is it better to store a reference to it somewhere in a class scope?
FWIW typically you don't need to implement a Processor as you can use
the bean binding instead and just invoke regular java bean methods,
http://camel.apache.org/bean-binding.html
allowing you to use the annotations to extract the various things from
the message you want which simplifies your code and also means you can
hide the Camel APIs. We should strive to write application code
without any explicit middleware APIs where possible as its easier to
understand and test
http://camel.apache.org/hiding-middleware.html
In terms of whether you should use a singleton instance of a
bean/processor or not, if you are using spring/guice then you can
specify this in the IoC container. Camel will use Spring/Guice to get
an instance of your bean - which allows each instance to be stateful
if you like (e.g. injecting a new database connection or whatever) to
avoid concurrency issues when sharing the same bean instance - or if
you prefer it can be a singleton.
--
James
-------
http://macstrac.blogspot.com/
Open Source Integration
http://fusesource.com/
Re: question: best practice to use camel's Producer class
Posted by James Strachan <ja...@gmail.com>.
2009/12/10 Nick Chistyakov <ch...@gmail.com>:
> Hello James,
>
> Thanks for a fast response!
> My question is actually about Producer - not about Processor.
> Your answer is not less valuable though :))
DOH! :) That'll teach me to answer emails when I'm jetlagged, its
early in the morning and I've not had coffee! :)
--
James
-------
http://macstrac.blogspot.com/
Open Source Integration
http://fusesource.com/
Re: question: best practice to use camel's Producer class
Posted by Nick Chistyakov <ch...@gmail.com>.
Great! Thanks a lot, Claus!!!!!!!!
On Thu, Dec 10, 2009 at 6:33 PM, Claus Ibsen <cl...@gmail.com> wrote:
> On Thu, Dec 10, 2009 at 4:11 PM, Nick Chistyakov <ch...@gmail.com>
> wrote:
> > Hello James,
> >
> > Thanks for a fast response!
> > My question is actually about Producer - not about Processor.
> > Your answer is not less valuable though :))
> >
> > Hello Claus,
> > Do you mean that for a case like this:
> >
> > public final class OrderProcessor implements Processor {
> >
> > public void process(Exchange exchange) {
> > try {
> >
> > Order order = exchange.getIn().getBody(Order.class);
> >
> > order.doSomethingWithOrder();
> >
> > Producer producer = exchange.getContext()
> > .getEndpoint("jms:queue:orders")
> > .createProducer();
> >
> > producer.start();
> >
> > Exchange statusExchange = producer.createExchange();
> > statusExchange.getIn().setBody(order);
> > producer.process(statusExchange);
> >
> > producer.stop();
> >
> > } catch (Exception e) {
> > e.printStackTrace();
> > }
> > }
> >
> >
> > }
> >
> > it is better to have producer as a member of class OrderProcessor?
> > or because of that local cache I can forget about it?
> >
>
> No its best as a member especially for producers which are "expensive
> to create such as JMS stuff" as opposed to a logger producer, a file
> producer etc.
>
> The cache is when you use ProducerTemplate and not hand crafting a
> Producer using the API as you do.
>
> ProducerTemplate template = context.newProducerTemplate();
> Exchange reply = template.send("jms:queue:foo", exchange);
>
>
> > Best regards,
> > Nick
> >
> > On Thu, Dec 10, 2009 at 5:58 PM, Claus Ibsen <cl...@gmail.com>
> wrote:
> >
> >> On Thu, Dec 10, 2009 at 3:44 PM, Nick Chistyakov <ch...@gmail.com>
> >> wrote:
> >> > Hi guys,
> >> >
> >> > What is actually the best practice to use camel's Producer class?
> >> > Is it expensive to create, for example, to create it every time when
> >> message
> >> > is processed.
> >> > Or is it better to store a reference to it somewhere in a class scope?
> >> >
> >>
> >> Also the ProducerTemplate has a local cache for Producers so its not
> >> expensive to use.
> >>
> >> Just need to be aware of this FAQ
> >>
> >>
> http://camel.apache.org/why-does-camel-use-too-many-threads-with-producertemplate.html
> >>
> >>
> >> > Best regards,
> >> > Nick
> >> >
> >>
> >>
> >>
> >> --
> >> Claus Ibsen
> >> Apache Camel Committer
> >>
> >> Author of Camel in Action: http://www.manning.com/ibsen/
> >> Open Source Integration: http://fusesource.com
> >> Blog: http://davsclaus.blogspot.com/
> >> Twitter: http://twitter.com/davsclaus
> >>
> >
>
>
>
> --
> Claus Ibsen
> Apache Camel Committer
>
> Author of Camel in Action: http://www.manning.com/ibsen/
> Open Source Integration: http://fusesource.com
> Blog: http://davsclaus.blogspot.com/
> Twitter: http://twitter.com/davsclaus
>
Re: question: best practice to use camel's Producer class
Posted by Claus Ibsen <cl...@gmail.com>.
On Thu, Dec 10, 2009 at 4:11 PM, Nick Chistyakov <ch...@gmail.com> wrote:
> Hello James,
>
> Thanks for a fast response!
> My question is actually about Producer - not about Processor.
> Your answer is not less valuable though :))
>
> Hello Claus,
> Do you mean that for a case like this:
>
> public final class OrderProcessor implements Processor {
>
> public void process(Exchange exchange) {
> try {
>
> Order order = exchange.getIn().getBody(Order.class);
>
> order.doSomethingWithOrder();
>
> Producer producer = exchange.getContext()
> .getEndpoint("jms:queue:orders")
> .createProducer();
>
> producer.start();
>
> Exchange statusExchange = producer.createExchange();
> statusExchange.getIn().setBody(order);
> producer.process(statusExchange);
>
> producer.stop();
>
> } catch (Exception e) {
> e.printStackTrace();
> }
> }
>
>
> }
>
> it is better to have producer as a member of class OrderProcessor?
> or because of that local cache I can forget about it?
>
No its best as a member especially for producers which are "expensive
to create such as JMS stuff" as opposed to a logger producer, a file
producer etc.
The cache is when you use ProducerTemplate and not hand crafting a
Producer using the API as you do.
ProducerTemplate template = context.newProducerTemplate();
Exchange reply = template.send("jms:queue:foo", exchange);
> Best regards,
> Nick
>
> On Thu, Dec 10, 2009 at 5:58 PM, Claus Ibsen <cl...@gmail.com> wrote:
>
>> On Thu, Dec 10, 2009 at 3:44 PM, Nick Chistyakov <ch...@gmail.com>
>> wrote:
>> > Hi guys,
>> >
>> > What is actually the best practice to use camel's Producer class?
>> > Is it expensive to create, for example, to create it every time when
>> message
>> > is processed.
>> > Or is it better to store a reference to it somewhere in a class scope?
>> >
>>
>> Also the ProducerTemplate has a local cache for Producers so its not
>> expensive to use.
>>
>> Just need to be aware of this FAQ
>>
>> http://camel.apache.org/why-does-camel-use-too-many-threads-with-producertemplate.html
>>
>>
>> > Best regards,
>> > Nick
>> >
>>
>>
>>
>> --
>> Claus Ibsen
>> Apache Camel Committer
>>
>> Author of Camel in Action: http://www.manning.com/ibsen/
>> Open Source Integration: http://fusesource.com
>> Blog: http://davsclaus.blogspot.com/
>> Twitter: http://twitter.com/davsclaus
>>
>
--
Claus Ibsen
Apache Camel Committer
Author of Camel in Action: http://www.manning.com/ibsen/
Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus
Re: question: best practice to use camel's Producer class
Posted by Nick Chistyakov <ch...@gmail.com>.
Hello James,
Thanks for a fast response!
My question is actually about Producer - not about Processor.
Your answer is not less valuable though :))
Hello Claus,
Do you mean that for a case like this:
public final class OrderProcessor implements Processor {
public void process(Exchange exchange) {
try {
Order order = exchange.getIn().getBody(Order.class);
order.doSomethingWithOrder();
Producer producer = exchange.getContext()
.getEndpoint("jms:queue:orders")
.createProducer();
producer.start();
Exchange statusExchange = producer.createExchange();
statusExchange.getIn().setBody(order);
producer.process(statusExchange);
producer.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
}
it is better to have producer as a member of class OrderProcessor?
or because of that local cache I can forget about it?
Best regards,
Nick
On Thu, Dec 10, 2009 at 5:58 PM, Claus Ibsen <cl...@gmail.com> wrote:
> On Thu, Dec 10, 2009 at 3:44 PM, Nick Chistyakov <ch...@gmail.com>
> wrote:
> > Hi guys,
> >
> > What is actually the best practice to use camel's Producer class?
> > Is it expensive to create, for example, to create it every time when
> message
> > is processed.
> > Or is it better to store a reference to it somewhere in a class scope?
> >
>
> Also the ProducerTemplate has a local cache for Producers so its not
> expensive to use.
>
> Just need to be aware of this FAQ
>
> http://camel.apache.org/why-does-camel-use-too-many-threads-with-producertemplate.html
>
>
> > Best regards,
> > Nick
> >
>
>
>
> --
> Claus Ibsen
> Apache Camel Committer
>
> Author of Camel in Action: http://www.manning.com/ibsen/
> Open Source Integration: http://fusesource.com
> Blog: http://davsclaus.blogspot.com/
> Twitter: http://twitter.com/davsclaus
>
Re: question: best practice to use camel's Producer class
Posted by Claus Ibsen <cl...@gmail.com>.
On Thu, Dec 10, 2009 at 3:44 PM, Nick Chistyakov <ch...@gmail.com> wrote:
> Hi guys,
>
> What is actually the best practice to use camel's Producer class?
> Is it expensive to create, for example, to create it every time when message
> is processed.
> Or is it better to store a reference to it somewhere in a class scope?
>
Also the ProducerTemplate has a local cache for Producers so its not
expensive to use.
Just need to be aware of this FAQ
http://camel.apache.org/why-does-camel-use-too-many-threads-with-producertemplate.html
> Best regards,
> Nick
>
--
Claus Ibsen
Apache Camel Committer
Author of Camel in Action: http://www.manning.com/ibsen/
Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus