Class RabbitTemplate
- All Implemented Interfaces:
AmqpTemplate
,MessageListener
,PublisherCallbackChannel.Listener
,RabbitOperations
,ChannelAwareMessageListener
,ListenerContainerAware
,Aware
,BeanFactoryAware
,BeanNameAware
,DisposableBean
,InitializingBean
,ApplicationContextAware
,Lifecycle
- Direct Known Subclasses:
BatchingRabbitTemplate
,TestRabbitTemplate
Helper class that simplifies synchronous RabbitMQ access (sending and receiving messages).
The default settings are for non-transactional messaging, which reduces the amount of data exchanged with the broker.
To use a new transaction for every send or receive set the channelTransacted
flag. To extend the transaction over multiple invocations (more efficient), you can use a Spring transaction to
bracket the calls (with channelTransacted=true
as well).
The only mandatory property is the ConnectionFactory
. There are
strategies available for converting messages to and from Java objects (
MessageConverter
) and for converting message headers (known as message
properties in AMQP, see MessagePropertiesConverter
). The defaults probably do something sensible for typical use cases, as long as the message content-type is set
appropriately.
The "send" methods all have overloaded versions that allow you to explicitly target an exchange and a routing key, or you can set default values to be used in all send operations. The plain "receive" methods allow you to explicitly target a queue to receive from, or you can set a default value for the template that applies to all explicit receives. The convenience methods for send and receive use the sender defaults if no exchange or routing key is specified, but they always use a temporary queue for the receive leg, so the default queue is ignored.
- Since:
- 1.0
- Author:
- Mark Pollack, Mark Fisher, Dave Syer, Gary Russell, Artem Bilan, Ernest Sadykov, Mark Norkin, Mohammad Hewedy, Alexey Platonov, Leonardo Ferreira, Ngoc Nhan
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
A callback for publisher confirmations.static interface
A callback for returned messages.protected static class
AddsRabbitTemplate.TemplateConsumer.toString()
to theDefaultConsumer
.Nested classes/interfaces inherited from interface org.springframework.amqp.rabbit.core.RabbitOperations
RabbitOperations.OperationsCallback<T>
-
Field Summary
Fields inherited from class org.springframework.amqp.rabbit.connection.RabbitAccessor
logger
-
Constructor Summary
ConstructorDescriptionConvenient constructor for use with setter injection.RabbitTemplate
(ConnectionFactory connectionFactory) Create a rabbit template with default strategies and settings. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addAfterReceivePostProcessors
(MessagePostProcessor... afterReceivePostProcessors) AddMessagePostProcessor
that will be invoked immediately after aChannel#basicGet()
and before any message conversion is performed.void
addBeforePublishPostProcessors
(MessagePostProcessor... beforePublishPostProcessors) AddMessagePostProcessor
that will be invoked immediately before invokingChannel#basicPublish()
, after all other processing, except creating theAMQP.BasicProperties
fromMessageProperties
.void
addConsumerArg
(String arg, Object value) When using receive methods with a non-zero timeout, aConsumer
is created to receive the message.void
addListener
(com.rabbitmq.client.Channel channel) Add this template as a confirms listener for the provided channel.void
convertAndSend
(Object object) Convert a Java object to an AmqpMessage
and send it to a default exchange with a default routing key.void
convertAndSend
(Object message, MessagePostProcessor messagePostProcessor) Convert a Java object to an AmqpMessage
and send it to a default exchange with a default routing key.void
convertAndSend
(Object message, MessagePostProcessor messagePostProcessor, CorrelationData correlationData) Convert a Java object to an AmqpMessage
and send it to a default exchange with a default routing key.void
convertAndSend
(String routingKey, Object object) Convert a Java object to an AmqpMessage
and send it to a default exchange with a specific routing key.void
convertAndSend
(String routingKey, Object message, MessagePostProcessor messagePostProcessor) Convert a Java object to an AmqpMessage
and send it to a default exchange with a specific routing key.void
convertAndSend
(String routingKey, Object message, MessagePostProcessor messagePostProcessor, CorrelationData correlationData) Convert a Java object to an AmqpMessage
and send it to a default exchange with a specific routing key.void
convertAndSend
(String routingKey, Object object, CorrelationData correlationData) Convert a Java object to an AmqpMessage
and send it to a default exchange with a specific routing key.void
convertAndSend
(String exchange, String routingKey, Object object) Convert a Java object to an AmqpMessage
and send it to a specific exchange with a specific routing key.void
convertAndSend
(String exchange, String routingKey, Object message, MessagePostProcessor messagePostProcessor) Convert a Java object to an AmqpMessage
and send it to a specific exchange with a specific routing key.void
convertAndSend
(String exchange, String routingKey, Object message, MessagePostProcessor messagePostProcessor, CorrelationData correlationData) Convert a Java object to an AmqpMessage
and send it to a specific exchange with a specific routing key.void
convertAndSend
(String exchange, String routingKey, Object object, CorrelationData correlationData) Convert a Java object to an AmqpMessage
and send it to a specific exchange with a specific routing key.protected Message
convertMessageIfNecessary
(Object object) convertSendAndReceive
(Object message) Basic RPC pattern with conversion.convertSendAndReceive
(Object message, MessagePostProcessor messagePostProcessor) Basic RPC pattern with conversion.convertSendAndReceive
(Object message, MessagePostProcessor messagePostProcessor, CorrelationData correlationData) Basic RPC pattern with conversion.convertSendAndReceive
(Object message, CorrelationData correlationData) Basic RPC pattern with conversion.convertSendAndReceive
(String routingKey, Object message) Basic RPC pattern with conversion.convertSendAndReceive
(String routingKey, Object message, MessagePostProcessor messagePostProcessor) Basic RPC pattern with conversion.convertSendAndReceive
(String routingKey, Object message, MessagePostProcessor messagePostProcessor, CorrelationData correlationData) Basic RPC pattern with conversion.convertSendAndReceive
(String routingKey, Object message, CorrelationData correlationData) Basic RPC pattern with conversion.convertSendAndReceive
(String exchange, String routingKey, Object message) Basic RPC pattern with conversion.convertSendAndReceive
(String exchange, String routingKey, Object message, MessagePostProcessor messagePostProcessor) Basic RPC pattern with conversion.convertSendAndReceive
(String exchange, String routingKey, Object message, MessagePostProcessor messagePostProcessor, CorrelationData correlationData) Basic RPC pattern with conversion.convertSendAndReceive
(String exchange, String routingKey, Object message, CorrelationData correlationData) Basic RPC pattern with conversion.<T> T
convertSendAndReceiveAsType
(Object message, MessagePostProcessor messagePostProcessor, CorrelationData correlationData, ParameterizedTypeReference<T> responseType) Basic RPC pattern with conversion.<T> T
convertSendAndReceiveAsType
(Object message, MessagePostProcessor messagePostProcessor, ParameterizedTypeReference<T> responseType) Basic RPC pattern with conversion.<T> T
convertSendAndReceiveAsType
(Object message, CorrelationData correlationData, ParameterizedTypeReference<T> responseType) Basic RPC pattern with conversion.<T> T
convertSendAndReceiveAsType
(Object message, ParameterizedTypeReference<T> responseType) Basic RPC pattern with conversion.<T> T
convertSendAndReceiveAsType
(String routingKey, Object message, MessagePostProcessor messagePostProcessor, CorrelationData correlationData, ParameterizedTypeReference<T> responseType) Basic RPC pattern with conversion.<T> T
convertSendAndReceiveAsType
(String routingKey, Object message, MessagePostProcessor messagePostProcessor, ParameterizedTypeReference<T> responseType) Basic RPC pattern with conversion.<T> T
convertSendAndReceiveAsType
(String routingKey, Object message, CorrelationData correlationData, ParameterizedTypeReference<T> responseType) Basic RPC pattern with conversion.<T> T
convertSendAndReceiveAsType
(String routingKey, Object message, ParameterizedTypeReference<T> responseType) Basic RPC pattern with conversion.<T> T
convertSendAndReceiveAsType
(String exchange, String routingKey, Object message, MessagePostProcessor messagePostProcessor, CorrelationData correlationData, ParameterizedTypeReference<T> responseType) Basic RPC pattern with conversion.<T> T
convertSendAndReceiveAsType
(String exchange, String routingKey, Object message, MessagePostProcessor messagePostProcessor, ParameterizedTypeReference<T> responseType) Basic RPC pattern with conversion.<T> T
convertSendAndReceiveAsType
(String exchange, String routingKey, Object message, ParameterizedTypeReference<T> responseType) Basic RPC pattern with conversion.protected Message
convertSendAndReceiveRaw
(String exchange, String routingKey, Object message, MessagePostProcessor messagePostProcessor, CorrelationData correlationData) Convert and send a message and return the raw reply message, or null.void
correlationConvertAndSend
(Object object, CorrelationData correlationData) Convert a Java object to an AmqpMessage
and send it to a default exchange with a default routing key.void
destroy()
protected Message
doReceiveNoWait
(String queueName) Non-blocking receive.void
doSend
(com.rabbitmq.client.Channel channel, String exchangeArg, String routingKeyArg, Message message, boolean mandatory, CorrelationData correlationData) Send the given message to the specified exchange.protected Message
doSendAndReceive
(String exchange, String routingKey, Message message, CorrelationData correlationData) Send a message and wait for a reply.protected Message
doSendAndReceiveWithFixed
(String exchange, String routingKey, Message message, CorrelationData correlationData) protected Message
doSendAndReceiveWithTemporary
(String exchange, String routingKey, Message message, CorrelationData correlationData) protected void
doStart()
Perform additional start actions.protected void
doStop()
Perform additional stop actions.<T> T
execute
(ChannelCallback<T> action) Execute the callback with a channel and reliably close the channel afterwards.Invoked by the container during startup so it can verify the queue is correctly configured (if a simple reply queue name is used instead of exchange/routingKey).Return configured after receiveMessagePostProcessor
s ornull
.Return configured before postMessagePostProcessor
s ornull
.Return the configured default receive queue.The encoding used when converting between byte arrays and Strings in message properties.Return the message converter for this template.protected MessagePropertiesConverter
Return the converter for properties.getUnconfirmed
(long age) Gets unconfirmed correlation data older than age and removes them.int
Gets unconfirmed messages count.getUUID()
Returns the UUID used to identify this Listener for returns.void
handleConfirm
(PendingConfirm pendingConfirm, boolean ack) Invoked by the channel when a confirm is received.void
handleReturn
(com.rabbitmq.client.Return returned) Handle a returned message.protected void
Set up the default strategies.<T> T
invoke
(RabbitOperations.OperationsCallback<T> action, com.rabbitmq.client.ConfirmCallback acks, com.rabbitmq.client.ConfirmCallback nacks) Invoke operations on the same channel.protected boolean
isChannelLocallyTransacted
(com.rabbitmq.client.Channel channel) Check whether the given Channel is locally transacted, that is, whether its transaction is managed by this template's Channel handling and not by an external transaction coordinator.boolean
isMandatoryFor
(Message message) Return whether the provided message should be sent with the mandatory flag set.boolean
boolean
boolean
True if separate publisher connection(s) are used.protected void
logReceived
(String prefix, Message message) Log a received message.nullSafeExchange
(String exchange) Return the exchange or the default exchange if null.Return the routing key or the default routing key if null.protected void
observeTheSend
(com.rabbitmq.client.Channel channel, Message message, boolean mandatory, String exch, String rKey) void
Callback for processing a received Rabbit message.receive()
Receive a message if there is one from a default queue.receive
(long timeoutMillis) Receive a message from a default queue, waiting up to the specified wait time if necessary for a message to become available.Receive a message if there is one from a specific queue.Receive a message from a specific queue, waiting up to the specified wait time if necessary for a message to become available.Receive a message if there is one from a default queue and convert it to a Java object.receiveAndConvert
(long timeoutMillis) Receive a message if there is one from a default queue and convert it to a Java object.<T> T
receiveAndConvert
(long timeoutMillis, ParameterizedTypeReference<T> type) Receive a message if there is one from a default queue and convert it to a Java object.receiveAndConvert
(String queueName) Receive a message if there is one from a specific queue and convert it to a Java object.receiveAndConvert
(String queueName, long timeoutMillis) Receive a message if there is one from a specific queue and convert it to a Java object.<T> T
receiveAndConvert
(String queueName, long timeoutMillis, ParameterizedTypeReference<T> type) Receive a message if there is one from a specific queue and convert it to a Java object.<T> T
receiveAndConvert
(String queueName, ParameterizedTypeReference<T> type) Receive a message if there is one from a specific queue and convert it to a Java object.<T> T
Receive a message if there is one from a default queue and convert it to a Java object.<R,
S> boolean receiveAndReply
(String queueName, ReceiveAndReplyCallback<R, S> callback) Receive a message if there is one from provided queue, invoke providedReceiveAndReplyCallback
and send reply message, if thecallback
returns one, to thereplyTo
Address
fromMessageProperties
or to default exchange and default routingKey.<R,
S> boolean receiveAndReply
(String queueName, ReceiveAndReplyCallback<R, S> callback, String replyExchange, String replyRoutingKey) Receive a message if there is one from provided queue, invoke providedReceiveAndReplyCallback
and send reply message, if thecallback
returns one, to the providedexchange
androutingKey
.<R,
S> boolean receiveAndReply
(String queueName, ReceiveAndReplyCallback<R, S> callback, ReplyToAddressCallback<S> replyToAddressCallback) Receive a message if there is one from provided queue, invoke providedReceiveAndReplyCallback
and send reply message, if thecallback
returns one, to thereplyTo
Address
from result ofReplyToAddressCallback
.<R,
S> boolean receiveAndReply
(ReceiveAndReplyCallback<R, S> callback) Receive a message if there is one from a default queue, invoke providedReceiveAndReplyCallback
and send reply message, if thecallback
returns one, to thereplyTo
Address
fromMessageProperties
or to default exchange and default routingKey.<R,
S> boolean receiveAndReply
(ReceiveAndReplyCallback<R, S> callback, String exchange, String routingKey) Receive a message if there is one from default queue, invoke providedReceiveAndReplyCallback
and send reply message, if thecallback
returns one, to the providedexchange
androutingKey
.<R,
S> boolean receiveAndReply
(ReceiveAndReplyCallback<R, S> callback, ReplyToAddressCallback<S> replyToAddressCallback) Receive a message if there is one from a default queue, invoke providedReceiveAndReplyCallback
and send reply message, if thecallback
returns one, to thereplyTo
Address
from result ofReplyToAddressCallback
.boolean
removeAfterReceivePostProcessor
(MessagePostProcessor afterReceivePostProcessor) Remove the providedMessagePostProcessor
from theafterReceivePostProcessors
list.boolean
removeBeforePublishPostProcessor
(MessagePostProcessor beforePublishPostProcessor) Remove the providedMessagePostProcessor
from thebeforePublishPostProcessors
list.removeConsumerArg
(String arg) When using receive methods with a non-zero timeout, aConsumer
is created to receive the message.protected void
replyTimedOut
(String correlationId) Subclasses can implement this to be notified that a reply has timed out.void
revoke
(com.rabbitmq.client.Channel channel) When called, this listener should remove all references to the channel - it will no longer be invoked by the channel.void
Send a message to a specific exchange with a specific routing key.void
send
(String exchange, String routingKey, Message message, CorrelationData correlationData) Send a message to a specific exchange with a specific routing key.void
Send a message to a default exchange with a specific routing key.void
send
(String routingKey, Message message, CorrelationData correlationData) Send a message to the default exchange with a specific routing key.void
Send a message to a default exchange with a default routing key.sendAndReceive
(String exchange, String routingKey, Message message) Basic RPC pattern.sendAndReceive
(String exchange, String routingKey, Message message, CorrelationData correlationData) sendAndReceive
(String routingKey, Message message) Basic RPC pattern.sendAndReceive
(String routingKey, Message message, CorrelationData correlationData) sendAndReceive
(Message message) Basic RPC pattern.sendAndReceive
(Message message, CorrelationData correlationData) protected void
sendToRabbit
(com.rabbitmq.client.Channel channel, String exchange, String routingKey, boolean mandatory, Message message) void
setAfterReceivePostProcessors
(MessagePostProcessor... afterReceivePostProcessors) Set aMessagePostProcessor
that will be invoked immediately after aChannel#basicGet()
and before any message conversion is performed.void
setApplicationContext
(ApplicationContext applicationContext) void
setBeanFactory
(BeanFactory beanFactory) void
setBeanName
(String name) void
setBeforePublishPostProcessors
(MessagePostProcessor... beforePublishPostProcessors) SetMessagePostProcessor
s that will be invoked immediately before invokingChannel#basicPublish()
, after all other processing, except creating theAMQP.BasicProperties
fromMessageProperties
.void
setConfirmCallback
(RabbitTemplate.ConfirmCallback confirmCallback) final void
setConnectionFactory
(ConnectionFactory connectionFactory) Set the ConnectionFactory to use for obtaining RabbitMQConnections
.void
setCorrelationDataPostProcessor
(CorrelationDataPostProcessor correlationDataPostProcessor) Set aCorrelationDataPostProcessor
to be invoked before publishing a message.void
setCorrelationKey
(String correlationKey) If set to 'correlationId' (default) the correlationId property will be used; otherwise the supplied key will be used.void
setDefaultReceiveQueue
(String queue) The name of the default queue to receive messages from when none is specified explicitly.void
setEncoding
(String encoding) The encoding to use when converting between byte arrays and Strings in message properties.void
setExchange
(String exchange) The name of the default exchange to use for send operations when none is specified.void
setMandatory
(boolean mandatory) Set the mandatory flag when sending messages; only applies if areturnCallback
had been provided.void
setMandatoryExpression
(Expression mandatoryExpression) void
setMandatoryExpressionString
(String mandatoryExpression) void
setMessageConverter
(MessageConverter messageConverter) Set the message converter for this template.void
setMessagePropertiesConverter
(MessagePropertiesConverter messagePropertiesConverter) Set theMessagePropertiesConverter
for this template.void
setNoLocalReplyConsumer
(boolean noLocalReplyConsumer) Set to true for a no-local consumer.void
setObservationConvention
(RabbitTemplateObservationConvention observationConvention) Set an observation convention; used to add additional key/values to observations.void
setObservationEnabled
(boolean observationEnabled) Enable observation via micrometer.void
setReceiveConnectionFactorySelectorExpression
(Expression receiveConnectionFactorySelectorExpression) A SpELExpression
to evaluate against eachreceive
queueName
, if the providedRabbitAccessor.getConnectionFactory()
is an instance ofAbstractRoutingConnectionFactory
.void
setReceiveTimeout
(long receiveTimeout) Specify the receive timeout in milliseconds when usingreceive()
methods (forsendAndReceive()
methods, refer toreplyTimeout
.void
setRecoveryCallback
(org.springframework.retry.RecoveryCallback<?> recoveryCallback) Add aRecoveryCallback
which is used for theretryTemplate.execute
.void
setReplyAddress
(String replyAddress) An address for replies; if not provided, a temporary exclusive, auto-delete queue will be used for each reply, unless RabbitMQ supports 'amq.rabbitmq.reply-to'void
setReplyErrorHandler
(ErrorHandler replyErrorHandler) When using a direct reply-to container for request/reply operations, set an error handler to be invoked when a reply delivery fails (e.g.void
setReplyTimeout
(long replyTimeout) Specify the timeout in milliseconds to be used when waiting for a reply Message when using one of the sendAndReceive methods.void
setRetryTemplate
(org.springframework.retry.support.RetryTemplate retryTemplate) Add aRetryTemplate
which will be used for all rabbit operations.void
setReturnsCallback
(RabbitTemplate.ReturnsCallback returnCallback) Set a callback to receive returned messages.void
setRoutingKey
(String routingKey) The value of a default routing key to use for send operations when none is specified.void
setSendConnectionFactorySelectorExpression
(Expression sendConnectionFactorySelectorExpression) A SpELExpression
to evaluate against each request message, if the providedRabbitAccessor.getConnectionFactory()
is an instance ofAbstractRoutingConnectionFactory
.void
setTaskExecutor
(Executor taskExecutor) Set a task executor to use when using aDirectReplyToMessageListenerContainer
.void
setUseChannelForCorrelation
(boolean useChannelForCorrelation) When using direct reply-to, set this to true to avoid the server needing to send the correlation id in a reply header.void
setUseDirectReplyToContainer
(boolean useDirectReplyToContainer) Set whether to use aDirectReplyToMessageListenerContainer
when direct reply-to is available and being used.void
setUsePublisherConnection
(boolean usePublisherConnection) To avoid deadlocked connections, it is generally recommended to use a separate connection for publishers and consumers (except when a publisher is participating in a consumer transaction).void
setUserCorrelationId
(boolean userCorrelationId) Set to true to use correlation id provided by the message instead of generating the correlation id for request/reply scenarios.void
setUserIdExpression
(Expression userIdExpression) Set an expression to be evaluated to set the userId message property if it evaluates to a non-null value and the property is not already set in the message to be sent.void
setUserIdExpressionString
(String userIdExpression) Set an expression to be evaluated to set the userId message property if it evaluates to a non-null value and the property is not already set in the message to be sent.void
setUseTemporaryReplyQueues
(boolean value) By default, when the broker supports it and noreplyAddress
is provided, send/receive methods will use Direct reply-to.void
start()
void
stop()
protected boolean
Override this method use some other criteria to decide whether to use (direct reply-to).boolean
waitForConfirms
(long timeout) Delegate to the underlying dedicated channel to wait for confirms.void
waitForConfirmsOrDie
(long timeout) Delegate to the underlying dedicated channel to wait for confirms.Methods inherited from class org.springframework.amqp.rabbit.connection.RabbitAccessor
afterPropertiesSet, convertRabbitAccessException, createConnection, getChannel, getConnection, getConnectionFactory, getObservationRegistry, getTransactionalResourceHolder, isChannelTransacted, obtainObservationRegistry, setChannelTransacted
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener
onMessage, onMessageBatch
Methods inherited from interface org.springframework.amqp.core.MessageListener
containerAckMode, isAsyncReplies, onMessageBatch
Methods inherited from interface org.springframework.amqp.rabbit.core.RabbitOperations
convertSendAndReceiveAsType, getConnectionFactory, invoke
-
Constructor Details
-
RabbitTemplate
public RabbitTemplate()Convenient constructor for use with setter injection. Don't forget to set the connection factory. -
RabbitTemplate
Create a rabbit template with default strategies and settings.- Parameters:
connectionFactory
- the connection factory to use
-
-
Method Details
-
initDefaultStrategies
protected void initDefaultStrategies()Set up the default strategies. Subclasses can override if necessary. -
setConnectionFactory
Description copied from class:RabbitAccessor
Set the ConnectionFactory to use for obtaining RabbitMQConnections
.- Overrides:
setConnectionFactory
in classRabbitAccessor
- Parameters:
connectionFactory
- The connection factory.
-
setApplicationContext
- Specified by:
setApplicationContext
in interfaceApplicationContextAware
- Throws:
BeansException
-
setObservationEnabled
public void setObservationEnabled(boolean observationEnabled) Enable observation via micrometer.- Parameters:
observationEnabled
- true to enable.- Since:
- 3.0
-
setObservationConvention
Set an observation convention; used to add additional key/values to observations.- Parameters:
observationConvention
- the convention.- Since:
- 3.0
-
setExchange
The name of the default exchange to use for send operations when none is specified. Defaults to""
which is the default exchange in the broker (per the AMQP specification).- Parameters:
exchange
- the exchange name to use for send operations
-
getExchange
- Returns:
- the name of the default exchange used by this template.
- Since:
- 1.6
-
setRoutingKey
The value of a default routing key to use for send operations when none is specified. Default is empty which is not helpful when using the default (or any direct) exchange, but fine if the exchange is a headers exchange for instance.- Parameters:
routingKey
- the default routing key to use for send operations
-
getRoutingKey
- Returns:
- the default routing key used by this template.
- Since:
- 1.6
-
setDefaultReceiveQueue
The name of the default queue to receive messages from when none is specified explicitly.- Parameters:
queue
- the default queue name to use for receive- Since:
- 2.1.2
-
getDefaultReceiveQueue
Return the configured default receive queue.- Returns:
- the queue or null if not configured.
- Since:
- 2.2.22
-
setEncoding
The encoding to use when converting between byte arrays and Strings in message properties.- Parameters:
encoding
- the encoding to set
-
getEncoding
The encoding used when converting between byte arrays and Strings in message properties.- Returns:
- the encoding.
-
setReplyAddress
An address for replies; if not provided, a temporary exclusive, auto-delete queue will be used for each reply, unless RabbitMQ supports 'amq.rabbitmq.reply-to'The address can be a simple queue name (in which case the reply will be routed via the default exchange), or with the form
exchange/routingKey
to route the reply using an explicit exchange and routing key.- Parameters:
replyAddress
- the replyAddress to set
-
setReceiveTimeout
public void setReceiveTimeout(long receiveTimeout) Specify the receive timeout in milliseconds when usingreceive()
methods (forsendAndReceive()
methods, refer toreplyTimeout
. By default, the value is zero, which means thereceive()
methods will returnnull
immediately if there is no message available. Set to less than zero to wait for a message indefinitely.- Parameters:
receiveTimeout
- the timeout.- Since:
- 1.5
-
setReplyTimeout
public void setReplyTimeout(long replyTimeout) Specify the timeout in milliseconds to be used when waiting for a reply Message when using one of the sendAndReceive methods. The default value is defined asDEFAULT_REPLY_TIMEOUT
. A negative value indicates an indefinite timeout. Not used in the plain receive methods because there is no blocking receive operation defined in the protocol.- Parameters:
replyTimeout
- the reply timeout in milliseconds- See Also:
-
setMessageConverter
Set the message converter for this template. Used to resolve Object parameters to convertAndSend methods and Object results from receiveAndConvert methods.The default converter is a SimpleMessageConverter, which is able to handle byte arrays, Strings, and Serializable Objects depending on the message content type header.
- Parameters:
messageConverter
- The message converter.- See Also:
-
setMessagePropertiesConverter
Set theMessagePropertiesConverter
for this template. This converter is used to convert between raw byte content in the message headers and plain Java objects. In particular there are limitations when dealing with very long string headers, which hopefully are rare in practice, but if you need to use long headers you might need to inject a special converter here.- Parameters:
messagePropertiesConverter
- The message properties converter.
-
getMessagePropertiesConverter
Return the converter for properties.- Returns:
- the converter.
- Since:
- 2.0
-
getMessageConverter
Return the message converter for this template. Useful for clients that want to take advantage of the converter inChannelCallback
implementations.- Returns:
- The message converter.
-
setConfirmCallback
-
setReturnsCallback
Set a callback to receive returned messages.- Parameters:
returnCallback
- the callback.
-
setMandatory
public void setMandatory(boolean mandatory) Set the mandatory flag when sending messages; only applies if areturnCallback
had been provided.- Parameters:
mandatory
- the mandatory to set.
-
setMandatoryExpression
- Parameters:
mandatoryExpression
- a SpELExpression
to evaluate against each request message, if areturnCallback
has been provided. The result of the evaluation must be aboolean
value.- Since:
- 1.4
-
setMandatoryExpressionString
- Parameters:
mandatoryExpression
- a SpELExpression
to evaluate against each request message, if areturnCallback
has been provided. The result of the evaluation must be aboolean
value.- Since:
- 2.0
-
setSendConnectionFactorySelectorExpression
public void setSendConnectionFactorySelectorExpression(Expression sendConnectionFactorySelectorExpression) A SpELExpression
to evaluate against each request message, if the providedRabbitAccessor.getConnectionFactory()
is an instance ofAbstractRoutingConnectionFactory
.The result of this expression is used as
lookupKey
to get the targetConnectionFactory
fromAbstractRoutingConnectionFactory
directly.If this expression is evaluated to
null
, we fallback to the normalAbstractRoutingConnectionFactory
logic.If there is no target
ConnectionFactory
with the evaluatedlookupKey
, we fallback to the normalAbstractRoutingConnectionFactory
logic only if its propertylenientFallback == true
.This expression is used for
send
operations.- Parameters:
sendConnectionFactorySelectorExpression
- a SpELExpression
to evaluate- Since:
- 1.4
-
setReceiveConnectionFactorySelectorExpression
public void setReceiveConnectionFactorySelectorExpression(Expression receiveConnectionFactorySelectorExpression) A SpELExpression
to evaluate against eachreceive
queueName
, if the providedRabbitAccessor.getConnectionFactory()
is an instance ofAbstractRoutingConnectionFactory
.The result of this expression is used as
lookupKey
to get the targetConnectionFactory
fromAbstractRoutingConnectionFactory
directly.If this expression is evaluated to
null
, we fallback to the normalAbstractRoutingConnectionFactory
logic.If there is no target
ConnectionFactory
with the evaluatedlookupKey
, we fallback to the normalAbstractRoutingConnectionFactory
logic only if its propertylenientFallback == true
.This expression is used for
receive
operations.- Parameters:
receiveConnectionFactorySelectorExpression
- a SpELExpression
to evaluate- Since:
- 1.4
-
setCorrelationKey
If set to 'correlationId' (default) the correlationId property will be used; otherwise the supplied key will be used.- Parameters:
correlationKey
- the correlationKey to set
-
setRetryTemplate
public void setRetryTemplate(org.springframework.retry.support.RetryTemplate retryTemplate) Add aRetryTemplate
which will be used for all rabbit operations.- Parameters:
retryTemplate
- The retry template.
-
setRecoveryCallback
public void setRecoveryCallback(org.springframework.retry.RecoveryCallback<?> recoveryCallback) Add aRecoveryCallback
which is used for theretryTemplate.execute
. IfretryTemplate
isn't providedrecoveryCallback
is ignored.RecoveryCallback
should produce result compatible withexecute(ChannelCallback)
return type.- Parameters:
recoveryCallback
- The retry recoveryCallback.- Since:
- 1.4
-
setBeanFactory
- Specified by:
setBeanFactory
in interfaceBeanFactoryAware
- Throws:
BeansException
-
getBeforePublishPostProcessors
Return configured before postMessagePostProcessor
s ornull
.- Returns:
- configured before post
MessagePostProcessor
s ornull
. - Since:
- 3.2
-
setBeforePublishPostProcessors
SetMessagePostProcessor
s that will be invoked immediately before invokingChannel#basicPublish()
, after all other processing, except creating theAMQP.BasicProperties
fromMessageProperties
. May be used for operations such as compression. Processors are invoked in order, depending onPriorityOrder
,Order
and finally unordered.- Parameters:
beforePublishPostProcessors
- the post processor.- Since:
- 1.4.2
- See Also:
-
addBeforePublishPostProcessors
AddMessagePostProcessor
that will be invoked immediately before invokingChannel#basicPublish()
, after all other processing, except creating theAMQP.BasicProperties
fromMessageProperties
. May be used for operations such as compression. Processors are invoked in order, depending onPriorityOrder
,Order
and finally unordered.In contrast to
setBeforePublishPostProcessors(MessagePostProcessor...)
, this method does not override the previously added beforePublishPostProcessors.- Parameters:
beforePublishPostProcessors
- the post processor.- Since:
- 2.1.4
-
removeBeforePublishPostProcessor
Remove the providedMessagePostProcessor
from thebeforePublishPostProcessors
list.- Parameters:
beforePublishPostProcessor
- the MessagePostProcessor to remove.- Returns:
- the boolean if the provided post processor has been removed.
- Since:
- 2.1.4
- See Also:
-
setAfterReceivePostProcessors
Set aMessagePostProcessor
that will be invoked immediately after aChannel#basicGet()
and before any message conversion is performed. May be used for operations such as decompression. Processors are invoked in order, depending onPriorityOrder
,Order
and finally unordered.- Parameters:
afterReceivePostProcessors
- the post processor.- Since:
- 1.5
- See Also:
-
getAfterReceivePostProcessors
Return configured after receiveMessagePostProcessor
s ornull
.- Returns:
- configured after receive
MessagePostProcessor
s ornull
. - Since:
- 2.1.5
-
addAfterReceivePostProcessors
AddMessagePostProcessor
that will be invoked immediately after aChannel#basicGet()
and before any message conversion is performed. May be used for operations such as decompression. Processors are invoked in order, depending onPriorityOrder
,Order
and finally unordered.In contrast to
setAfterReceivePostProcessors(MessagePostProcessor...)
, this method does not override the previously added afterReceivePostProcessors.- Parameters:
afterReceivePostProcessors
- the post processor.- Since:
- 2.1.4
-
removeAfterReceivePostProcessor
Remove the providedMessagePostProcessor
from theafterReceivePostProcessors
list.- Parameters:
afterReceivePostProcessor
- the MessagePostProcessor to remove.- Returns:
- the boolean if the provided post processor has been removed.
- Since:
- 2.1.4
- See Also:
-
setCorrelationDataPostProcessor
public void setCorrelationDataPostProcessor(CorrelationDataPostProcessor correlationDataPostProcessor) Set aCorrelationDataPostProcessor
to be invoked before publishing a message. Correlation data is used to correlate publisher confirms.- Parameters:
correlationDataPostProcessor
- the post processor.- Since:
- 1.6.7
- See Also:
-
setUseTemporaryReplyQueues
public void setUseTemporaryReplyQueues(boolean value) By default, when the broker supports it and noreplyAddress
is provided, send/receive methods will use Direct reply-to. Setting this property to true will override that behavior and use a temporary, auto-delete, queue for each request instead. Changing this property has no effect once the first request has been processed.- Parameters:
value
- true to use temporary queues.- Since:
- 1.6
-
setUseDirectReplyToContainer
public void setUseDirectReplyToContainer(boolean useDirectReplyToContainer) Set whether to use aDirectReplyToMessageListenerContainer
when direct reply-to is available and being used. When false, a new consumer is created for each request (the mechanism used in versions prior to 2.0). Default true.- Parameters:
useDirectReplyToContainer
- set to false to use a consumer per request.- Since:
- 2.0
- See Also:
-
setUserIdExpression
Set an expression to be evaluated to set the userId message property if it evaluates to a non-null value and the property is not already set in the message to be sent. See validated-user-id- Parameters:
userIdExpression
- the expression.- Since:
- 1.6
-
setUserIdExpressionString
Set an expression to be evaluated to set the userId message property if it evaluates to a non-null value and the property is not already set in the message to be sent. See validated-user-id- Parameters:
userIdExpression
- the expression.- Since:
- 1.6
-
setBeanName
- Specified by:
setBeanName
in interfaceBeanNameAware
-
setTaskExecutor
Set a task executor to use when using aDirectReplyToMessageListenerContainer
.- Parameters:
taskExecutor
- the executor.- Since:
- 2.0
-
setUserCorrelationId
public void setUserCorrelationId(boolean userCorrelationId) Set to true to use correlation id provided by the message instead of generating the correlation id for request/reply scenarios. The correlation id must be unique for all in-process requests to avoid cross talk.Users must therefore take create care to ensure uniqueness.
- Parameters:
userCorrelationId
- true to use user correlation data.
-
isUsePublisherConnection
public boolean isUsePublisherConnection()True if separate publisher connection(s) are used.- Returns:
- true or false.
- Since:
- 2.0.2
- See Also:
-
setUsePublisherConnection
public void setUsePublisherConnection(boolean usePublisherConnection) To avoid deadlocked connections, it is generally recommended to use a separate connection for publishers and consumers (except when a publisher is participating in a consumer transaction). Default 'false'. When setting this to true, be careful in that aRabbitAdmin
that uses this template will declare queues on the publisher connection; this may not be what you expect, especially with exclusive queues that might be consumed in this application.- Parameters:
usePublisherConnection
- true to use a publisher connection.- Since:
- 2.0.2
-
setNoLocalReplyConsumer
public void setNoLocalReplyConsumer(boolean noLocalReplyConsumer) Set to true for a no-local consumer. Defaults to false.- Parameters:
noLocalReplyConsumer
- true for a no-local consumer.- Since:
- 2.1
- See Also:
-
AbstractMessageListenerContainer.setNoLocal(boolean)
Channel.basicConsume(String, boolean, String, boolean, boolean, Map, com.rabbitmq.client.Consumer)
-
setReplyErrorHandler
When using a direct reply-to container for request/reply operations, set an error handler to be invoked when a reply delivery fails (e.g. due to a late reply).- Parameters:
replyErrorHandler
- the reply error handler- Since:
- 2.0.11
- See Also:
-
setUseChannelForCorrelation
public void setUseChannelForCorrelation(boolean useChannelForCorrelation) When using direct reply-to, set this to true to avoid the server needing to send the correlation id in a reply header. Use the channel to correlate the reply to a request instead.- Parameters:
useChannelForCorrelation
- true to use the channel.- Since:
- 2.3.7
-
expectedQueueNames
Invoked by the container during startup so it can verify the queue is correctly configured (if a simple reply queue name is used instead of exchange/routingKey).- Specified by:
expectedQueueNames
in interfaceListenerContainerAware
- Returns:
- the queue name, if configured.
- Since:
- 1.5
-
getUnconfirmed
Gets unconfirmed correlation data older than age and removes them.- Parameters:
age
- in milliseconds- Returns:
- the collection of correlation data for which confirms have not been received or null if no such confirms exist.
-
getUnconfirmedCount
public int getUnconfirmedCount()Gets unconfirmed messages count.- Returns:
- The count of the messages that are not confirmed yet by RabbitMQ.
- Since:
- 2.0
-
addConsumerArg
When using receive methods with a non-zero timeout, aConsumer
is created to receive the message. Use this property to add arguments to the consumer (e.g.x-priority
).- Parameters:
arg
- the argument name to pass into thebasicConsume
operation.value
- the argument value to pass into thebasicConsume
operation.- Since:
- 2.4.8
- See Also:
-
removeConsumerArg
When using receive methods with a non-zero timeout, aConsumer
is created to receive the message. Use this method to remove an argument from those passed into thebasicConsume
operation.- Parameters:
arg
- the argument name.- Returns:
- the previous value.
- Since:
- 2.4.8
- See Also:
-
start
public void start()- Specified by:
start
in interfaceLifecycle
- Specified by:
start
in interfaceRabbitOperations
-
doStart
protected void doStart()Perform additional start actions.- Since:
- 2.0
-
stop
public void stop()- Specified by:
stop
in interfaceLifecycle
- Specified by:
stop
in interfaceRabbitOperations
-
doStop
protected void doStop()Perform additional stop actions.- Since:
- 2.0
-
isRunning
public boolean isRunning()- Specified by:
isRunning
in interfaceLifecycle
- Specified by:
isRunning
in interfaceRabbitOperations
-
destroy
public void destroy()- Specified by:
destroy
in interfaceDisposableBean
-
useDirectReplyTo
protected boolean useDirectReplyTo()Override this method use some other criteria to decide whether to use (direct reply-to). The default implementation returns true if the broker supports it and there is noreplyAddress
set anduseTemporaryReplyQueues
is false. When direct reply-to is not used, the template will create a temporary, exclusive, auto-delete queue for the reply.This method is invoked once only - when the first message is sent, from a locked block.
- Returns:
- true to use direct reply-to.
-
send
Description copied from interface:AmqpTemplate
Send a message to a default exchange with a default routing key.- Specified by:
send
in interfaceAmqpTemplate
- Parameters:
message
- a message to send- Throws:
AmqpException
- if there is a problem
-
send
Description copied from interface:AmqpTemplate
Send a message to a default exchange with a specific routing key.- Specified by:
send
in interfaceAmqpTemplate
- Parameters:
routingKey
- the routing keymessage
- a message to send- Throws:
AmqpException
- if there is a problem
-
send
public void send(String routingKey, Message message, CorrelationData correlationData) throws AmqpException Description copied from interface:RabbitOperations
Send a message to the default exchange with a specific routing key.- Specified by:
send
in interfaceRabbitOperations
- Parameters:
routingKey
- the routing keymessage
- a message to sendcorrelationData
- data to correlate publisher confirms.- Throws:
AmqpException
- if there is a problem
-
send
Description copied from interface:AmqpTemplate
Send a message to a specific exchange with a specific routing key.- Specified by:
send
in interfaceAmqpTemplate
- Parameters:
exchange
- the name of the exchangeroutingKey
- the routing keymessage
- a message to send- Throws:
AmqpException
- if there is a problem
-
send
public void send(String exchange, String routingKey, Message message, @Nullable CorrelationData correlationData) throws AmqpException Description copied from interface:RabbitOperations
Send a message to a specific exchange with a specific routing key.- Specified by:
send
in interfaceRabbitOperations
- Parameters:
exchange
- the name of the exchangeroutingKey
- the routing keymessage
- a message to sendcorrelationData
- data to correlate publisher confirms.- Throws:
AmqpException
- if there is a problem
-
convertAndSend
Description copied from interface:AmqpTemplate
Convert a Java object to an AmqpMessage
and send it to a default exchange with a default routing key.- Specified by:
convertAndSend
in interfaceAmqpTemplate
- Parameters:
object
- a message to send- Throws:
AmqpException
- if there is a problem
-
correlationConvertAndSend
public void correlationConvertAndSend(Object object, CorrelationData correlationData) throws AmqpException Description copied from interface:RabbitOperations
Convert a Java object to an AmqpMessage
and send it to a default exchange with a default routing key.- Specified by:
correlationConvertAndSend
in interfaceRabbitOperations
- Parameters:
object
- a message to sendcorrelationData
- data to correlate publisher confirms.- Throws:
AmqpException
- if there is a problem
-
convertAndSend
Description copied from interface:AmqpTemplate
Convert a Java object to an AmqpMessage
and send it to a default exchange with a specific routing key.- Specified by:
convertAndSend
in interfaceAmqpTemplate
- Parameters:
routingKey
- the routing keyobject
- a message to send- Throws:
AmqpException
- if there is a problem
-
convertAndSend
public void convertAndSend(String routingKey, Object object, CorrelationData correlationData) throws AmqpException Description copied from interface:RabbitOperations
Convert a Java object to an AmqpMessage
and send it to a default exchange with a specific routing key.- Specified by:
convertAndSend
in interfaceRabbitOperations
- Parameters:
routingKey
- the routing keyobject
- a message to sendcorrelationData
- data to correlate publisher confirms.- Throws:
AmqpException
- if there is a problem
-
convertAndSend
Description copied from interface:AmqpTemplate
Convert a Java object to an AmqpMessage
and send it to a specific exchange with a specific routing key.- Specified by:
convertAndSend
in interfaceAmqpTemplate
- Parameters:
exchange
- the name of the exchangeroutingKey
- the routing keyobject
- a message to send- Throws:
AmqpException
- if there is a problem
-
convertAndSend
public void convertAndSend(String exchange, String routingKey, Object object, @Nullable CorrelationData correlationData) throws AmqpException Description copied from interface:RabbitOperations
Convert a Java object to an AmqpMessage
and send it to a specific exchange with a specific routing key.- Specified by:
convertAndSend
in interfaceRabbitOperations
- Parameters:
exchange
- the name of the exchangeroutingKey
- the routing keyobject
- a message to sendcorrelationData
- data to correlate publisher confirms.- Throws:
AmqpException
- if there is a problem
-
convertAndSend
public void convertAndSend(Object message, MessagePostProcessor messagePostProcessor) throws AmqpException Description copied from interface:AmqpTemplate
Convert a Java object to an AmqpMessage
and send it to a default exchange with a default routing key.- Specified by:
convertAndSend
in interfaceAmqpTemplate
- Parameters:
message
- a message to sendmessagePostProcessor
- a processor to apply to the message before it is sent- Throws:
AmqpException
- if there is a problem
-
convertAndSend
public void convertAndSend(String routingKey, Object message, MessagePostProcessor messagePostProcessor) throws AmqpException Description copied from interface:AmqpTemplate
Convert a Java object to an AmqpMessage
and send it to a default exchange with a specific routing key.- Specified by:
convertAndSend
in interfaceAmqpTemplate
- Parameters:
routingKey
- the routing keymessage
- a message to sendmessagePostProcessor
- a processor to apply to the message before it is sent- Throws:
AmqpException
- if there is a problem
-
convertAndSend
public void convertAndSend(Object message, MessagePostProcessor messagePostProcessor, CorrelationData correlationData) throws AmqpException Description copied from interface:RabbitOperations
Convert a Java object to an AmqpMessage
and send it to a default exchange with a default routing key.- Specified by:
convertAndSend
in interfaceRabbitOperations
- Parameters:
message
- a message to sendmessagePostProcessor
- a processor to apply to the message before it is sentcorrelationData
- data to correlate publisher confirms.- Throws:
AmqpException
- if there is a problem
-
convertAndSend
public void convertAndSend(String routingKey, Object message, MessagePostProcessor messagePostProcessor, CorrelationData correlationData) throws AmqpException Description copied from interface:RabbitOperations
Convert a Java object to an AmqpMessage
and send it to a default exchange with a specific routing key.- Specified by:
convertAndSend
in interfaceRabbitOperations
- Parameters:
routingKey
- the routing keymessage
- a message to sendmessagePostProcessor
- a processor to apply to the message before it is sentcorrelationData
- data to correlate publisher confirms.- Throws:
AmqpException
- if there is a problem
-
convertAndSend
public void convertAndSend(String exchange, String routingKey, Object message, MessagePostProcessor messagePostProcessor) throws AmqpException Description copied from interface:AmqpTemplate
Convert a Java object to an AmqpMessage
and send it to a specific exchange with a specific routing key.- Specified by:
convertAndSend
in interfaceAmqpTemplate
- Parameters:
exchange
- the name of the exchangeroutingKey
- the routing keymessage
- a message to sendmessagePostProcessor
- a processor to apply to the message before it is sent- Throws:
AmqpException
- if there is a problem
-
convertAndSend
public void convertAndSend(String exchange, String routingKey, Object message, MessagePostProcessor messagePostProcessor, @Nullable CorrelationData correlationData) throws AmqpException Description copied from interface:RabbitOperations
Convert a Java object to an AmqpMessage
and send it to a specific exchange with a specific routing key.- Specified by:
convertAndSend
in interfaceRabbitOperations
- Parameters:
exchange
- the name of the exchangeroutingKey
- the routing keymessage
- a message to sendmessagePostProcessor
- a processor to apply to the message before it is sentcorrelationData
- data to correlate publisher confirms.- Throws:
AmqpException
- if there is a problem
-
receive
Description copied from interface:AmqpTemplate
Receive a message if there is one from a default queue. Returns immediately, possibly with a null value.- Specified by:
receive
in interfaceAmqpTemplate
- Returns:
- a message or null if there is none waiting
- Throws:
AmqpException
- if there is a problem
-
receive
Description copied from interface:AmqpTemplate
Receive a message if there is one from a specific queue. Returns immediately, possibly with a null value.- Specified by:
receive
in interfaceAmqpTemplate
- Parameters:
queueName
- the name of the queue to poll- Returns:
- a message or null if there is none waiting
-
doReceiveNoWait
Non-blocking receive.- Parameters:
queueName
- the queue to receive from.- Returns:
- The message, or null if none immediately available.
- Since:
- 1.5
-
receive
Description copied from interface:AmqpTemplate
Receive a message from a default queue, waiting up to the specified wait time if necessary for a message to become available.- Specified by:
receive
in interfaceAmqpTemplate
- Parameters:
timeoutMillis
- how long to wait before giving up. Zero value means the method will returnnull
immediately if there is no message available. Negative value makes method wait for a message indefinitely.- Returns:
- a message or null if the time expires
- Throws:
AmqpException
- if there is a problem
-
receive
Description copied from interface:AmqpTemplate
Receive a message from a specific queue, waiting up to the specified wait time if necessary for a message to become available.- Specified by:
receive
in interfaceAmqpTemplate
- Parameters:
queueName
- the queue to receive fromtimeoutMillis
- how long to wait before giving up. Zero value means the method will returnnull
immediately if there is no message available. Negative value makes method wait for a message indefinitely.- Returns:
- a message or null if the time expires
-
receiveAndConvert
Description copied from interface:AmqpTemplate
Receive a message if there is one from a default queue and convert it to a Java object. Returns immediately, possibly with a null value.- Specified by:
receiveAndConvert
in interfaceAmqpTemplate
- Returns:
- a message or null if there is none waiting
- Throws:
AmqpException
- if there is a problem
-
receiveAndConvert
Description copied from interface:AmqpTemplate
Receive a message if there is one from a specific queue and convert it to a Java object. Returns immediately, possibly with a null value.- Specified by:
receiveAndConvert
in interfaceAmqpTemplate
- Parameters:
queueName
- the name of the queue to poll- Returns:
- a message or null if there is none waiting
- Throws:
AmqpException
- if there is a problem
-
receiveAndConvert
Description copied from interface:AmqpTemplate
Receive a message if there is one from a default queue and convert it to a Java object. Wait up to the specified wait time if necessary for a message to become available.- Specified by:
receiveAndConvert
in interfaceAmqpTemplate
- Parameters:
timeoutMillis
- how long to wait before giving up. Zero value means the method will returnnull
immediately if there is no message available. Negative value makes method wait for a message indefinitely.- Returns:
- a message or null if the time expires
- Throws:
AmqpException
- if there is a problem
-
receiveAndConvert
@Nullable public Object receiveAndConvert(String queueName, long timeoutMillis) throws AmqpException Description copied from interface:AmqpTemplate
Receive a message if there is one from a specific queue and convert it to a Java object. Wait up to the specified wait time if necessary for a message to become available.- Specified by:
receiveAndConvert
in interfaceAmqpTemplate
- Parameters:
queueName
- the name of the queue to polltimeoutMillis
- how long to wait before giving up. Zero value means the method will returnnull
immediately if there is no message available. Negative value makes method wait for a message indefinitely.- Returns:
- a message or null if the time expires
- Throws:
AmqpException
- if there is a problem
-
receiveAndConvert
Description copied from interface:AmqpTemplate
Receive a message if there is one from a default queue and convert it to a Java object. Returns immediately, possibly with a null value. Requires aSmartMessageConverter
.- Specified by:
receiveAndConvert
in interfaceAmqpTemplate
- Type Parameters:
T
- the type.- Parameters:
type
- the type to convert to.- Returns:
- a message or null if there is none waiting.
- Throws:
AmqpException
- if there is a problem.
-
receiveAndConvert
@Nullable public <T> T receiveAndConvert(String queueName, ParameterizedTypeReference<T> type) throws AmqpException Description copied from interface:AmqpTemplate
Receive a message if there is one from a specific queue and convert it to a Java object. Returns immediately, possibly with a null value. Requires aSmartMessageConverter
.- Specified by:
receiveAndConvert
in interfaceAmqpTemplate
- Type Parameters:
T
- the type.- Parameters:
queueName
- the name of the queue to polltype
- the type to convert to.- Returns:
- a message or null if there is none waiting
- Throws:
AmqpException
- if there is a problem
-
receiveAndConvert
@Nullable public <T> T receiveAndConvert(long timeoutMillis, ParameterizedTypeReference<T> type) throws AmqpException Description copied from interface:AmqpTemplate
Receive a message if there is one from a default queue and convert it to a Java object. Wait up to the specified wait time if necessary for a message to become available. Requires aSmartMessageConverter
.- Specified by:
receiveAndConvert
in interfaceAmqpTemplate
- Type Parameters:
T
- the type.- Parameters:
timeoutMillis
- how long to wait before giving up. Zero value means the method will returnnull
immediately if there is no message available. Negative value makes method wait for a message indefinitely.type
- the type to convert to.- Returns:
- a message or null if the time expires
- Throws:
AmqpException
- if there is a problem
-
receiveAndConvert
@Nullable public <T> T receiveAndConvert(String queueName, long timeoutMillis, ParameterizedTypeReference<T> type) throws AmqpException Description copied from interface:AmqpTemplate
Receive a message if there is one from a specific queue and convert it to a Java object. Wait up to the specified wait time if necessary for a message to become available. Requires aSmartMessageConverter
.- Specified by:
receiveAndConvert
in interfaceAmqpTemplate
- Type Parameters:
T
- the type.- Parameters:
queueName
- the name of the queue to polltimeoutMillis
- how long to wait before giving up. Zero value means the method will returnnull
immediately if there is no message available. Negative value makes method wait for a message indefinitely.type
- the type to convert to.- Returns:
- a message or null if the time expires
- Throws:
AmqpException
- if there is a problem
-
receiveAndReply
Description copied from interface:AmqpTemplate
Receive a message if there is one from a default queue, invoke providedReceiveAndReplyCallback
and send reply message, if thecallback
returns one, to thereplyTo
Address
fromMessageProperties
or to default exchange and default routingKey.- Specified by:
receiveAndReply
in interfaceAmqpTemplate
- Type Parameters:
R
- The type of the request after conversion from theMessage
.S
- The type of the response.- Parameters:
callback
- a user-providedReceiveAndReplyCallback
implementation to process received message and return a reply message.- Returns:
true
, if message was received- Throws:
AmqpException
- if there is a problem
-
receiveAndReply
public <R,S> boolean receiveAndReply(String queueName, ReceiveAndReplyCallback<R, S> callback) throws AmqpExceptionDescription copied from interface:AmqpTemplate
Receive a message if there is one from provided queue, invoke providedReceiveAndReplyCallback
and send reply message, if thecallback
returns one, to thereplyTo
Address
fromMessageProperties
or to default exchange and default routingKey.- Specified by:
receiveAndReply
in interfaceAmqpTemplate
- Type Parameters:
R
- The type of the request after conversion from theMessage
.S
- The type of the response.- Parameters:
queueName
- the queue name to receive a message.callback
- a user-providedReceiveAndReplyCallback
implementation to process received message and return a reply message.- Returns:
true
, if message was received.- Throws:
AmqpException
- if there is a problem.
-
receiveAndReply
public <R,S> boolean receiveAndReply(ReceiveAndReplyCallback<R, S> callback, String exchange, String routingKey) throws AmqpExceptionDescription copied from interface:AmqpTemplate
Receive a message if there is one from default queue, invoke providedReceiveAndReplyCallback
and send reply message, if thecallback
returns one, to the providedexchange
androutingKey
.- Specified by:
receiveAndReply
in interfaceAmqpTemplate
- Type Parameters:
R
- The type of the request after conversion from theMessage
.S
- The type of the response.- Parameters:
callback
- a user-providedReceiveAndReplyCallback
implementation to process received message and return a reply message.exchange
- the exchange name to send reply message.routingKey
- the routing key to send reply message.- Returns:
true
, if message was received.- Throws:
AmqpException
- if there is a problem.
-
receiveAndReply
public <R,S> boolean receiveAndReply(String queueName, ReceiveAndReplyCallback<R, S> callback, String replyExchange, String replyRoutingKey) throws AmqpExceptionDescription copied from interface:AmqpTemplate
Receive a message if there is one from provided queue, invoke providedReceiveAndReplyCallback
and send reply message, if thecallback
returns one, to the providedexchange
androutingKey
.- Specified by:
receiveAndReply
in interfaceAmqpTemplate
- Type Parameters:
R
- The type of the request after conversion from theMessage
.S
- The type of the response.- Parameters:
queueName
- the queue name to receive a message.callback
- a user-providedReceiveAndReplyCallback
implementation to process received message and return a reply message.replyExchange
- the exchange name to send reply message.replyRoutingKey
- the routing key to send reply message.- Returns:
true
, if message was received- Throws:
AmqpException
- if there is a problem
-
receiveAndReply
public <R,S> boolean receiveAndReply(ReceiveAndReplyCallback<R, S> callback, ReplyToAddressCallback<S> replyToAddressCallback) throws AmqpExceptionDescription copied from interface:AmqpTemplate
Receive a message if there is one from a default queue, invoke providedReceiveAndReplyCallback
and send reply message, if thecallback
returns one, to thereplyTo
Address
from result ofReplyToAddressCallback
.- Specified by:
receiveAndReply
in interfaceAmqpTemplate
- Type Parameters:
R
- The type of the request after conversion from theMessage
.S
- The type of the response.- Parameters:
callback
- a user-providedReceiveAndReplyCallback
implementation to process received message and return a reply message.replyToAddressCallback
- the callback to determine replyTo address at runtime.- Returns:
true
, if message was received.- Throws:
AmqpException
- if there is a problem.
-
receiveAndReply
public <R,S> boolean receiveAndReply(String queueName, ReceiveAndReplyCallback<R, S> callback, ReplyToAddressCallback<S> replyToAddressCallback) throws AmqpExceptionDescription copied from interface:AmqpTemplate
Receive a message if there is one from provided queue, invoke providedReceiveAndReplyCallback
and send reply message, if thecallback
returns one, to thereplyTo
Address
from result ofReplyToAddressCallback
.- Specified by:
receiveAndReply
in interfaceAmqpTemplate
- Type Parameters:
R
- The type of the request after conversion from theMessage
.S
- The type of the response.- Parameters:
queueName
- the queue name to receive a message.callback
- a user-providedReceiveAndReplyCallback
implementation to process received message and return a reply message.replyToAddressCallback
- the callback to determine replyTo address at runtime.- Returns:
true
, if message was received- Throws:
AmqpException
- if there is a problem
-
logReceived
Log a received message. The default implementation logs the full message at DEBUG level. Override this method to change that behavior.- Parameters:
prefix
- a prefix, e.g. "Received: " or "Reply: ".message
- the message.- Since:
- 2.4.6
-
sendAndReceive
Description copied from interface:AmqpTemplate
Basic RPC pattern. Send a message to a default exchange with a default routing key and attempt to receive a response. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.- Specified by:
sendAndReceive
in interfaceAmqpTemplate
- Parameters:
message
- a message to send.- Returns:
- the response; or null if the reply times out.
- Throws:
AmqpException
- if there is a problem.
-
sendAndReceive
@Nullable public Message sendAndReceive(Message message, @Nullable CorrelationData correlationData) throws AmqpException - Throws:
AmqpException
-
sendAndReceive
Description copied from interface:AmqpTemplate
Basic RPC pattern. Send a message to a default exchange with a specific routing key and attempt to receive a response. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.- Specified by:
sendAndReceive
in interfaceAmqpTemplate
- Parameters:
routingKey
- the routing key.message
- a message to send.- Returns:
- the response; or null if the reply times out.
- Throws:
AmqpException
- if there is a problem.
-
sendAndReceive
@Nullable public Message sendAndReceive(String routingKey, Message message, @Nullable CorrelationData correlationData) throws AmqpException - Throws:
AmqpException
-
sendAndReceive
@Nullable public Message sendAndReceive(String exchange, String routingKey, Message message) throws AmqpException Description copied from interface:AmqpTemplate
Basic RPC pattern. Send a message to a specific exchange with a specific routing key and attempt to receive a response. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.- Specified by:
sendAndReceive
in interfaceAmqpTemplate
- Parameters:
exchange
- the name of the exchange.routingKey
- the routing key.message
- a message to send.- Returns:
- the response; or null if the reply times out.
- Throws:
AmqpException
- if there is a problem.
-
sendAndReceive
@Nullable public Message sendAndReceive(String exchange, String routingKey, Message message, @Nullable CorrelationData correlationData) throws AmqpException - Throws:
AmqpException
-
convertSendAndReceive
Description copied from interface:AmqpTemplate
Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a default routing key and attempt to receive a response, converting that to a Java object. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.- Specified by:
convertSendAndReceive
in interfaceAmqpTemplate
- Parameters:
message
- a message to send.- Returns:
- the response; or null if the reply times out.
- Throws:
AmqpException
- if there is a problem.
-
convertSendAndReceive
@Nullable public Object convertSendAndReceive(Object message, @Nullable CorrelationData correlationData) throws AmqpException Description copied from interface:RabbitOperations
Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a default routing key and attempt to receive a response, converting that to a Java object. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.- Specified by:
convertSendAndReceive
in interfaceRabbitOperations
- Parameters:
message
- a message to send.correlationData
- data to correlate publisher confirms.- Returns:
- the response if there is one
- Throws:
AmqpException
- if there is a problem
-
convertSendAndReceive
@Nullable public Object convertSendAndReceive(String routingKey, Object message) throws AmqpException Description copied from interface:AmqpTemplate
Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a specific routing key and attempt to receive a response, converting that to a Java object. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.- Specified by:
convertSendAndReceive
in interfaceAmqpTemplate
- Parameters:
routingKey
- the routing key.message
- a message to send.- Returns:
- the response; or null if the reply times out.
- Throws:
AmqpException
- if there is a problem.
-
convertSendAndReceive
@Nullable public Object convertSendAndReceive(String routingKey, Object message, @Nullable CorrelationData correlationData) throws AmqpException Description copied from interface:RabbitOperations
Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a specific routing key and attempt to receive a response, converting that to a Java object. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.- Specified by:
convertSendAndReceive
in interfaceRabbitOperations
- Parameters:
routingKey
- the routing keymessage
- a message to sendcorrelationData
- data to correlate publisher confirms.- Returns:
- the response if there is one
- Throws:
AmqpException
- if there is a problem
-
convertSendAndReceive
@Nullable public Object convertSendAndReceive(String exchange, String routingKey, Object message) throws AmqpException Description copied from interface:AmqpTemplate
Basic RPC pattern with conversion. Send a Java object converted to a message to a specific exchange with a specific routing key and attempt to receive a response, converting that to a Java object. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.- Specified by:
convertSendAndReceive
in interfaceAmqpTemplate
- Parameters:
exchange
- the name of the exchange.routingKey
- the routing key.message
- a message to send.- Returns:
- the response; or null if the reply times out.
- Throws:
AmqpException
- if there is a problem.
-
convertSendAndReceive
@Nullable public Object convertSendAndReceive(String exchange, String routingKey, Object message, @Nullable CorrelationData correlationData) throws AmqpException Description copied from interface:RabbitOperations
Basic RPC pattern with conversion. Send a Java object converted to a message to a specific exchange with a specific routing key and attempt to receive a response, converting that to a Java object. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.- Specified by:
convertSendAndReceive
in interfaceRabbitOperations
- Parameters:
exchange
- the name of the exchangeroutingKey
- the routing keymessage
- a message to sendcorrelationData
- data to correlate publisher confirms.- Returns:
- the response if there is one
- Throws:
AmqpException
- if there is a problem
-
convertSendAndReceive
@Nullable public Object convertSendAndReceive(Object message, MessagePostProcessor messagePostProcessor) throws AmqpException Description copied from interface:AmqpTemplate
Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a default routing key and attempt to receive a response, converting that to a Java object. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.- Specified by:
convertSendAndReceive
in interfaceAmqpTemplate
- Parameters:
message
- a message to send.messagePostProcessor
- a processor to apply to the message before it is sent.- Returns:
- the response; or null if the reply times out.
- Throws:
AmqpException
- if there is a problem.
-
convertSendAndReceive
@Nullable public Object convertSendAndReceive(Object message, MessagePostProcessor messagePostProcessor, @Nullable CorrelationData correlationData) throws AmqpException Description copied from interface:RabbitOperations
Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a default routing key and attempt to receive a response, converting that to a Java object. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.- Specified by:
convertSendAndReceive
in interfaceRabbitOperations
- Parameters:
message
- a message to sendmessagePostProcessor
- a processor to apply to the message before it is sentcorrelationData
- data to correlate publisher confirms.- Returns:
- the response if there is one
- Throws:
AmqpException
- if there is a problem
-
convertSendAndReceive
@Nullable public Object convertSendAndReceive(String routingKey, Object message, MessagePostProcessor messagePostProcessor) throws AmqpException Description copied from interface:AmqpTemplate
Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a specific routing key and attempt to receive a response, converting that to a Java object. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.- Specified by:
convertSendAndReceive
in interfaceAmqpTemplate
- Parameters:
routingKey
- the routing key.message
- a message to send.messagePostProcessor
- a processor to apply to the message before it is sent.- Returns:
- the response; or null if the reply times out.
- Throws:
AmqpException
- if there is a problem.
-
convertSendAndReceive
@Nullable public Object convertSendAndReceive(String routingKey, Object message, MessagePostProcessor messagePostProcessor, @Nullable CorrelationData correlationData) throws AmqpException Description copied from interface:RabbitOperations
Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a specific routing key and attempt to receive a response, converting that to a Java object. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.- Specified by:
convertSendAndReceive
in interfaceRabbitOperations
- Parameters:
routingKey
- the routing keymessage
- a message to sendmessagePostProcessor
- a processor to apply to the message before it is sentcorrelationData
- data to correlate publisher confirms.- Returns:
- the response if there is one
- Throws:
AmqpException
- if there is a problem
-
convertSendAndReceive
@Nullable public Object convertSendAndReceive(String exchange, String routingKey, Object message, MessagePostProcessor messagePostProcessor) throws AmqpException Description copied from interface:AmqpTemplate
Basic RPC pattern with conversion. Send a Java object converted to a message to a specific exchange with a specific routing key and attempt to receive a response, converting that to a Java object. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.- Specified by:
convertSendAndReceive
in interfaceAmqpTemplate
- Parameters:
exchange
- the name of the exchange.routingKey
- the routing key.message
- a message to send.messagePostProcessor
- a processor to apply to the message before it is sent.- Returns:
- the response; or null if the reply times out.
- Throws:
AmqpException
- if there is a problem.
-
convertSendAndReceive
@Nullable public Object convertSendAndReceive(String exchange, String routingKey, Object message, @Nullable MessagePostProcessor messagePostProcessor, @Nullable CorrelationData correlationData) throws AmqpException Description copied from interface:RabbitOperations
Basic RPC pattern with conversion. Send a Java object converted to a message to a specific exchange with a specific routing key and attempt to receive a response, converting that to a Java object. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.- Specified by:
convertSendAndReceive
in interfaceRabbitOperations
- Parameters:
exchange
- the name of the exchangeroutingKey
- the routing keymessage
- a message to sendmessagePostProcessor
- a processor to apply to the message before it is sentcorrelationData
- data to correlate publisher confirms.- Returns:
- the response if there is one
- Throws:
AmqpException
- if there is a problem
-
convertSendAndReceiveAsType
@Nullable public <T> T convertSendAndReceiveAsType(Object message, ParameterizedTypeReference<T> responseType) throws AmqpException Description copied from interface:AmqpTemplate
Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a default routing key and attempt to receive a response, converting that to a Java object. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout. Requires aSmartMessageConverter
.- Specified by:
convertSendAndReceiveAsType
in interfaceAmqpTemplate
- Type Parameters:
T
- the type.- Parameters:
message
- a message to send.responseType
- the type to convert the reply to.- Returns:
- the response; or null if the reply times out.
- Throws:
AmqpException
- if there is a problem.
-
convertSendAndReceiveAsType
@Nullable public <T> T convertSendAndReceiveAsType(Object message, @Nullable CorrelationData correlationData, ParameterizedTypeReference<T> responseType) throws AmqpException Description copied from interface:RabbitOperations
Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a default routing key and attempt to receive a response, converting that to a Java object. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout. Requires aSmartMessageConverter
.- Specified by:
convertSendAndReceiveAsType
in interfaceRabbitOperations
- Type Parameters:
T
- the type.- Parameters:
message
- a message to send.correlationData
- data to correlate publisher confirms.responseType
- the type to convert the reply to.- Returns:
- the response if there is one.
- Throws:
AmqpException
- if there is a problem.
-
convertSendAndReceiveAsType
@Nullable public <T> T convertSendAndReceiveAsType(String routingKey, Object message, ParameterizedTypeReference<T> responseType) throws AmqpException Description copied from interface:AmqpTemplate
Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a specific routing key and attempt to receive a response, converting that to a Java object. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout. Requires aSmartMessageConverter
.- Specified by:
convertSendAndReceiveAsType
in interfaceAmqpTemplate
- Type Parameters:
T
- the type.- Parameters:
routingKey
- the routing key.message
- a message to send.responseType
- the type to convert the reply to.- Returns:
- the response; or null if the reply times out.
- Throws:
AmqpException
- if there is a problem.
-
convertSendAndReceiveAsType
@Nullable public <T> T convertSendAndReceiveAsType(String routingKey, Object message, @Nullable CorrelationData correlationData, ParameterizedTypeReference<T> responseType) throws AmqpException Description copied from interface:RabbitOperations
Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a specific routing key and attempt to receive a response, converting that to a Java object. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout. Requires aSmartMessageConverter
.- Specified by:
convertSendAndReceiveAsType
in interfaceRabbitOperations
- Type Parameters:
T
- the type.- Parameters:
routingKey
- the routing keymessage
- a message to sendcorrelationData
- data to correlate publisher confirms.responseType
- the type to convert the reply to.- Returns:
- the response if there is one
- Throws:
AmqpException
- if there is a problem
-
convertSendAndReceiveAsType
@Nullable public <T> T convertSendAndReceiveAsType(String exchange, String routingKey, Object message, ParameterizedTypeReference<T> responseType) throws AmqpException Description copied from interface:AmqpTemplate
Basic RPC pattern with conversion. Send a Java object converted to a message to a specific exchange with a specific routing key and attempt to receive a response, converting that to a Java object. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout. Requires aSmartMessageConverter
.- Specified by:
convertSendAndReceiveAsType
in interfaceAmqpTemplate
- Type Parameters:
T
- the type.- Parameters:
exchange
- the name of the exchange.routingKey
- the routing key.message
- a message to send.responseType
- the type to convert the reply to.- Returns:
- the response; or null if the reply times out.
- Throws:
AmqpException
- if there is a problem.
-
convertSendAndReceiveAsType
@Nullable public <T> T convertSendAndReceiveAsType(Object message, @Nullable MessagePostProcessor messagePostProcessor, ParameterizedTypeReference<T> responseType) throws AmqpException Description copied from interface:AmqpTemplate
Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a default routing key and attempt to receive a response, converting that to a Java object. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout. Requires aSmartMessageConverter
.- Specified by:
convertSendAndReceiveAsType
in interfaceAmqpTemplate
- Type Parameters:
T
- the type.- Parameters:
message
- a message to send.messagePostProcessor
- a processor to apply to the message before it is sent.responseType
- the type to convert the reply to.- Returns:
- the response; or null if the reply times out.
- Throws:
AmqpException
- if there is a problem.
-
convertSendAndReceiveAsType
@Nullable public <T> T convertSendAndReceiveAsType(Object message, @Nullable MessagePostProcessor messagePostProcessor, @Nullable CorrelationData correlationData, ParameterizedTypeReference<T> responseType) throws AmqpException Description copied from interface:RabbitOperations
Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a default routing key and attempt to receive a response, converting that to a Java object. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout. Requires aSmartMessageConverter
.- Specified by:
convertSendAndReceiveAsType
in interfaceRabbitOperations
- Type Parameters:
T
- the type.- Parameters:
message
- a message to sendmessagePostProcessor
- a processor to apply to the message before it is sentcorrelationData
- data to correlate publisher confirms.responseType
- the type to convert the reply to.- Returns:
- the response if there is one
- Throws:
AmqpException
- if there is a problem
-
convertSendAndReceiveAsType
@Nullable public <T> T convertSendAndReceiveAsType(String routingKey, Object message, @Nullable MessagePostProcessor messagePostProcessor, ParameterizedTypeReference<T> responseType) throws AmqpException Description copied from interface:AmqpTemplate
Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a specific routing key and attempt to receive a response, converting that to a Java object. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout. Requires aSmartMessageConverter
.- Specified by:
convertSendAndReceiveAsType
in interfaceAmqpTemplate
- Type Parameters:
T
- the type.- Parameters:
routingKey
- the routing key.message
- a message to send.messagePostProcessor
- a processor to apply to the message before it is sent.responseType
- the type to convert the reply to.- Returns:
- the response; or null if the reply times out.
- Throws:
AmqpException
- if there is a problem.
-
convertSendAndReceiveAsType
@Nullable public <T> T convertSendAndReceiveAsType(String routingKey, Object message, @Nullable MessagePostProcessor messagePostProcessor, @Nullable CorrelationData correlationData, ParameterizedTypeReference<T> responseType) throws AmqpException Description copied from interface:RabbitOperations
Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a specific routing key and attempt to receive a response, converting that to a Java object. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout. Requires aSmartMessageConverter
.- Specified by:
convertSendAndReceiveAsType
in interfaceRabbitOperations
- Type Parameters:
T
- the type.- Parameters:
routingKey
- the routing keymessage
- a message to sendmessagePostProcessor
- a processor to apply to the message before it is sentcorrelationData
- data to correlate publisher confirms.responseType
- the type to convert the reply to.- Returns:
- the response if there is one
- Throws:
AmqpException
- if there is a problem
-
convertSendAndReceiveAsType
@Nullable public <T> T convertSendAndReceiveAsType(String exchange, String routingKey, Object message, MessagePostProcessor messagePostProcessor, ParameterizedTypeReference<T> responseType) throws AmqpException Description copied from interface:AmqpTemplate
Basic RPC pattern with conversion. Send a Java object converted to a message to a specific exchange with a specific routing key and attempt to receive a response, converting that to a Java object. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout. Requires aSmartMessageConverter
.- Specified by:
convertSendAndReceiveAsType
in interfaceAmqpTemplate
- Type Parameters:
T
- the type.- Parameters:
exchange
- the name of the exchange.routingKey
- the routing key.message
- a message to send.messagePostProcessor
- a processor to apply to the message before it is sent.responseType
- the type to convert the reply to.- Returns:
- the response; or null if the reply times out.
- Throws:
AmqpException
- if there is a problem.
-
convertSendAndReceiveAsType
@Nullable public <T> T convertSendAndReceiveAsType(String exchange, String routingKey, Object message, @Nullable MessagePostProcessor messagePostProcessor, @Nullable CorrelationData correlationData, ParameterizedTypeReference<T> responseType) throws AmqpException Description copied from interface:RabbitOperations
Basic RPC pattern with conversion. Send a Java object converted to a message to a specific exchange with a specific routing key and attempt to receive a response, converting that to a Java object. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout. Requires aSmartMessageConverter
.- Specified by:
convertSendAndReceiveAsType
in interfaceRabbitOperations
- Type Parameters:
T
- the type.- Parameters:
exchange
- the name of the exchangeroutingKey
- the routing keymessage
- a message to sendmessagePostProcessor
- a processor to apply to the message before it is sentcorrelationData
- data to correlate publisher confirms.responseType
- the type to convert the reply to.- Returns:
- the response if there is one
- Throws:
AmqpException
- if there is a problem
-
convertSendAndReceiveRaw
@Nullable protected Message convertSendAndReceiveRaw(String exchange, String routingKey, Object message, @Nullable MessagePostProcessor messagePostProcessor, @Nullable CorrelationData correlationData) Convert and send a message and return the raw reply message, or null. Subclasses can invoke this method if they want to perform conversion on the outbound message but have direct access to the reply message before conversion.- Parameters:
exchange
- the exchange.routingKey
- the routing key.message
- the data to send.messagePostProcessor
- a message post processor (can be null).correlationData
- correlation data (can be null).- Returns:
- the reply message or null if a timeout occurs.
- Since:
- 1.6.6
-
convertMessageIfNecessary
-
doSendAndReceive
@Nullable protected Message doSendAndReceive(String exchange, String routingKey, Message message, @Nullable CorrelationData correlationData) Send a message and wait for a reply.- Parameters:
exchange
- the exchange nameroutingKey
- the routing keymessage
- the message to sendcorrelationData
- the correlation data for confirms- Returns:
- the message that is received in reply
-
doSendAndReceiveWithTemporary
-
doSendAndReceiveWithFixed
-
replyTimedOut
Subclasses can implement this to be notified that a reply has timed out.- Parameters:
correlationId
- the correlationId- Since:
- 2.1.2
-
isMandatoryFor
Return whether the provided message should be sent with the mandatory flag set.- Parameters:
message
- the message.- Returns:
- true for mandatory.
- Since:
- 2.0
-
execute
Description copied from interface:RabbitOperations
Execute the callback with a channel and reliably close the channel afterwards.- Specified by:
execute
in interfaceRabbitOperations
- Type Parameters:
T
- the return type.- Parameters:
action
- the call back.- Returns:
- the result from the
ChannelCallback.doInRabbit(com.rabbitmq.client.Channel)
.
-
invoke
@Nullable public <T> T invoke(RabbitOperations.OperationsCallback<T> action, @Nullable com.rabbitmq.client.ConfirmCallback acks, @Nullable com.rabbitmq.client.ConfirmCallback nacks) Description copied from interface:RabbitOperations
Invoke operations on the same channel. If callbacks are needed, both callbacks must be supplied.- Specified by:
invoke
in interfaceRabbitOperations
- Type Parameters:
T
- the return type.- Parameters:
action
- the callback.acks
- a confirm callback for acks.nacks
- a confirm callback for nacks.- Returns:
- the result of the action method.
-
waitForConfirms
public boolean waitForConfirms(long timeout) Description copied from interface:RabbitOperations
Delegate to the underlying dedicated channel to wait for confirms. The connection factory must be configured for publisher confirms and this method must be called within the scope of anRabbitOperations.invoke(OperationsCallback)
operation. RequiresCachingConnectionFactory#setPublisherConfirms(true)
.- Specified by:
waitForConfirms
in interfaceRabbitOperations
- Parameters:
timeout
- the timeout- Returns:
- true if acks and no nacks are received.
- See Also:
-
Channel.waitForConfirms(long)
-
waitForConfirmsOrDie
public void waitForConfirmsOrDie(long timeout) Description copied from interface:RabbitOperations
Delegate to the underlying dedicated channel to wait for confirms. The connection factory must be configured for publisher confirms and this method must be called within the scope of anRabbitOperations.invoke(OperationsCallback)
operation. RequiresCachingConnectionFactory#setPublisherConfirms(true)
.- Specified by:
waitForConfirmsOrDie
in interfaceRabbitOperations
- Parameters:
timeout
- the timeout- See Also:
-
Channel.waitForConfirmsOrDie(long)
-
doSend
public void doSend(com.rabbitmq.client.Channel channel, String exchangeArg, String routingKeyArg, Message message, boolean mandatory, @Nullable CorrelationData correlationData) Send the given message to the specified exchange.- Parameters:
channel
- The RabbitMQ Channel to operate within.exchangeArg
- The name of the RabbitMQ exchange to send to.routingKeyArg
- The routing key.message
- The Message to send.mandatory
- The mandatory flag.correlationData
- The correlation data.
-
observeTheSend
-
nullSafeExchange
Return the exchange or the default exchange if null.- Parameters:
exchange
- the exchange.- Returns:
- the result.
- Since:
- 2.3.4
-
nullSafeRoutingKey
Return the routing key or the default routing key if null.- Parameters:
rk
- the routing key.- Returns:
- the result.
- Since:
- 2.3.4
-
sendToRabbit
-
isChannelLocallyTransacted
protected boolean isChannelLocallyTransacted(com.rabbitmq.client.Channel channel) Check whether the given Channel is locally transacted, that is, whether its transaction is managed by this template's Channel handling and not by an external transaction coordinator.- Parameters:
channel
- the Channel to check- Returns:
- whether the given Channel is locally transacted
- See Also:
-
addListener
public void addListener(com.rabbitmq.client.Channel channel) Add this template as a confirms listener for the provided channel.- Parameters:
channel
- the channel.- Since:
- 2.0
-
handleConfirm
Description copied from interface:PublisherCallbackChannel.Listener
Invoked by the channel when a confirm is received.- Specified by:
handleConfirm
in interfacePublisherCallbackChannel.Listener
- Parameters:
pendingConfirm
- The pending confirmation, containing correlation data.ack
- true when 'ack', false when 'nack'.
-
handleReturn
public void handleReturn(com.rabbitmq.client.Return returned) Description copied from interface:PublisherCallbackChannel.Listener
Handle a returned message.- Specified by:
handleReturn
in interfacePublisherCallbackChannel.Listener
- Parameters:
returned
- the message and metadata.
-
isConfirmListener
public boolean isConfirmListener()- Specified by:
isConfirmListener
in interfacePublisherCallbackChannel.Listener
-
isReturnListener
public boolean isReturnListener()- Specified by:
isReturnListener
in interfacePublisherCallbackChannel.Listener
-
revoke
public void revoke(com.rabbitmq.client.Channel channel) Description copied from interface:PublisherCallbackChannel.Listener
When called, this listener should remove all references to the channel - it will no longer be invoked by the channel.- Specified by:
revoke
in interfacePublisherCallbackChannel.Listener
- Parameters:
channel
- The channel.
-
getUUID
Description copied from interface:PublisherCallbackChannel.Listener
Returns the UUID used to identify this Listener for returns.- Specified by:
getUUID
in interfacePublisherCallbackChannel.Listener
- Returns:
- A string representation of the UUID.
-
onMessage
Description copied from interface:ChannelAwareMessageListener
Callback for processing a received Rabbit message.Implementors are supposed to process the given Message, typically sending reply messages through the given Session.
- Specified by:
onMessage
in interfaceChannelAwareMessageListener
- Parameters:
message
- the received AMQP message (nevernull
)channel
- the underlying Rabbit Channel (nevernull
unless called by the stream listener container).
-