use of com.rabbitmq.client.QueueingConsumer in project microservices by pwillhan.
the class PublishSubscribeReceiver method receive.
public String receive(String queue) {
if (channel == null) {
initialize();
}
String message = null;
try {
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
channel.queueDeclare(queue, false, false, false, null);
channel.queueBind(queue, EXCHANGE_NAME, "");
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(queue, true, consumer);
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
message = new String(delivery.getBody());
LOGGER.info("Message received: " + message);
return message;
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
} catch (ShutdownSignalException e) {
LOGGER.error(e.getMessage(), e);
} catch (ConsumerCancelledException e) {
LOGGER.error(e.getMessage(), e);
} catch (InterruptedException e) {
LOGGER.error(e.getMessage(), e);
}
return message;
}
use of com.rabbitmq.client.QueueingConsumer in project rabbitmq-java-client by rabbitmq.
the class RequeueOnClose method publishLotsAndGet.
private void publishLotsAndGet() throws IOException, InterruptedException, ShutdownSignalException, TimeoutException {
openConnection();
open();
channel.queueDeclare(Q, false, false, false, null);
channel.queueDelete(Q);
channel.queueDeclare(Q, false, false, false, null);
for (int i = 0; i < MESSAGE_COUNT; i++) {
channel.basicPublish("", Q, null, "in flight message".getBytes());
}
QueueingConsumer c = new QueueingConsumer(channel);
channel.basicConsume(Q, c);
c.nextDelivery();
close();
open();
for (int i = 0; i < MESSAGE_COUNT; i++) {
assertNotNull("only got " + i + " out of " + MESSAGE_COUNT + " messages", channel.basicGet(Q, true));
}
assertNull("got more messages than " + MESSAGE_COUNT + " expected", channel.basicGet(Q, true));
channel.queueDelete(Q);
close();
closeConnection();
}
use of com.rabbitmq.client.QueueingConsumer in project rabbitmq-java-client by rabbitmq.
the class RequeueOnClose method requeueingConsumer.
/**
* Test we requeue unacknowledged message (using consumer)
* @throws Exception untested
*/
@Test
public void requeueingConsumer() throws Exception {
openConnection();
open();
injectMessage();
QueueingConsumer c = new QueueingConsumer(channel);
channel.basicConsume(Q, c);
c.nextDelivery();
close();
open();
assertNotNull(getMessage());
close();
closeConnection();
}
use of com.rabbitmq.client.QueueingConsumer in project rabbitmq-java-client by rabbitmq.
the class TTLHandling method expiryWithReQueueAfterConsume.
/*
* Test expiry of re-queued messages after being consumed instantly
*/
@Test
public void expiryWithReQueueAfterConsume() throws Exception {
declareAndBindQueue(100);
QueueingConsumer c = new QueueingConsumer(channel);
channel.basicConsume(TTL_QUEUE_NAME, c);
publish(MSG[0]);
assertNotNull(c.nextDelivery(100));
closeChannel();
Thread.sleep(150);
openChannel();
assertNull("Re-queued message not expired", get());
}
use of com.rabbitmq.client.QueueingConsumer in project rabbitmq-java-client by rabbitmq.
the class TTLHandling method zeroTTLDelivery.
@Test
public void zeroTTLDelivery() throws Exception {
declareAndBindQueue(0);
// when there is no consumer, message should expire
publish(MSG[0]);
assertNull(get());
// when there is a consumer, message should be delivered
QueueingConsumer c = new QueueingConsumer(channel);
channel.basicConsume(TTL_QUEUE_NAME, c);
publish(MSG[0]);
QueueingConsumer.Delivery d = c.nextDelivery(100);
assertNotNull(d);
// requeued messages should expire
channel.basicReject(d.getEnvelope().getDeliveryTag(), true);
assertNull(c.nextDelivery(100));
}
Aggregations