Chris created CAMEL-15362:
-----------------------------

             Summary: Make DefaultFluentProducerTemplate not thread safe for 
processors/endpoints
                 Key: CAMEL-15362
                 URL: https://issues.apache.org/jira/browse/CAMEL-15362
             Project: Camel
          Issue Type: Improvement
          Components: camel-base
    Affects Versions: 3.4.2
            Reporter: Chris


I am using Camel 3.4.2 in a SpringBoot application using the Camel SpringBoot 
starter. In the CamelAutoConfiguration class it create a FluentProducerTemplate 
that I am injecting into my service classes. In order to use this with 
different endpoints, producer side processors, etc, it looks like I need to 
call the clearAll() method on the FluentProducerTemplate before each usage to 
make its re-use threadsafe. I don't see this documented anywhere, and also this 
seems a little wonky. Additionally, this only clears the body and headers that 
are set as ThreadLocals. It looks like to me, if I set an exchange, processor, 
or even an endpoint these threadlocals will be re-used the next time I used 
this same thread even if I call clearAll() before using it. Is that the way 
this is supposed to work?

Unless I'm missing something here, it seems like it would be better to have the 
FluentProducerTemplate return a new object instance with each new request. 
Something like below - missing lots of details).
{code:java}
/**
 The top-level class would manage the service lifecyle, context, ...
 Nested FluentProducerTemplateContext class would be the "state" for each 
request
 started by a call to one of the "to" methods or others that make sense.
 Additionally, it would implement the send/request methods using the producer
 template.
**/
public class DefaultFluentProducerTemplate extends ServiceSupport {
 private final CamelContext context;
 private Endpoint defaultEndpoint;
 private volatile ProducerTemplate template;
 
 public FluentProducerTemplate to(Endpoint endpoint) {
   return new FluentProducerTemplateContext(endpoint);
 }

 public FluentProducerTemplate toDefaultEndpoint() {
   return new FluentProducerTemplateContext(defaultEndpoint);
 }

 public static class FluentProducerTemplateContext implements 
FluentProducerTemplate {
 private final Endpoint endpoint;
 private final Map<String, Object> headers = new HashMap<>();
 
 private Object body;
 private Supplier<Exchange> exchangeSupplier;
 private Supplier<Processor> processorSupplier;

 public FluentProducerTemplateNested withBody(Object body) {
   this.body = body;
   return this;
 }

 //
 // Remainder of with methods, send, request, etc.
 // 
 }
}
{code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to