Example usage for com.rabbitmq.client Channel queueDeclarePassive

List of usage examples for com.rabbitmq.client Channel queueDeclarePassive

Introduction

In this page you can find the example usage for com.rabbitmq.client Channel queueDeclarePassive.

Prototype

Queue.DeclareOk queueDeclarePassive(String queue) throws IOException;

Source Link

Document

Declare a queue passively; i.e., check if it exists.

Usage

From source file:pl.nask.hsn2.bus.rabbitmq.RbtUtils.java

License:Open Source License

/**
 * Gets a count of messages in given queue.
 * /* ww  w. j av  a 2s . c o  m*/
 * @param connection Connection to Rabbit MQ server.
 * @param queueName Name of the queue to check.
 * @return Number of messages in the queue.
 * @throws BusException Any problem with connection or queue will thrown an exception.
 */
public static int getMessageCount(Connection connection, String queueName) throws BusException {
    Channel channel = createChannel(connection);
    try {
        DeclareOk ok = channel.queueDeclarePassive(queueName);
        return ok.getMessageCount();
    } catch (IOException ex) {
        throw new BusException(ex);
    } finally {
        closeChannel(channel);
    }
}

From source file:pl.nask.hsn2.bus.rabbitmq.RbtUtils.java

License:Open Source License

/**
 * Gets a count of consumers attached to given queue.
 * /*  www.  j a  v a  2s  . co  m*/
 * @param connection Connection to Rabbit MQ server.
 * @param queueName Name of the queue to check.
 * @return Number of consumers attached to the queue.
 * @throws BusException Any problem with connection or queue will thrown an exception.
 */
public static int getConsumersCount(Connection connection, String queueName) throws BusException {
    Channel channel = createChannel(connection);
    try {
        DeclareOk ok = channel.queueDeclarePassive(queueName);
        return ok.getConsumerCount();
    } catch (IOException ex) {
        throw new BusException(ex);
    } finally {
        closeChannel(channel);
    }
}

From source file:reactor.rabbitmq.RabbitFluxTests.java

License:Open Source License

@Test
public void declareDeleteResources() throws Exception {
    Channel channel = connection.createChannel();

    final String queueName = UUID.randomUUID().toString();
    final String exchangeName = UUID.randomUUID().toString();

    try {/* w  w w.  java 2  s  .  co  m*/
        CountDownLatch latchCreation = new CountDownLatch(1);
        sender = createSender();
        Disposable resourceCreation = sender.declare(exchange(exchangeName))
                .then(sender.declare(queue(queueName)))
                .then(sender.bind(binding(exchangeName, "a.b", queueName)))
                .doAfterTerminate(() -> latchCreation.countDown()).subscribe();

        assertTrue(latchCreation.await(1, TimeUnit.SECONDS));

        channel.exchangeDeclarePassive(exchangeName);
        channel.queueDeclarePassive(queueName);
        resourceCreation.dispose();

        CountDownLatch latchDeletion = new CountDownLatch(1);

        Disposable resourceDeletion = sender.unbind(binding(exchangeName, "a.b", queueName))
                .then(sender.delete(exchange(exchangeName))).then(sender.delete(queue(queueName)))
                .doAfterTerminate(() -> latchDeletion.countDown()).subscribe();

        assertTrue(latchDeletion.await(1, TimeUnit.SECONDS));
        resourceDeletion.dispose();
    } finally {
        try {
            channel.exchangeDeclarePassive(exchangeName);
            fail("The exchange should have been deleted, exchangeDeclarePassive should have thrown an exception");
        } catch (IOException e) {
            // OK
            channel = connection.createChannel();
        }
        try {
            channel.queueDeclarePassive(queueName);
            fail("The queue should have been deleted, queueDeclarePassive should have thrown an exception");
        } catch (IOException e) {
            // OK
        }
    }
}

From source file:reactor.rabbitmq.RabbitFluxTests.java

License:Open Source License

