use of com.rabbitmq.client.Envelope in project rabbitmq-java-client by rabbitmq.
the class TopologyRecoveryRetry method topologyRecoveryBindingFailure.
@Test
public void topologyRecoveryBindingFailure() throws Exception {
final String queue = "topology-recovery-retry-binding-failure" + System.currentTimeMillis();
channel.queueDeclare(queue, false, false, true, new HashMap<>());
channel.queueBind(queue, "amq.topic", "topic1");
channel.queueBind(queue, "amq.topic", "topic2");
final CountDownLatch messagesReceivedLatch = new CountDownLatch(2);
channel.basicConsume(queue, true, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) {
messagesReceivedLatch.countDown();
}
});
final CountDownLatch recoveryLatch = new CountDownLatch(1);
((AutorecoveringConnection) connection).addRecoveryListener(new RecoveryListener() {
@Override
public void handleRecoveryStarted(Recoverable recoverable) {
// no-op
}
@Override
public void handleRecovery(Recoverable recoverable) {
recoveryLatch.countDown();
}
});
// we want recovery to fail when recovering the 2nd binding
// give the 2nd recorded binding a bad queue name so it fails
final RecordedBinding binding2 = ((AutorecoveringConnection) connection).getRecordedBindings().get(1);
binding2.destination(UUID.randomUUID().toString());
// use the backoffConsumer to know that it has failed
// then delete the real queue & fix the recorded binding
// it should fail once more because queue is gone, and then succeed
final CountDownLatch backoffLatch = new CountDownLatch(1);
backoffConsumer = attempt -> {
if (attempt == 1) {
binding2.destination(queue);
try {
Host.rabbitmqctl("delete_queue " + queue);
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
}
backoffLatch.countDown();
};
// close connection
Host.closeAllConnections();
// assert backoff was called
assertTrue(backoffLatch.await(90, TimeUnit.SECONDS));
// wait for full recovery
assertTrue(recoveryLatch.await(90, TimeUnit.SECONDS));
// publish messages to verify both bindings were recovered
basicPublishVolatile("test1".getBytes(), "amq.topic", "topic1");
basicPublishVolatile("test2".getBytes(), "amq.topic", "topic2");
assertTrue(messagesReceivedLatch.await(10, TimeUnit.SECONDS));
}
use of com.rabbitmq.client.Envelope in project rabbitmq-java-client by rabbitmq.
the class CloseInMainLoop method closeWithFaultyConsumer.
// The thrown runtime exception should get intercepted by the
// consumer exception handler, and result in a clean shut down.
@Test
public void closeWithFaultyConsumer() throws Exception {
SpecialConnection connection = new SpecialConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare("x", "direct");
channel.queueDeclare("q", false, false, false, null);
channel.queueBind("q", "x", "k");
channel.basicConsume("q", true, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) {
throw new RuntimeException("I am a bad consumer");
}
});
channel.basicPublish("x", "k", null, new byte[10]);
assertTrue(closeLatch.await(1000, TimeUnit.MILLISECONDS));
assertTrue(connection.hadValidShutdown());
}
use of com.rabbitmq.client.Envelope in project rabbitmq-java-client by rabbitmq.
the class ChannelAsyncCompletableFutureTest method async.
@Test
public void async() throws Exception {
channel.confirmSelect();
CountDownLatch latch = new CountDownLatch(1);
AMQP.Queue.Declare queueDeclare = new AMQImpl.Queue.Declare.Builder().queue(queue).durable(true).exclusive(false).autoDelete(false).arguments(null).build();
channel.asyncCompletableRpc(queueDeclare).thenComposeAsync(action -> {
try {
return channel.asyncCompletableRpc(new AMQImpl.Exchange.Declare.Builder().exchange(exchange).type("fanout").durable(false).autoDelete(false).arguments(null).build());
} catch (IOException e) {
throw new RuntimeException(e);
}
}, executor).thenComposeAsync(action -> {
try {
return channel.asyncCompletableRpc(new AMQImpl.Queue.Bind.Builder().queue(queue).exchange(exchange).routingKey("").arguments(null).build());
} catch (IOException e) {
throw new RuntimeException(e);
}
}, executor).thenAcceptAsync(action -> {
try {
channel.basicPublish("", queue, null, "dummy".getBytes());
} catch (IOException e) {
throw new RuntimeException(e);
}
}, executor).thenAcceptAsync((whatever) -> {
try {
channel.basicConsume(queue, true, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
latch.countDown();
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}, executor);
channel.waitForConfirmsOrDie(1000);
assertTrue(latch.await(2, TimeUnit.SECONDS));
}
use of com.rabbitmq.client.Envelope in project rabbitmq-java-client by rabbitmq.
the class QosTests method singleChannelAndQueueFairness.
@Test
public void singleChannelAndQueueFairness() throws IOException {
// check that when we have multiple consumers on the same
// channel & queue, and a prefetch limit set, that all
// consumers get a fair share of the messages.
channel.basicQos(1, true);
String q = channel.queueDeclare().getQueue();
channel.queueBind(q, "amq.fanout", "");
final Map<String, Integer> counts = Collections.synchronizedMap(new HashMap<String, Integer>());
QueueingConsumer c = new QueueingConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
counts.put(consumerTag, counts.get(consumerTag) + 1);
super.handleDelivery(consumerTag, envelope, properties, body);
}
};
channel.basicConsume(q, false, "c1", c);
channel.basicConsume(q, false, "c2", c);
int count = 10;
counts.put("c1", 0);
counts.put("c2", 0);
fill(count);
try {
for (int i = 0; i < count; i++) {
Delivery d = c.nextDelivery();
channel.basicAck(d.getEnvelope().getDeliveryTag(), false);
}
} catch (InterruptedException ie) {
fail("interrupted");
}
// we only check that the server isn't grossly unfair; perfect
// fairness is too much to ask for (even though RabbitMQ atm
// does actually provide it in this case)
assertTrue(counts.get("c1").intValue() > 0);
assertTrue(counts.get("c2").intValue() > 0);
}
use of com.rabbitmq.client.Envelope in project rabbitmq-java-client by rabbitmq.
the class PriorityQueues method prioritiesOfEnqueuedMessages.
private List<Integer> prioritiesOfEnqueuedMessages(String q, int n) throws InterruptedException, IOException {
final List<Integer> xs = new ArrayList<Integer>();
final CountDownLatch latch = new CountDownLatch(n);
channel.basicConsume(q, true, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
xs.add(properties.getPriority());
latch.countDown();
}
});
latch.await(5, TimeUnit.SECONDS);
return xs;
}
Aggregations