|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.springframework.amqp.rabbit.connection.RabbitAccessor
org.springframework.amqp.rabbit.core.RabbitTemplate
public class RabbitTemplate
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.
| Nested Class Summary | |
|---|---|
static interface |
RabbitTemplate.ConfirmCallback
|
static interface |
RabbitTemplate.ReturnCallback
|
| Field Summary |
|---|
| Fields inherited from class org.springframework.amqp.rabbit.connection.RabbitAccessor |
|---|
logger |
| Constructor Summary | |
|---|---|
RabbitTemplate()
Convenient constructor for use with setter injection. |
|
RabbitTemplate(ConnectionFactory connectionFactory)
Create a rabbit template with default strategies and settings. |
|
| Method Summary | ||
|---|---|---|
void |
convertAndSend(java.lang.Object object)
|
|
void |
convertAndSend(java.lang.Object message,
org.springframework.amqp.core.MessagePostProcessor messagePostProcessor)
|
|
void |
convertAndSend(java.lang.String routingKey,
java.lang.Object object)
|
|
void |
convertAndSend(java.lang.String routingKey,
java.lang.Object object,
CorrelationData correlationData)
|
|
void |
convertAndSend(java.lang.String routingKey,
java.lang.Object message,
org.springframework.amqp.core.MessagePostProcessor messagePostProcessor)
|
|
void |
convertAndSend(java.lang.String routingKey,
java.lang.Object message,
org.springframework.amqp.core.MessagePostProcessor messagePostProcessor,
CorrelationData correlationData)
|
|
void |
convertAndSend(java.lang.String exchange,
java.lang.String routingKey,
java.lang.Object object)
|
|
void |
convertAndSend(java.lang.String exchange,
java.lang.String routingKey,
java.lang.Object object,
CorrelationData corrationData)
|
|
void |
convertAndSend(java.lang.String exchange,
java.lang.String routingKey,
java.lang.Object message,
org.springframework.amqp.core.MessagePostProcessor messagePostProcessor)
|
|
void |
convertAndSend(java.lang.String exchange,
java.lang.String routingKey,
java.lang.Object message,
org.springframework.amqp.core.MessagePostProcessor messagePostProcessor,
CorrelationData correlationData)
|
|
protected org.springframework.amqp.core.Message |
convertMessageIfNecessary(java.lang.Object object)
|
|
java.lang.Object |
convertSendAndReceive(java.lang.Object message)
|
|
java.lang.Object |
convertSendAndReceive(java.lang.Object message,
org.springframework.amqp.core.MessagePostProcessor messagePostProcessor)
|
|
java.lang.Object |
convertSendAndReceive(java.lang.String routingKey,
java.lang.Object message)
|
|
java.lang.Object |
convertSendAndReceive(java.lang.String routingKey,
java.lang.Object message,
org.springframework.amqp.core.MessagePostProcessor messagePostProcessor)
|
|
java.lang.Object |
convertSendAndReceive(java.lang.String exchange,
java.lang.String routingKey,
java.lang.Object message)
|
|
java.lang.Object |
convertSendAndReceive(java.lang.String exchange,
java.lang.String routingKey,
java.lang.Object message,
org.springframework.amqp.core.MessagePostProcessor messagePostProcessor)
|
|
void |
correlationconvertAndSend(java.lang.Object object,
CorrelationData correlationData)
|
|
protected void |
doSend(com.rabbitmq.client.Channel channel,
java.lang.String exchange,
java.lang.String routingKey,
org.springframework.amqp.core.Message message,
CorrelationData correlationData)
Send the given message to the specified exchange. |
|
protected org.springframework.amqp.core.Message |
doSendAndReceive(java.lang.String exchange,
java.lang.String routingKey,
org.springframework.amqp.core.Message message)
Send a message and wait for a reply. |
|
protected org.springframework.amqp.core.Message |
doSendAndReceiveWithFixed(java.lang.String exchange,
java.lang.String routingKey,
org.springframework.amqp.core.Message message)
|
|
protected org.springframework.amqp.core.Message |
doSendAndReceiveWithTemporary(java.lang.String exchange,
java.lang.String routingKey,
org.springframework.amqp.core.Message message)
|
|
|
execute(ChannelCallback<T> action)
|
|
org.springframework.amqp.support.converter.MessageConverter |
getMessageConverter()
Return the message converter for this template. |
|
java.util.Collection<CorrelationData> |
getUnconfirmed(long age)
Gets unconfirmed correlatiom data older than age and removes them. |
|
java.lang.String |
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(int replyCode,
java.lang.String replyText,
java.lang.String exchange,
java.lang.String routingKey,
com.rabbitmq.client.AMQP.BasicProperties properties,
byte[] body)
|
|
protected void |
initDefaultStrategies()
Set up the default strategies. |
|
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 |
isConfirmListener()
|
|
boolean |
isReturnListener()
|
|
void |
onMessage(org.springframework.amqp.core.Message message)
|
|
org.springframework.amqp.core.Message |
receive()
|
|
org.springframework.amqp.core.Message |
receive(java.lang.String queueName)
|
|
java.lang.Object |
receiveAndConvert()
|
|
java.lang.Object |
receiveAndConvert(java.lang.String queueName)
|
|
void |
removePendingConfirmsReference(com.rabbitmq.client.Channel channel,
java.util.SortedMap<java.lang.Long,PendingConfirm> unconfirmed)
When called, this listener must remove all references to the pending confirm map. |
|
void |
send(org.springframework.amqp.core.Message message)
|
|
void |
send(java.lang.String routingKey,
org.springframework.amqp.core.Message message)
|
|
void |
send(java.lang.String exchange,
java.lang.String routingKey,
org.springframework.amqp.core.Message message)
|
|
void |
send(java.lang.String exchange,
java.lang.String routingKey,
org.springframework.amqp.core.Message message,
CorrelationData correlationData)
|
|
org.springframework.amqp.core.Message |
sendAndReceive(org.springframework.amqp.core.Message message)
|
|
org.springframework.amqp.core.Message |
sendAndReceive(java.lang.String routingKey,
org.springframework.amqp.core.Message message)
|
|
org.springframework.amqp.core.Message |
sendAndReceive(java.lang.String exchange,
java.lang.String routingKey,
org.springframework.amqp.core.Message message)
|
|
void |
setConfirmCallback(RabbitTemplate.ConfirmCallback confirmCallback)
|
|
void |
setCorrelationKey(java.lang.String correlationKey)
If set to 'correlationId' (default) the correlationId property will be used; otherwise the supplied key will be used. |
|
void |
setEncoding(java.lang.String encoding)
The encoding to use when inter-converting between byte arrays and Strings in message properties. |
|
void |
setExchange(java.lang.String exchange)
The name of the default exchange to use for send operations when none is specified. |
|
void |
setImmediate(boolean immediate)
Deprecated. - RabbitMQ no longer supports this option when publishing messages. |
|
void |
setMandatory(boolean mandatory)
|
|
void |
setMessageConverter(org.springframework.amqp.support.converter.MessageConverter messageConverter)
Set the message converter for this template. |
|
void |
setMessagePropertiesConverter(MessagePropertiesConverter messagePropertiesConverter)
Set the MessagePropertiesConverter for this template. |
|
void |
setQueue(java.lang.String queue)
The name of the default queue to receive messages from when none is specified explicitly. |
|
void |
setReplyQueue(org.springframework.amqp.core.Queue replyQueue)
A queue for replies; if not provided, a temporary exclusive, auto-delete queue will be used for each reply. |
|
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 |
setReturnCallback(RabbitTemplate.ReturnCallback returnCallback)
|
|
void |
setRoutingKey(java.lang.String routingKey)
The value of a default routing key to use for send operations when none is specified. |
|
| Methods inherited from class org.springframework.amqp.rabbit.connection.RabbitAccessor |
|---|
afterPropertiesSet, convertRabbitAccessException, createConnection, getChannel, getConnection, getConnectionFactory, getTransactionalResourceHolder, isChannelTransacted, setChannelTransacted, setConnectionFactory |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public RabbitTemplate()
public RabbitTemplate(ConnectionFactory connectionFactory)
connectionFactory - the connection factory to use| Method Detail |
|---|
protected void initDefaultStrategies()
public void setExchange(java.lang.String exchange)
""
which is the default exchange in the broker (per the AMQP specification).
exchange - the exchange name to use for send operationspublic void setRoutingKey(java.lang.String routingKey)
routingKey - the default routing key to use for send operationspublic void setQueue(java.lang.String queue)
queue - the default queue name to use for receivepublic void setEncoding(java.lang.String encoding)
encoding - the encoding to setpublic void setReplyQueue(org.springframework.amqp.core.Queue replyQueue)
replyQueue - the replyQueue to setpublic void setReplyTimeout(long replyTimeout)
DEFAULT_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.
replyTimeout - the reply timeout in millisecondssendAndReceive(String, String, Message),
convertSendAndReceive(String, String, Object)public void setMessageConverter(org.springframework.amqp.support.converter.MessageConverter messageConverter)
The default converter is a SimpleMessageConverter, which is able to handle byte arrays, Strings, and Serializable Objects depending on the message content type header.
convertAndSend(java.lang.Object),
receiveAndConvert(),
SimpleMessageConverterpublic void setMessagePropertiesConverter(MessagePropertiesConverter messagePropertiesConverter)
MessagePropertiesConverter 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.
public org.springframework.amqp.support.converter.MessageConverter getMessageConverter()
ChannelCallback implementations.
public void setConfirmCallback(RabbitTemplate.ConfirmCallback confirmCallback)
public void setReturnCallback(RabbitTemplate.ReturnCallback returnCallback)
public void setMandatory(boolean mandatory)
@Deprecated public void setImmediate(boolean immediate)
public void setCorrelationKey(java.lang.String correlationKey)
correlationKey - the correlationKey to setpublic java.util.Collection<CorrelationData> getUnconfirmed(long age)
age - in millseconds
public void send(org.springframework.amqp.core.Message message)
throws org.springframework.amqp.AmqpException
send in interface org.springframework.amqp.core.AmqpTemplateorg.springframework.amqp.AmqpException
public void send(java.lang.String routingKey,
org.springframework.amqp.core.Message message)
throws org.springframework.amqp.AmqpException
send in interface org.springframework.amqp.core.AmqpTemplateorg.springframework.amqp.AmqpException
public void send(java.lang.String exchange,
java.lang.String routingKey,
org.springframework.amqp.core.Message message)
throws org.springframework.amqp.AmqpException
send in interface org.springframework.amqp.core.AmqpTemplateorg.springframework.amqp.AmqpException
public void send(java.lang.String exchange,
java.lang.String routingKey,
org.springframework.amqp.core.Message message,
CorrelationData correlationData)
throws org.springframework.amqp.AmqpException
org.springframework.amqp.AmqpException
public void convertAndSend(java.lang.Object object)
throws org.springframework.amqp.AmqpException
convertAndSend in interface org.springframework.amqp.core.AmqpTemplateorg.springframework.amqp.AmqpException
public void correlationconvertAndSend(java.lang.Object object,
CorrelationData correlationData)
throws org.springframework.amqp.AmqpException
org.springframework.amqp.AmqpException
public void convertAndSend(java.lang.String routingKey,
java.lang.Object object)
throws org.springframework.amqp.AmqpException
convertAndSend in interface org.springframework.amqp.core.AmqpTemplateorg.springframework.amqp.AmqpException
public void convertAndSend(java.lang.String routingKey,
java.lang.Object object,
CorrelationData correlationData)
throws org.springframework.amqp.AmqpException
org.springframework.amqp.AmqpException
public void convertAndSend(java.lang.String exchange,
java.lang.String routingKey,
java.lang.Object object)
throws org.springframework.amqp.AmqpException
convertAndSend in interface org.springframework.amqp.core.AmqpTemplateorg.springframework.amqp.AmqpException
public void convertAndSend(java.lang.String exchange,
java.lang.String routingKey,
java.lang.Object object,
CorrelationData corrationData)
throws org.springframework.amqp.AmqpException
org.springframework.amqp.AmqpException
public void convertAndSend(java.lang.Object message,
org.springframework.amqp.core.MessagePostProcessor messagePostProcessor)
throws org.springframework.amqp.AmqpException
convertAndSend in interface org.springframework.amqp.core.AmqpTemplateorg.springframework.amqp.AmqpException
public void convertAndSend(java.lang.String routingKey,
java.lang.Object message,
org.springframework.amqp.core.MessagePostProcessor messagePostProcessor)
throws org.springframework.amqp.AmqpException
convertAndSend in interface org.springframework.amqp.core.AmqpTemplateorg.springframework.amqp.AmqpException
public void convertAndSend(java.lang.String routingKey,
java.lang.Object message,
org.springframework.amqp.core.MessagePostProcessor messagePostProcessor,
CorrelationData correlationData)
throws org.springframework.amqp.AmqpException
org.springframework.amqp.AmqpException
public void convertAndSend(java.lang.String exchange,
java.lang.String routingKey,
java.lang.Object message,
org.springframework.amqp.core.MessagePostProcessor messagePostProcessor)
throws org.springframework.amqp.AmqpException
convertAndSend in interface org.springframework.amqp.core.AmqpTemplateorg.springframework.amqp.AmqpException
public void convertAndSend(java.lang.String exchange,
java.lang.String routingKey,
java.lang.Object message,
org.springframework.amqp.core.MessagePostProcessor messagePostProcessor,
CorrelationData correlationData)
throws org.springframework.amqp.AmqpException
org.springframework.amqp.AmqpException
public org.springframework.amqp.core.Message receive()
throws org.springframework.amqp.AmqpException
receive in interface org.springframework.amqp.core.AmqpTemplateorg.springframework.amqp.AmqpExceptionpublic org.springframework.amqp.core.Message receive(java.lang.String queueName)
receive in interface org.springframework.amqp.core.AmqpTemplate
public java.lang.Object receiveAndConvert()
throws org.springframework.amqp.AmqpException
receiveAndConvert in interface org.springframework.amqp.core.AmqpTemplateorg.springframework.amqp.AmqpException
public java.lang.Object receiveAndConvert(java.lang.String queueName)
throws org.springframework.amqp.AmqpException
receiveAndConvert in interface org.springframework.amqp.core.AmqpTemplateorg.springframework.amqp.AmqpException
public org.springframework.amqp.core.Message sendAndReceive(org.springframework.amqp.core.Message message)
throws org.springframework.amqp.AmqpException
sendAndReceive in interface org.springframework.amqp.core.AmqpTemplateorg.springframework.amqp.AmqpException
public org.springframework.amqp.core.Message sendAndReceive(java.lang.String routingKey,
org.springframework.amqp.core.Message message)
throws org.springframework.amqp.AmqpException
sendAndReceive in interface org.springframework.amqp.core.AmqpTemplateorg.springframework.amqp.AmqpException
public org.springframework.amqp.core.Message sendAndReceive(java.lang.String exchange,
java.lang.String routingKey,
org.springframework.amqp.core.Message message)
throws org.springframework.amqp.AmqpException
sendAndReceive in interface org.springframework.amqp.core.AmqpTemplateorg.springframework.amqp.AmqpException
public java.lang.Object convertSendAndReceive(java.lang.Object message)
throws org.springframework.amqp.AmqpException
convertSendAndReceive in interface org.springframework.amqp.core.AmqpTemplateorg.springframework.amqp.AmqpException
public java.lang.Object convertSendAndReceive(java.lang.String routingKey,
java.lang.Object message)
throws org.springframework.amqp.AmqpException
convertSendAndReceive in interface org.springframework.amqp.core.AmqpTemplateorg.springframework.amqp.AmqpException
public java.lang.Object convertSendAndReceive(java.lang.String exchange,
java.lang.String routingKey,
java.lang.Object message)
throws org.springframework.amqp.AmqpException
convertSendAndReceive in interface org.springframework.amqp.core.AmqpTemplateorg.springframework.amqp.AmqpException
public java.lang.Object convertSendAndReceive(java.lang.Object message,
org.springframework.amqp.core.MessagePostProcessor messagePostProcessor)
throws org.springframework.amqp.AmqpException
convertSendAndReceive in interface org.springframework.amqp.core.AmqpTemplateorg.springframework.amqp.AmqpException
public java.lang.Object convertSendAndReceive(java.lang.String routingKey,
java.lang.Object message,
org.springframework.amqp.core.MessagePostProcessor messagePostProcessor)
throws org.springframework.amqp.AmqpException
convertSendAndReceive in interface org.springframework.amqp.core.AmqpTemplateorg.springframework.amqp.AmqpException
public java.lang.Object convertSendAndReceive(java.lang.String exchange,
java.lang.String routingKey,
java.lang.Object message,
org.springframework.amqp.core.MessagePostProcessor messagePostProcessor)
throws org.springframework.amqp.AmqpException
convertSendAndReceive in interface org.springframework.amqp.core.AmqpTemplateorg.springframework.amqp.AmqpExceptionprotected org.springframework.amqp.core.Message convertMessageIfNecessary(java.lang.Object object)
protected org.springframework.amqp.core.Message doSendAndReceive(java.lang.String exchange,
java.lang.String routingKey,
org.springframework.amqp.core.Message message)
exchange - the exchange nameroutingKey - the routing keymessage - the message to send
protected org.springframework.amqp.core.Message doSendAndReceiveWithTemporary(java.lang.String exchange,
java.lang.String routingKey,
org.springframework.amqp.core.Message message)
protected org.springframework.amqp.core.Message doSendAndReceiveWithFixed(java.lang.String exchange,
java.lang.String routingKey,
org.springframework.amqp.core.Message message)
public <T> T execute(ChannelCallback<T> action)
execute in interface RabbitOperations
protected void doSend(com.rabbitmq.client.Channel channel,
java.lang.String exchange,
java.lang.String routingKey,
org.springframework.amqp.core.Message message,
CorrelationData correlationData)
throws java.lang.Exception
channel - the RabbitMQ Channel to operate withinexchange - the name of the RabbitMQ exchange to send toroutingKey - the routing keymessage - the Message to send
java.io.IOException - if thrown by RabbitMQ API methods
java.lang.Exceptionprotected boolean isChannelLocallyTransacted(com.rabbitmq.client.Channel channel)
channel - the Channel to check
ConnectionFactoryUtils.isChannelTransactional(com.rabbitmq.client.Channel, org.springframework.amqp.rabbit.connection.ConnectionFactory),
RabbitAccessor.isChannelTransacted()
public void handleConfirm(PendingConfirm pendingConfirm,
boolean ack)
PublisherCallbackChannel.Listener
handleConfirm in interface PublisherCallbackChannel.ListenerpendingConfirm - The pending confirmation, containing
correlation data.ack - true when 'ack', false when 'nack'.
public void handleReturn(int replyCode,
java.lang.String replyText,
java.lang.String exchange,
java.lang.String routingKey,
com.rabbitmq.client.AMQP.BasicProperties properties,
byte[] body)
throws java.io.IOException
handleReturn in interface PublisherCallbackChannel.Listenerjava.io.IOExceptionpublic boolean isConfirmListener()
isConfirmListener in interface PublisherCallbackChannel.Listenerpublic boolean isReturnListener()
isReturnListener in interface PublisherCallbackChannel.Listener
public void removePendingConfirmsReference(com.rabbitmq.client.Channel channel,
java.util.SortedMap<java.lang.Long,PendingConfirm> unconfirmed)
PublisherCallbackChannel.Listener
removePendingConfirmsReference in interface PublisherCallbackChannel.Listenerunconfirmed - The pending confirm map.public java.lang.String getUUID()
PublisherCallbackChannel.Listener
getUUID in interface PublisherCallbackChannel.Listenerpublic void onMessage(org.springframework.amqp.core.Message message)
onMessage in interface org.springframework.amqp.core.MessageListener
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||