Interface FluentProducerTemplate

All Superinterfaces:
AutoCloseable, Service

public interface FluentProducerTemplate extends Service
Template for working with Camel and sending Message instances in an Exchange to an Endpoint using a fluent build style.

Important: Read the javadoc of each method carefully to ensure the behavior of the method is understood. Some methods is for InOnly, others for InOut MEP. And some methods throws CamelExecutionException while others stores any thrown exception on the returned Exchange.

The FluentProducerTemplate is thread safe with the assumption that its the same (single) thread that builds the message (via the fluent methods) that also sends the message.

When using the fluent template its required to chain the methods such as:

     FluentProducerTemplate fluent = ...
     fluent.withHeader("foo", 123).withHeader("bar", 456).withBody("Hello World").to("kafka:cheese").send();
 
The following code is wrong (do not do this)
     FluentProducerTemplate fluent = ...
     fluent.withHeader("foo", 123);
     fluent.withHeader("bar", 456);
     fluent.withBody("Hello World");
     fluent.to("kafka:cheese");
     fluent.send();
 
If you do not want to chain fluent methods you can do as follows:
     FluentProducerTemplate fluent = ...
     fluent = fluent.withHeader("foo", 123);
     fluent = fluent.withHeader("bar", 456);
     fluent = fluent.withBody("Hello World");
     fluent = fluent.to("kafka:cheese")
     fluent.send();
 

You can either only use either withExchange, or withProcessor or a combination of withBody/withHeaders to construct the message to be sent.

All the methods which sends a message may throw FailedToCreateProducerException in case the Producer could not be created. Or a NoSuchEndpointException if the endpoint could not be resolved. There may be other related exceptions being thrown which occurs before the Producer has started sending the message.

All the send or request methods will return the content according to this strategy:

  • throws CamelExecutionException if processing failed during routing with the caused exception wrapped
  • The fault.body if there is a fault message set and its not null
  • Either IN or OUT body according to the message exchange pattern. If the pattern is Out capable then the OUT body is returned, otherwise IN.

Before using the template it must be started. And when you are done using the template, make sure to Service.stop() the template.

Important note on usage: See this FAQ entry before using.

See Also:
  • Method Details

    • getCamelContext

      CamelContext getCamelContext()
      Get the CamelContext
      Returns:
      camelContext the Camel context
    • getMaximumCacheSize

      int getMaximumCacheSize()
      Gets the maximum cache size used in the backing cache pools.
      Returns:
      the maximum cache size
    • setMaximumCacheSize

      void setMaximumCacheSize(int maximumCacheSize)
      Sets a custom maximum cache size to use in the backing cache pools.
      Parameters:
      maximumCacheSize - the custom maximum cache size
    • getCurrentCacheSize

      int getCurrentCacheSize()
      Gets an approximated size of the current cached resources in the backing cache pools.
      Returns:
      the size of current cached resources
    • getDefaultEndpoint

      Endpoint getDefaultEndpoint()
      Get the default endpoint to use if none is specified
      Returns:
      the default endpoint instance
    • setDefaultEndpoint

      void setDefaultEndpoint(Endpoint defaultEndpoint)
      Sets the default endpoint to use if none is specified
      Parameters:
      defaultEndpoint - the default endpoint instance
    • setDefaultEndpointUri

      void setDefaultEndpointUri(String endpointUri)
      Sets the default endpoint uri to use if none is specified
      Parameters:
      endpointUri - the default endpoint uri
    • setEventNotifierEnabled

      void setEventNotifierEnabled(boolean enabled)
      Sets whether the EventNotifier should be used by this ProducerTemplate to send events about the Exchange being sent.

      By default this is enabled.

      Parameters:
      enabled - true to enable, false to disable.
    • isEventNotifierEnabled

      boolean isEventNotifierEnabled()
      Whether the EventNotifier should be used by this ProducerTemplate to send events about the Exchange being sent.
      Returns:
      true if enabled, false otherwise
    • cleanUp

      void cleanUp()
      Cleanup the cache (purging stale entries)
    • withHeaders

      FluentProducerTemplate withHeaders(Map<String,Object> headers)
      Set the headers Important: You can either only use either withExchange, or withProcessor or a combination of withBody/withHeaders to construct the message to be sent.
      Parameters:
      headers - the headers
    • withHeader

      FluentProducerTemplate withHeader(String key, Object value)
      Set the header Important: You can either only use either withExchange, or withProcessor or a combination of withBody/withHeaders to construct the message to be sent.
      Parameters:
      key - the key of the header
      value - the value of the header
    • withBody

      Set the message body Important: You can either only use either withExchange, or withProcessor or a combination of withBody/withHeaders to construct the message to be sent.
      Parameters:
      body - the body
    • withBodyAs

      FluentProducerTemplate withBodyAs(Object body, Class<?> type)
      Set the message body after converting it to the given type Important: You can either only use either withExchange, or withProcessor or a combination of withBody/withHeaders to construct the message to be sent.
      Parameters:
      body - the body
      type - the type which the body should be converted to
    • withTemplateCustomizer

      FluentProducerTemplate withTemplateCustomizer(Consumer<ProducerTemplate> templateCustomizer)
      To customize the producer template for advanced usage like to set the executor service to use.
       
       FluentProducerTemplate fluent = context.createFluentProducerTemplate();
       fluent.withTemplateCustomizer(
               t -> {
                   t.setExecutorService(myExecutor);
                   t.setMaximumCacheSize(10);
               }
            )
           .withBody("the body")
           .to("direct:start")
           .send()
       
      Note that it is invoked only once.
      Parameters:
      templateCustomizer - the customizer
    • withExchange

      FluentProducerTemplate withExchange(Exchange exchange)
      Set the exchange to use for send. When using withExchange then you must use the send method (request is not supported). Important: You can either only use either withExchange, or withProcessor or a combination of withBody/withHeaders to construct the message to be sent.
      Parameters:
      exchange - the exchange
    • withExchange

      FluentProducerTemplate withExchange(Supplier<Exchange> exchangeSupplier)
      Set the exchangeSupplier which will be invoke to get the exchange to be used for send. When using withExchange then you must use the send method (request is not supported). Important: You can either only use either withExchange, or withProcessor or a combination of withBody/withHeaders to construct the message to be sent.
      Parameters:
      exchangeSupplier - the supplier
    • withProcessor

      FluentProducerTemplate withProcessor(Processor processor)
      Set the processor to use for send/request.
       
       FluentProducerTemplate.on(context)
               .withProcessor(
                       exchange -> {
                           exchange.getIn().setHeader("Key1", "Val1");
                           exchange.getIn().setHeader("Key2", "Val2");
                           exchange.getIn().setBody("the body");
                       })
               .to("direct:start")
               .request()
       
       
      Important: You can either only use either withExchange, or withProcessor or a combination of withBody/withHeaders to construct the message to be sent.
      Parameters:
      processor - the processor
    • withProcessor

      FluentProducerTemplate withProcessor(Supplier<Processor> processorSupplier)
      Set the processorSupplier which will be invoke to get the processor to be used for send/request. Important: You can either only use either withExchange, or withProcessor or a combination of withBody/withHeaders to construct the message to be sent.
      Parameters:
      processorSupplier - the supplier
    • withDefaultEndpoint

      FluentProducerTemplate withDefaultEndpoint(String endpointUri)
      Sets the default endpoint
      Parameters:
      endpointUri - the endpoint URI to send to
    • withDefaultEndpoint

      FluentProducerTemplate withDefaultEndpoint(EndpointProducerResolver resolver)
      Sets the default endpoint
      Parameters:
      resolver - the EndpointProducerResolver that supply the endpoint to send to.
    • withDefaultEndpoint

      FluentProducerTemplate withDefaultEndpoint(Endpoint endpoint)
      Sets the default endpoint
      Parameters:
      endpoint - the endpoint to send to
    • to

      default FluentProducerTemplate to(String endpointUri)
      Endpoint to send to
      Parameters:
      endpointUri - the endpoint URI to send to
    • toF

      default FluentProducerTemplate toF(String uri, Object... args)
      Endpoint to send to.
      Parameters:
      uri - the String formatted endpoint uri to send to
      args - arguments for the string formatting of the uri
    • to

      Endpoint to send to
      Parameters:
      resolver - the EndpointProducerResolver that supply the endpoint to send to.
    • to

      Endpoint to send to
      Parameters:
      endpoint - the endpoint to send to
    • request

      Send to an endpoint (InOut) returning any result output body.
      Returns:
      the result
      Throws:
      CamelExecutionException - is thrown if error occurred
    • request

      <T> T request(Class<T> type) throws CamelExecutionException
      Send to an endpoint (InOut).
      Parameters:
      type - the expected response type
      Returns:
      the result
      Throws:
      CamelExecutionException - is thrown if error occurred
    • asyncRequest

      Future<Object> asyncRequest()
      Sends asynchronously to the given endpoint (InOut).
      Returns:
      a handle to be used to get the response in the future
    • asyncRequest

      <T> Future<T> asyncRequest(Class<T> type)
      Sends asynchronously to the given endpoint (InOut).
      Parameters:
      type - the expected response type
      Returns:
      a handle to be used to get the response in the future
    • send

      Send to an endpoint (InOnly)
      Throws:
      CamelExecutionException - is thrown if error occurred
    • asyncSend

      Future<Exchange> asyncSend()
      Sends asynchronously to the given endpoint (InOnly).
      Returns:
      a handle to be used to get the response in the future