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.

    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:
    ProducerTemplate, ConsumerTemplate
    • Method Detail

      • 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)
      • withBodyAs

        FluentProducerTemplate withBodyAs​(Object body,
                                          Class<?> type)
        Set the message body after converting it to the given type
        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).
        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).
        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()
         
        Parameters:
        processor -
      • withProcessor

        FluentProducerTemplate withProcessor​(Supplier<Processor> processorSupplier)
        Set the processorSupplier which will be invoke to get the processor to be used for send/request.
        Parameters:
        processorSupplier - the supplier
      • 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
      • 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
      • asyncSend

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