Uploaded image for project: 'Camel'
  1. Camel
  2. CAMEL-15362

Make DefaultFluentProducerTemplate not thread safe for processors/endpoints

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 3.4.2
    • 3.5.0
    • camel-core
    • None
    • Unknown

    Description

      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).

      /**
       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.
       // 
       }
      }
      

      Attachments

        Activity

          People

            davsclaus Claus Ibsen
            perch24 Chris
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: