use of com.rabbitmq.client.DefaultConsumer in project rabbitmq-java-client by rabbitmq.
the class ExceptionHandling method testConsumerHandleConsumerException.
protected void testConsumerHandleConsumerException(ExceptionHandler eh, CountDownLatch latch, boolean expectChannelClose) throws InterruptedException, TimeoutException, IOException {
ConnectionFactory cf = newConnectionFactory(eh);
assertEquals(cf.getExceptionHandler(), eh);
Connection conn = cf.newConnection();
assertEquals(conn.getExceptionHandler(), eh);
Channel ch = conn.createChannel();
String q = ch.queueDeclare().getQueue();
ch.basicConsume(q, new DefaultConsumer(ch) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
throw new RuntimeException("exception expected here, don't freak out");
}
});
ch.basicPublish("", q, null, "".getBytes());
wait(latch);
assertEquals(!expectChannelClose, ch.isOpen());
}
use of com.rabbitmq.client.DefaultConsumer in project rabbitmq-java-client by rabbitmq.
the class Metrics method metrics.
@Test
public void metrics() throws IOException, TimeoutException {
StandardMetricsCollector metrics = new StandardMetricsCollector();
connectionFactory.setMetricsCollector(metrics);
Connection connection1 = null;
Connection connection2 = null;
try {
connection1 = connectionFactory.newConnection();
assertThat(metrics.getConnections().getCount()).isEqualTo(1L);
connection1.createChannel();
connection1.createChannel();
Channel channel = connection1.createChannel();
assertThat(metrics.getChannels().getCount()).isEqualTo(3L);
sendMessage(channel);
assertThat(metrics.getPublishedMessages().getCount()).isEqualTo(1L);
sendMessage(channel);
assertThat(metrics.getPublishedMessages().getCount()).isEqualTo(2L);
channel.basicGet(QUEUE, true);
assertThat(metrics.getConsumedMessages().getCount()).isEqualTo(1L);
channel.basicGet(QUEUE, true);
assertThat(metrics.getConsumedMessages().getCount()).isEqualTo(2L);
channel.basicGet(QUEUE, true);
assertThat(metrics.getConsumedMessages().getCount()).isEqualTo(2L);
connection2 = connectionFactory.newConnection();
assertThat(metrics.getConnections().getCount()).isEqualTo(2L);
connection2.createChannel();
channel = connection2.createChannel();
assertThat(metrics.getChannels().getCount()).isEqualTo(3L + 2L);
sendMessage(channel);
sendMessage(channel);
assertThat(metrics.getPublishedMessages().getCount()).isEqualTo(2L + 2L);
channel.basicGet(QUEUE, true);
assertThat(metrics.getConsumedMessages().getCount()).isEqualTo(2L + 1L);
channel.basicConsume(QUEUE, true, new DefaultConsumer(channel));
waitAtMost(timeout(), () -> metrics.getConsumedMessages().getCount() == 2L + 1L + 1L);
safeClose(connection1);
waitAtMost(timeout(), () -> metrics.getConnections().getCount() == 1L);
waitAtMost(timeout(), () -> metrics.getChannels().getCount() == 2L);
safeClose(connection2);
waitAtMost(timeout(), () -> metrics.getConnections().getCount() == 0L);
waitAtMost(timeout(), () -> metrics.getChannels().getCount() == 0L);
assertThat(metrics.getAcknowledgedMessages().getCount()).isEqualTo(0L);
assertThat(metrics.getRejectedMessages().getCount()).isEqualTo(0L);
} finally {
safeClose(connection1);
safeClose(connection2);
}
}
use of com.rabbitmq.client.DefaultConsumer in project rabbitmq-java-client by rabbitmq.
the class TopologyRecoveryRetry method topologyRecoveryConsumerFailure.
@Test
public void topologyRecoveryConsumerFailure() throws Exception {
final String queue = "topology-recovery-retry-consumer-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 consumer
// give the recorded consumer a bad queue name so it fails
final RecordedConsumer consumer = ((AutorecoveringConnection) connection).getRecordedConsumers().values().iterator().next();
consumer.setQueue(UUID.randomUUID().toString());
// use the backoffConsumer to know that it has failed
// then delete the real queue & fix the recorded consumer
// it should fail once more because queue is gone, and then succeed
final CountDownLatch backoffLatch = new CountDownLatch(1);
backoffConsumer = attempt -> {
if (attempt == 1) {
consumer.setQueue(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 & consumer 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.DefaultConsumer in project rabbitmq-java-client by rabbitmq.
the class Bug19219Test method it.
@Test
public void it() throws IOException, InterruptedException {
final Consumer c = new DefaultConsumer(channel);
// each.
for (int i = 0; i < Q_COUNT; i++) {
String qName = channel.queueDeclare().getQueue();
channel.queueBind(qName, "amq.fanout", "");
channel.basicConsume(qName, false, c);
}
// 2. send lots of messages in background, to keep the server,
// and especially the queues, busy
final Runnable r = new Runnable() {
public void run() {
try {
startPublisher();
} catch (IOException e) {
} catch (InterruptedException e) {
} catch (TimeoutException e) {
e.printStackTrace();
}
}
};
for (int i = 0; i < PUB_THREAD_COUNT; i++) {
final Thread t = new Thread(r);
t.start();
// wait for thread to finish initialisation
init.acquire();
}
// tell all threads to resume
resume.countDown();
// wait for threads to get into full swing
Thread.sleep(CLOSE_DELAY);
// 3. close channel. This will result in the server notifying
// all the queues in parallel, which in turn will requeue all
// the messages. The combined workload may result in some
// notifications timing out.
boolean success = false;
try {
channel.abort();
success = true;
} catch (ShutdownSignalException e) {
} finally {
// We deliberately do not perform a clean shutdown of all
// the connections. This test is pushing the server really
// hard, so we chose the quickest way to end things.
channel = null;
connection = null;
assertTrue(success);
}
}
use of com.rabbitmq.client.DefaultConsumer in project rabbitmq-java-client by rabbitmq.
the class QueueLifecycle method exclusiveNotAutoDelete.
@Test
public void exclusiveNotAutoDelete() throws IOException {
String name = "exclusivequeue";
channel.queueDeclare(name, false, true, false, null);
// now it's there
verifyQueue(name, false, true, false, null);
Consumer consumer = new DefaultConsumer(channel);
String consumerTag = channel.basicConsume(name, consumer);
channel.basicCancel(consumerTag);
// and still there, because exclusive no longer implies autodelete
verifyQueueExists(name);
}
Aggregations