Package org.apache.camel
Interface FluentProducerTemplate
-
- All Superinterfaces:
AutoCloseable
,Service
public interface FluentProducerTemplate extends Service
Template for working with Camel and sendingMessage
instances in anExchange
to anEndpoint
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 throwsCamelExecutionException
while others stores any thrown exception on the returnedExchange
.
TheFluentProducerTemplate
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 throwFailedToCreateProducerException
in case theProducer
could not be created. Or aNoSuchEndpointException
if the endpoint could not be resolved. There may be other related exceptions being thrown which occurs before theProducer
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 toService.stop()
the template.
Important note on usage: See this FAQ entry before using.- See Also:
ProducerTemplate
,ConsumerTemplate
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Deprecated Methods Modifier and Type Method Description Future<Object>
asyncRequest()
Sends asynchronously to the given endpoint (InOut).<T> Future<T>
asyncRequest(Class<T> type)
Sends asynchronously to the given endpoint (InOut).Future<Exchange>
asyncSend()
Sends asynchronously to the given endpoint (InOnly).void
cleanUp()
Cleanup the cache (purging stale entries)FluentProducerTemplate
clearAll()
Deprecated.the template automatic clears when sendingFluentProducerTemplate
clearBody()
Deprecated.the template automatic clears when sendingFluentProducerTemplate
clearHeaders()
Deprecated.the template automatic clears when sendingCamelContext
getCamelContext()
Get theCamelContext
int
getCurrentCacheSize()
Gets an approximated size of the current cached resources in the backing cache pools.Endpoint
getDefaultEndpoint()
Get the default endpoint to use if none is specifiedint
getMaximumCacheSize()
Gets the maximum cache size used in the backing cache pools.boolean
isEventNotifierEnabled()
Whether theEventNotifier
should be used by thisProducerTemplate
to send events about theExchange
being sent.Object
request()
Send to an endpoint (InOut) returning any result output body.<T> T
request(Class<T> type)
Send to an endpoint (InOut).Exchange
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
to(String endpointUri)
Endpoint to send toFluentProducerTemplate
to(Endpoint endpoint)
Endpoint to send todefault FluentProducerTemplate
to(EndpointProducerResolver resolver)
Endpoint to send todefault FluentProducerTemplate
toF(String uri, Object... args)
Endpoint to send to.FluentProducerTemplate
withBody(Object body)
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.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.FluentProducerTemplate
withDefaultEndpoint(String endpointUri)
Sets the default endpointFluentProducerTemplate
withDefaultEndpoint(Endpoint endpoint)
Sets the default endpointFluentProducerTemplate
withDefaultEndpoint(EndpointProducerResolver resolver)
Sets the default endpointFluentProducerTemplate
withExchange(Supplier<Exchange> exchangeSupplier)
Set the exchangeSupplier which will be invoke to get the exchange to be used for send.FluentProducerTemplate
withExchange(Exchange exchange)
Set the exchange to use for send.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.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.FluentProducerTemplate
withProcessor(Supplier<Processor> processorSupplier)
Set the processorSupplier which will be invoke to get the processor to be used for send/request.FluentProducerTemplate
withProcessor(Processor processor)
Set the processor to use for send/request.FluentProducerTemplate
withTemplateCustomizer(Consumer<ProducerTemplate> templateCustomizer)
To customize the producer template for advanced usage like to set the executor service to use.
-
-
-
Method Detail
-
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
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 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)
-
clearAll
@Deprecated FluentProducerTemplate clearAll()
Deprecated.the template automatic clears when sendingRemove the body and headers.
-
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 headervalue
- the value of the header
-
clearHeaders
@Deprecated FluentProducerTemplate clearHeaders()
Deprecated.the template automatic clears when sendingRemove the headers.
-
withBody
FluentProducerTemplate withBody(Object body)
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 bodytype
- the type which the body should be converted to
-
clearBody
@Deprecated FluentProducerTemplate clearBody()
Deprecated.the template automatic clears when sendingRemove the body.
-
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()
- 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()
- 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
- theEndpointProducerResolver
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 toargs
- arguments for the string formatting of the uri
-
to
default FluentProducerTemplate to(EndpointProducerResolver resolver)
Endpoint to send to- Parameters:
resolver
- theEndpointProducerResolver
that supply the endpoint to send to.
-
to
FluentProducerTemplate to(Endpoint endpoint)
Endpoint to send to- Parameters:
endpoint
- the endpoint to send to
-
request
Object request() throws CamelExecutionException
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
Exchange send() throws CamelExecutionException
Send to an endpoint (InOnly)- Throws:
CamelExecutionException
- is thrown if error occurred
-
-