public class QueueingConsumer extends DefaultConsumer
Consumer
with
straightforward blocking semantics.
The general pattern for using QueueingConsumer is as follows:
// Create connection and channel.ConnectionFactory
factory = new ConnectionFactory(); Connection conn = factory.newConnection();Channel
ch1 = conn.createChannel(); // Declare a queue and bind it to an exchange. String queueName = ch1.queueDeclare().getQueue
(); ch1.queueBind
(queueName, exchangeName, queueName); // Create the QueueingConsumer and have it consume from the queue QueueingConsumer consumer = newQueueingConsumer
(ch1); ch1.basicConsume
(queueName, false, consumer); // Process deliveries while (/* some condition * /) {QueueingConsumer.Delivery
delivery = consumer.nextDelivery
(); // process delivery ch1.basicAck
(delivery.getEnvelope
().getDeliveryTag
(), false); }
For a more detailed explanation, see the java api guide.
For a more complete example, see LogTail in the test/src/com/rabbitmq/examples directory of the source distribution.
Modifier and Type | Class and Description |
---|---|
static class |
QueueingConsumer.Delivery
Encapsulates an arbitrary message - simple "bean" holder structure.
|
Constructor and Description |
---|
QueueingConsumer(Channel ch) |
QueueingConsumer(Channel ch,
java.util.concurrent.BlockingQueue<QueueingConsumer.Delivery> q) |
Modifier and Type | Method and Description |
---|---|
void |
handleCancel(java.lang.String consumerTag)
No-op implementation of
Consumer.handleCancel(String) |
void |
handleDelivery(java.lang.String consumerTag,
Envelope envelope,
AMQP.BasicProperties properties,
byte[] body)
|
void |
handleShutdownSignal(java.lang.String consumerTag,
ShutdownSignalException sig)
No-op implementation of
Consumer.handleShutdownSignal(java.lang.String, com.rabbitmq.client.ShutdownSignalException) . |
QueueingConsumer.Delivery |
nextDelivery()
Main application-side API: wait for the next message delivery and return it.
|
QueueingConsumer.Delivery |
nextDelivery(long timeout)
Main application-side API: wait for the next message delivery and return it.
|
getChannel, getConsumerTag, handleCancelOk, handleConsumeOk, handleRecoverOk
public QueueingConsumer(Channel ch)
public QueueingConsumer(Channel ch, java.util.concurrent.BlockingQueue<QueueingConsumer.Delivery> q)
public void handleShutdownSignal(java.lang.String consumerTag, ShutdownSignalException sig)
DefaultConsumer
Consumer.handleShutdownSignal(java.lang.String, com.rabbitmq.client.ShutdownSignalException)
.handleShutdownSignal
in interface Consumer
handleShutdownSignal
in class DefaultConsumer
consumerTag
- the defined consumerTag (either client- or server-generated)sig
- an exception object encapsulating the reason for shutdownpublic void handleCancel(java.lang.String consumerTag) throws java.io.IOException
DefaultConsumer
Consumer.handleCancel(String)
handleCancel
in interface Consumer
handleCancel
in class DefaultConsumer
consumerTag
- the defined consumer tag (client- or server-generated)java.io.IOException
public void handleDelivery(java.lang.String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws java.io.IOException
DefaultConsumer
Consumer.handleDelivery(java.lang.String, com.rabbitmq.client.Envelope, com.rabbitmq.client.AMQP.BasicProperties, byte[])
.handleDelivery
in interface Consumer
handleDelivery
in class DefaultConsumer
consumerTag
- the defined consumerTag (either client- or server-generated)envelope
- packaging data for the messageproperties
- content header data for the messagebody
- the message body (opaque client-specific byte array)java.io.IOException
- if the consumer hits an I/O error while processing the messagepublic QueueingConsumer.Delivery nextDelivery() throws java.lang.InterruptedException, ShutdownSignalException, ConsumerCancelledException
java.lang.InterruptedException
- if an interrupt is received while waitingShutdownSignalException
- if the connection is shut down while waitingConsumerCancelledException
public QueueingConsumer.Delivery nextDelivery(long timeout) throws java.lang.InterruptedException, ShutdownSignalException, ConsumerCancelledException
timeout
- timeout in millisecondjava.lang.InterruptedException
- if an interrupt is received while waitingShutdownSignalException
- if the connection is shut down while waitingConsumerCancelledException