Package org.apache.camel
Interface FluentProducerTemplate
-
- All Superinterfaces:
AutoCloseable,Service
public interface FluentProducerTemplate extends Service
Template for working with Camel and sendingMessageinstances in anExchangeto anEndpointusing 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 throwsCamelExecutionExceptionwhile others stores any thrown exception on the returnedExchange.
TheFluentProducerTemplateis 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 throwFailedToCreateProducerExceptionin case theProducercould not be created. Or aNoSuchEndpointExceptionif the endpoint could not be resolved. There may be other related exceptions being thrown which occurs before theProducerhas started sending the message.
All the send or request methods will return the content according to this strategy:- throws
CamelExecutionExceptionif 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).voidcleanUp()Cleanup the cache (purging stale entries)FluentProducerTemplateclearAll()Deprecated.the template automatic clears when sendingFluentProducerTemplateclearBody()Deprecated.the template automatic clears when sendingFluentProducerTemplateclearHeaders()Deprecated.the template automatic clears when sendingCamelContextgetCamelContext()Get theCamelContextintgetCurrentCacheSize()Gets an approximated size of the current cached resources in the backing cache pools.EndpointgetDefaultEndpoint()Get the default endpoint to use if none is specifiedintgetMaximumCacheSize()Gets the maximum cache size used in the backing cache pools.booleanisEventNotifierEnabled()Whether theEventNotifiershould be used by thisProducerTemplateto send events about theExchangebeing sent.Objectrequest()Send to an endpoint (InOut) returning any result output body.<T> Trequest(Class<T> type)Send to an endpoint (InOut).Exchangesend()Send to an endpoint (InOnly)voidsetDefaultEndpoint(Endpoint defaultEndpoint)Sets the default endpoint to use if none is specifiedvoidsetDefaultEndpointUri(String endpointUri)Sets the default endpoint uri to use if none is specifiedvoidsetEventNotifierEnabled(boolean enabled)Sets whether theEventNotifiershould be used by thisProducerTemplateto send events about theExchangebeing sent.voidsetMaximumCacheSize(int maximumCacheSize)Sets a custom maximum cache size to use in the backing cache pools.default FluentProducerTemplateto(String endpointUri)Endpoint to send toFluentProducerTemplateto(Endpoint endpoint)Endpoint to send todefault FluentProducerTemplateto(EndpointProducerResolver resolver)Endpoint to send todefault FluentProducerTemplatetoF(String uri, Object... args)Endpoint to send to.FluentProducerTemplatewithBody(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.FluentProducerTemplatewithBodyAs(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.FluentProducerTemplatewithDefaultEndpoint(String endpointUri)Sets the default endpointFluentProducerTemplatewithDefaultEndpoint(Endpoint endpoint)Sets the default endpointFluentProducerTemplatewithDefaultEndpoint(EndpointProducerResolver resolver)Sets the default endpointFluentProducerTemplatewithExchange(Supplier<Exchange> exchangeSupplier)Set the exchangeSupplier which will be invoke to get the exchange to be used for send.FluentProducerTemplatewithExchange(Exchange exchange)Set the exchange to use for send.FluentProducerTemplatewithHeader(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.FluentProducerTemplatewithHeaders(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.FluentProducerTemplatewithProcessor(Supplier<Processor> processorSupplier)Set the processorSupplier which will be invoke to get the processor to be used for send/request.FluentProducerTemplatewithProcessor(Processor processor)Set the processor to use for send/request.FluentProducerTemplatewithTemplateCustomizer(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 theEventNotifiershould be used by thisProducerTemplateto send events about theExchangebeing sent. By default this is enabled.- Parameters:
enabled- true to enable, false to disable.
-
isEventNotifierEnabled
boolean isEventNotifierEnabled()
Whether theEventNotifiershould be used by thisProducerTemplateto send events about theExchangebeing 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.
Note that it is invoked only once.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.
Important: You can either only use either withExchange, or withProcessor or a combination of withBody/withHeaders to construct the message to be sent.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- theEndpointProducerResolverthat 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- theEndpointProducerResolverthat 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
-
-