How can I set time for consumer side in RABBITMQ
This is my code and i set the true on autoDelete both queue , exchange finally publish is not sending any message to consumer several minute at this time i would li开发者_如何学运维ke stop the consumer side automatically maybe you are not understand my sentence perfectly.
how can i setting that ^^
and how do I get document Object(doc) in server side
public void initConsumer() {
try {
ConnectionFactory factory = new ConnectionFactory();
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(this.queueName, this.maintain, false, this.queueAutoDelete, null);
channel.exchangeDeclare(this.exchangeName, this.exchangeType, this.maintain, this.exchangeAutoDelete, null);
channel.queueBind(this.queueName, this.exchangeName, this.routingKey);
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(this.queueName, false, consumer);
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
System.out.println(" [x] Received "
+ new String(delivery.getBody()));
channel
.basicAck(delivery.getEnvelope().getDeliveryTag(),
false);
}
} catch (Exception e) {
System.out.println("Exception error at initConsumer()");
}
}
You can use the overloaded version of nextDelivery() which has a timeout parameter:
QueueingConsumer.Delivery delivery = null;
long timeout = 2 * 60 * 1000; // 2 minutes in milliseconds
delivery = queuingConsumer.nextDelivery(timeout);
if (delivery == null) {
// shut down your consumer here - no events arrived
// before the timeout was reached
}
else {
// process the delivered message here
}
Hope that helps.
精彩评论