@Test
public void declareDeleteResourcesWithOptions() throws Exception {
    Channel channel = connection.createChannel();

    final String queueName = UUID.randomUUID().toString();
    final String exchangeName = UUID.randomUUID().toString();

    try {/*from  www.  j a  va  2 s. c  o m*/
        SenderOptions senderOptions = new SenderOptions();
        Mono<Connection> connectionMono = Mono
                .fromCallable(() -> senderOptions.getConnectionFactory().newConnection());
        AtomicInteger channelMonoCalls = new AtomicInteger(0);
        Mono<Channel> channelMono = connectionMono.map(c -> {
            try {
                channelMonoCalls.incrementAndGet();
                return c.createChannel();
            } catch (Exception e) {
                Exceptions.propagate(e);
            }
            return null;
        });
        senderOptions.connectionMono(connectionMono);
        CountDownLatch latchCreation = new CountDownLatch(1);
        sender = createSender(senderOptions);

        ResourceManagementOptions options = new ResourceManagementOptions().channelMono(channelMono);

        Disposable resourceCreation = sender.declare(exchange(exchangeName), options)
                .then(sender.declare(queue(queueName), options))
                .then(sender.bind(binding(exchangeName, "a.b", queueName), options))
                .doAfterTerminate(() -> latchCreation.countDown()).subscribe();

        assertTrue(latchCreation.await(1, TimeUnit.SECONDS));
        assertEquals(3, channelMonoCalls.get());

        channel.exchangeDeclarePassive(exchangeName);
        channel.queueDeclarePassive(queueName);
        resourceCreation.dispose();

        CountDownLatch latchDeletion = new CountDownLatch(1);

        Disposable resourceDeletion = sender.unbind(binding(exchangeName, "a.b", queueName), options)
                .then(sender.delete(exchange(exchangeName), options))
                .then(sender.delete(queue(queueName), options))
                .doAfterTerminate(() -> latchDeletion.countDown()).subscribe();

        assertTrue(latchDeletion.await(1, TimeUnit.SECONDS));
        assertEquals(3 + 3, channelMonoCalls.get());
        resourceDeletion.dispose();
    } finally {
        try {
            channel.exchangeDeclarePassive(exchangeName);
            fail("The exchange should have been deleted, exchangeDeclarePassive should have thrown an exception");
        } catch (IOException e) {
            // OK
            channel = connection.createChannel();
        }
        try {
            channel.queueDeclarePassive(queueName);
            fail("The queue should have been deleted, queueDeclarePassive should have thrown an exception");
        } catch (IOException e) {
            // OK
        }
    }
}

From source file:reactor.rabbitmq.ReactorRabbitMqTests.java

License:Open Source License

@Test
public void createResources() throws Exception {
    final Channel channel = connection.createChannel();

    final String queueName = UUID.randomUUID().toString();
    final String exchangeName = UUID.randomUUID().toString();

    try {//  w ww  . ja  v a 2 s . c om
        sender = ReactorRabbitMq.createSender();

        Mono<AMQP.Queue.BindOk> resources = sender
                .createExchange(ExchangeSpecification.exchange().name(exchangeName))
                .then(sender.createQueue(QueueSpecification.queue(queueName)))
                .then(sender.bind(BindingSpecification.binding().queue(queueName).exchange(exchangeName)
                        .routingKey("a.b")));

        resources.block(java.time.Duration.ofSeconds(1));

        channel.exchangeDeclarePassive(exchangeName);
        channel.queueDeclarePassive(queueName);
    } finally {
        channel.exchangeDelete(exchangeName);
        channel.queueDelete(queueName);
        channel.close();
    }
}

From source file:uk.ac.soton.itinnovation.experimedia.arch.ecc.amqpAPI.impl.amqp.AMQPBasicChannel.java

public static boolean amqpQueueExists(AMQPConnectionFactory conFactory, String queueName) throws Exception {
    // Safety first
    if (conFactory == null)
        throw new Exception("Could not test amqp queue: amqp connection factory is null");
    if (queueName == null)
        throw new Exception("Could not test amqp queue: queue name is null");
    if (!conFactory.isConnectionValid())
        throw new Exception("Could not test amqp queue: factory connection is invalid");

    // Need to create an independent connection & channel to test for a queue -
    // a negative result automatically closes a channel.

    AMQPBasicChannel bc = conFactory.createNewChannel();
    Channel channelImpl = (Channel) bc.getChannelImpl();

    boolean result = false;
    String resultInfo = "AMQP queue ( " + queueName + ") existence result: ";

    if (channelImpl != null && channelImpl.isOpen()) {
        // Try passively declaring the queue to see if it exists
        try {// ww  w  .j av  a 2 s  . c o  m
            channelImpl.queueDeclarePassive(queueName);
            result = true;
            resultInfo += "exists";
            channelImpl.close();
        } catch (IOException ex) {
            resultInfo += "does not exist";
            // Channel will be automatically closed in this case
        }

    } else
        resultInfo += " could not test: channel is null or closed";

    LoggerFactory.getLogger(AMQPBasicChannel.class).info(resultInfo);

    return result;
}

From source file:vn.com.uet.performance.rabbitmq.MulticastParams.java

License:Open Source License

private static boolean queueExists(Connection connection, final String queueName) throws IOException {
    return queueName != null && exists(connection, new Checker() {
        public void check(Channel ch) throws IOException {
            ch.queueDeclarePassive(queueName);
        }//from w  ww  .j  a  v  a  2  s.  co m
    });
}