Package org.apache.camel
Interface FluentProducerTemplate
- All Superinterfaces:
AutoCloseable
,Service
Template for working with Camel and sending
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
The
When using the fluent template its required to chain the methods such as:
All the methods which sends a message may throw
All the send or request methods will return the content according to this strategy:
Before using the template it must be started. And when you are done using the template, make sure to
Important note on usage: See this FAQ entry before using.
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 Summary
Modifier and TypeMethodDescriptionSends asynchronously to the given endpoint (InOut).<T> Future<T>
asyncRequest
(Class<T> type) Sends asynchronously to the given endpoint (InOut).Sends asynchronously to the given endpoint (InOnly).void
cleanUp()
Cleanup the cache (purging stale entries)Get theCamelContext
int
Gets an approximated size of the current cached resources in the backing cache pools.Get the default endpoint to use if none is specifiedint
Gets the maximum cache size used in the backing cache pools.boolean
Whether theEventNotifier
should be used by thisProducerTemplate
to send events about theExchange
being sent.request()
Send to an endpoint (InOut) returning any result output body.<T> T
Send to an endpoint (InOut).send()
Send to an endpoint (InOnly)void
setDefaultEndpoint
(Endpoint defaultEndpoint) Sets the default endpoint to use if none is specifiedvoid
setDefaultEndpointUri
(String endpointUri) Sets the default endpoint uri to use if none is specifiedvoid
setEventNotifierEnabled
(boolean enabled) Sets whether theEventNotifier
should be used by thisProducerTemplate
to send events about theExchange
being sent.void
setMaximumCacheSize
(int maximumCacheSize) Sets a custom maximum cache size to use in the backing cache pools.default FluentProducerTemplate
Endpoint to send toEndpoint to send todefault FluentProducerTemplate
to
(EndpointProducerResolver resolver) Endpoint to send todefault FluentProducerTemplate
Endpoint to send to.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.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.withDefaultEndpoint
(String endpointUri) Sets the default endpointwithDefaultEndpoint
(Endpoint endpoint) Sets the default endpointwithDefaultEndpoint
(EndpointProducerResolver resolver) Sets the default endpointwithExchange
(Supplier<Exchange> exchangeSupplier) Set the exchangeSupplier which will be invoke to get the exchange to be used for send.withExchange
(Exchange exchange) Set the exchange to use for send.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.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.withProcessor
(Supplier<Processor> processorSupplier) Set the processorSupplier which will be invoke to get the processor to be used for send/request.withProcessor
(Processor processor) Set the processor to use for send/request.withTemplateCustomizer
(Consumer<ProducerTemplate> templateCustomizer) To customize the producer template for advanced usage like to set the executor service to use.
-
Method Details
-
getCamelContext
CamelContext getCamelContext()Get theCamelContext
- 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
Sets the default endpoint to use if none is specified- Parameters:
defaultEndpoint
- the default endpoint instance
-
setDefaultEndpointUri
Sets the default endpoint uri to use if none is specified- Parameters:
endpointUri
- the default endpoint uri
-
setEventNotifierEnabled
void setEventNotifierEnabled(boolean enabled) Sets whether theEventNotifier
should be used by thisProducerTemplate
to send events about theExchange
being sent. By default this is enabled.- Parameters:
enabled
- true to enable, false to disable.
-
isEventNotifierEnabled
boolean isEventNotifierEnabled()Whether theEventNotifier
should be used by thisProducerTemplate
to send events about theExchange
being sent.- Returns:
- true if enabled, false otherwise
-
cleanUp
void cleanUp()Cleanup the cache (purging stale entries) -
withHeaders
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
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 headervalue
- 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
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 bodytype
- the type which the body should be converted to
-
withTemplateCustomizer
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()
- Parameters:
templateCustomizer
- the customizer
-
withExchange
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
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
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
- the processor
-
withProcessor
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
Sets the default endpoint- Parameters:
endpointUri
- the endpoint URI to send to
-
withDefaultEndpoint
Sets the default endpoint- Parameters:
resolver
- theEndpointProducerResolver
that supply the endpoint to send to.
-
withDefaultEndpoint
Sets the default endpoint- Parameters:
endpoint
- the endpoint to send to
-
to
Endpoint to send to- Parameters:
endpointUri
- the endpoint URI to send to
-
toF
Endpoint to send to.- Parameters:
uri
- the String formatted endpoint uri to send toargs
- arguments for the string formatting of the uri
-
to
Endpoint to send to- Parameters:
resolver
- theEndpointProducerResolver
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
Send to an endpoint (InOut).- Parameters:
type
- the expected response type- Returns:
- the result
- Throws:
CamelExecutionException
- is thrown if error occurred
-
asyncRequest
Sends asynchronously to the given endpoint (InOut).- Returns:
- a handle to be used to get the response in the future
-
asyncRequest
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
Sends asynchronously to the given endpoint (InOnly).- Returns:
- a handle to be used to get the response in the future
-