Discussion:
how could I get multiple different ProducerTemplate instances with Auto-Configured Producer Templates
Wang Yan
2018-11-28 20:50:01 UTC
Permalink
If I use the auto configured ProducerTemplate, how could I get multiple
different ProducerTemplate instances?

for example I need use producer template send different exchanges to
different endpoints
in this case, Do i need different producer template instances? if yes how ?
Any suggestions ?
Auto-Configured Consumer and Producer Templates

Camel auto-configuration provides pre-configured *ConsumerTemplate* and
*ProducerTemplate* instances. You can simply inject them into your
Spring-managed beans:

@Component
public class InvoiceProcessor {

@Autowired
private ProducerTemplate producerTemplate;

@Autowired
private ConsumerTemplate consumerTemplate;
public void processNextInvoice() {
Invoice invoice = consumerTemplate.receiveBody("jms:invoices",
Invoice.class);
...
producerTemplate.sendBody("netty-http:http://invoicing.com/received/"
+ invoice.id());
}
}
Claus Ibsen
2018-12-03 18:18:53 UTC
Permalink
Hi

No you can use the same template to send to multiple endpoints.

Just beware that sending to unlimited number of endpoints uris is not
a good practice as an endpoint / producer takes up resource. So in
your case with the HTTP endpoint, then its better to use a static uri
for the hostname and then a header with the dynamic context-path, then
you reuse the same endpoint / producer when doing network connection
to the remote http server.
Post by Wang Yan
If I use the auto configured ProducerTemplate, how could I get multiple
different ProducerTemplate instances?
for example I need use producer template send different exchanges to
different endpoints
in this case, Do i need different producer template instances? if yes how ?
Auto-Configured Consumer and Producer Templates
Camel auto-configuration provides pre-configured *ConsumerTemplate* and
*ProducerTemplate* instances. You can simply inject them into your
@Component
public class InvoiceProcessor {
@Autowired
private ProducerTemplate producerTemplate;
@Autowired
private ConsumerTemplate consumerTemplate;
public void processNextInvoice() {
Invoice invoice = consumerTemplate.receiveBody("jms:invoices",
Invoice.class);
...
producerTemplate.sendBody("netty-http:http://invoicing.com/received/"
+ invoice.id());
}
}
--
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2
Claus Ibsen
2018-12-03 21:47:42 UTC
Permalink
Btw mind that in very latest Camel releases we have optimised the http
endpoints to try to auto-detect the dynamic aspects of the
context-path and optimise themselves.
So in that case you may not need to use the header. But I cannot
recall without looking in the source, whether we allowed this
optimisation to happen on producer template level too (in routes it
would do it)
Post by Claus Ibsen
Hi
No you can use the same template to send to multiple endpoints.
Just beware that sending to unlimited number of endpoints uris is not
a good practice as an endpoint / producer takes up resource. So in
your case with the HTTP endpoint, then its better to use a static uri
for the hostname and then a header with the dynamic context-path, then
you reuse the same endpoint / producer when doing network connection
to the remote http server.
Post by Wang Yan
If I use the auto configured ProducerTemplate, how could I get multiple
different ProducerTemplate instances?
for example I need use producer template send different exchanges to
different endpoints
in this case, Do i need different producer template instances? if yes how ?
Auto-Configured Consumer and Producer Templates
Camel auto-configuration provides pre-configured *ConsumerTemplate* and
*ProducerTemplate* instances. You can simply inject them into your
@Component
public class InvoiceProcessor {
@Autowired
private ProducerTemplate producerTemplate;
@Autowired
private ConsumerTemplate consumerTemplate;
public void processNextInvoice() {
Invoice invoice = consumerTemplate.receiveBody("jms:invoices",
Invoice.class);
...
producerTemplate.sendBody("netty-http:http://invoicing.com/received/"
+ invoice.id());
}
}
--
Claus Ibsen
-----------------
Camel in Action 2: https://www.manning.com/ibsen2
--
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2
Wang Yan
2018-12-07 18:07:26 UTC
Permalink
What is different to createProducerTemplate via AutoConfigure and
EndPointInject

in SpringBoot ? are the both way correct?

via EndpointInject I could create multiple instance of
ProducerTemplate, but via Autoconfig I could only create one
ProducerTemplate instace



############################ via
EndpointInject#######################@Component
public class InvoiceProcessor {

@EndpointInject(uri="netty-http:http://invoicing.com/received")
private ProducerTemplate producer;
public void processNextInvoice() {
Invoice invoice = consumerTemplate.receiveBody("jms:invoices",
Invoice.class);
producer.sendBody(invoice.id());
}
}




############## via autoconfig ############################

@Component
public class InvoiceProcessor {

@Autowired
private ProducerTemplate producerTemplate;

@Autowired
private ConsumerTemplate consumerTemplate;
public void processNextInvoice() {
Invoice invoice = consumerTemplate.receiveBody("jms:invoices",
Invoice.class);
...
producerTemplate.sendBody("netty-http:http://invoicing.com/received/"
+ invoice.id());
}
}

Loading...