|
||||||||||
| 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.
| 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,
MessagePostProcessor messagePostProcessor)
|
|
void |
convertAndSend(java.lang.String routingKey,
java.lang.Object object)
|
|
void |
convertAndSend(java.lang.String routingKey,
java.lang.Object message,
MessagePostProcessor messagePostProcessor)
|
|
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 message,
MessagePostProcessor messagePostProcessor)
|
|
java.lang.Object |
convertSendAndReceive(java.lang.Object message)
|
|
java.lang.Object |
convertSendAndReceive(java.lang.Object message,
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,
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,
MessagePostProcessor messagePostProcessor)
|
|
protected void |
doSend(com.rabbitmq.client.Channel channel,
java.lang.String exchange,
java.lang.String routingKey,
Message message)
Send the given message to the specified exchange. |
|
protected Message |
doSendAndReceive(java.lang.String exchange,
java.lang.String routingKey,
Message message)
Send a message and wait for a reply. |
|
|
execute(ChannelCallback<T> action)
|
|
MessageConverter |
getMessageConverter()
Return the message converter for this template. |
|
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. |
|
Message |
receive()
|
|
Message |
receive(java.lang.String queueName)
|
|
java.lang.Object |
receiveAndConvert()
|
|
java.lang.Object |
receiveAndConvert(java.lang.String queueName)
|
|
void |
send(Message message)
|
|
void |
send(java.lang.String routingKey,
Message message)
|
|
void |
send(java.lang.String exchange,
java.lang.String routingKey,
Message message)
|
|
Message |
sendAndReceive(Message message)
|
|
Message |
sendAndReceive(java.lang.String routingKey,
Message message)
|
|
Message |
sendAndReceive(java.lang.String exchange,
java.lang.String routingKey,
Message message)
|
|
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 |
setMessageConverter(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 |
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 |
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)
exchange - 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 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(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 MessageConverter getMessageConverter()
ChannelCallback implementations.
public void send(Message message)
throws AmqpException
send in interface AmqpTemplateAmqpException
public void send(java.lang.String routingKey,
Message message)
throws AmqpException
send in interface AmqpTemplateAmqpException
public void send(java.lang.String exchange,
java.lang.String routingKey,
Message message)
throws AmqpException
send in interface AmqpTemplateAmqpException
public void convertAndSend(java.lang.Object object)
throws AmqpException
convertAndSend in interface AmqpTemplateAmqpException
public void convertAndSend(java.lang.String routingKey,
java.lang.Object object)
throws AmqpException
convertAndSend in interface AmqpTemplateAmqpException
public void convertAndSend(java.lang.String exchange,
java.lang.String routingKey,
java.lang.Object object)
throws AmqpException
convertAndSend in interface AmqpTemplateAmqpException
public void convertAndSend(java.lang.Object message,
MessagePostProcessor messagePostProcessor)
throws AmqpException
convertAndSend in interface AmqpTemplateAmqpException
public void convertAndSend(java.lang.String routingKey,
java.lang.Object message,
MessagePostProcessor messagePostProcessor)
throws AmqpException
convertAndSend in interface AmqpTemplateAmqpException
public void convertAndSend(java.lang.String exchange,
java.lang.String routingKey,
java.lang.Object message,
MessagePostProcessor messagePostProcessor)
throws AmqpException
convertAndSend in interface AmqpTemplateAmqpException
public Message receive()
throws AmqpException
receive in interface AmqpTemplateAmqpExceptionpublic Message receive(java.lang.String queueName)
receive in interface AmqpTemplate
public java.lang.Object receiveAndConvert()
throws AmqpException
receiveAndConvert in interface AmqpTemplateAmqpException
public java.lang.Object receiveAndConvert(java.lang.String queueName)
throws AmqpException
receiveAndConvert in interface AmqpTemplateAmqpException
public Message sendAndReceive(Message message)
throws AmqpException
sendAndReceive in interface AmqpTemplateAmqpException
public Message sendAndReceive(java.lang.String routingKey,
Message message)
throws AmqpException
sendAndReceive in interface AmqpTemplateAmqpException
public Message sendAndReceive(java.lang.String exchange,
java.lang.String routingKey,
Message message)
throws AmqpException
sendAndReceive in interface AmqpTemplateAmqpException
public java.lang.Object convertSendAndReceive(java.lang.Object message)
throws AmqpException
convertSendAndReceive in interface AmqpTemplateAmqpException
public java.lang.Object convertSendAndReceive(java.lang.String routingKey,
java.lang.Object message)
throws AmqpException
convertSendAndReceive in interface AmqpTemplateAmqpException
public java.lang.Object convertSendAndReceive(java.lang.String exchange,
java.lang.String routingKey,
java.lang.Object message)
throws AmqpException
convertSendAndReceive in interface AmqpTemplateAmqpException
public java.lang.Object convertSendAndReceive(java.lang.Object message,
MessagePostProcessor messagePostProcessor)
throws AmqpException
convertSendAndReceive in interface AmqpTemplateAmqpException
public java.lang.Object convertSendAndReceive(java.lang.String routingKey,
java.lang.Object message,
MessagePostProcessor messagePostProcessor)
throws AmqpException
convertSendAndReceive in interface AmqpTemplateAmqpException
public java.lang.Object convertSendAndReceive(java.lang.String exchange,
java.lang.String routingKey,
java.lang.Object message,
MessagePostProcessor messagePostProcessor)
throws AmqpException
convertSendAndReceive in interface AmqpTemplateAmqpException
protected Message doSendAndReceive(java.lang.String exchange,
java.lang.String routingKey,
Message message)
exchange - the exchange nameroutingKey - the routing keymessage - the message to send
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,
Message message)
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()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||