Example usage for com.rabbitmq.client Channel close

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

Introduction

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

Prototype

@Override
void close() throws IOException, TimeoutException;

Source Link

Document

Close this channel with the com.rabbitmq.client.AMQP#REPLY_SUCCESS close code and message 'OK'.

Usage

From source file:br.uff.labtempo.omcp.common.utils.RabbitComm.java

License:Apache License

public void checkExchangeOrDie(String exchangeName) throws UnreachableModuleException {
    if (!connection.isOpen()) {
        throw new UnreachableModuleException("Not connected to broker!");
    }//from w w  w .j a v  a2 s.c  o m
    try {
        Channel ch = connection.createChannel();
        AMQP.Exchange.DeclareOk declare = ch.exchangeDeclarePassive(exchangeName);
        ch.close();
    } catch (Exception e) {
        throw new UnreachableModuleException("Queue not exists!");
    }
}

From source file:br.uff.labtempo.omcp.common.utils.RabbitComm.java

License:Apache License

public void checkHasConsumersOrDie(String queueName) throws UnreachableModuleException {
    if (!connection.isOpen()) {
        throw new UnreachableModuleException("Not connected to broker!");
    }//from  w  ww .  j av a 2s. c  om
    try {
        Channel ch = connection.createChannel();
        AMQP.Queue.DeclareOk declare = ch.queueDeclarePassive(queueName);
        ch.close();
        if (declare.getConsumerCount() <= 0) {
            throw new UnreachableModuleException("Module is not online");
        }
    } catch (UnreachableModuleException e) {
        throw e;
    } catch (Exception e) {
        throw new UnreachableModuleException("Queue not exists!");
    }
}

From source file:br.uff.labtempo.omcp.common.utils.RabbitComm.java

License:Apache License

public void checkNotHasConsumersOrDie(String queueName) throws UnreachableModuleException {
    if (!connection.isOpen()) {
        throw new UnreachableModuleException("Not connected to broker!");
    }/*from   w  ww  .j a v a 2  s.  c o  m*/
    try {
        Channel ch = connection.createChannel();
        AMQP.Queue.DeclareOk declare = ch.queueDeclarePassive(queueName);
        int consumers = declare.getConsumerCount();
        ch.close();
        if (consumers > 0) {
            throw new UnreachableModuleException("Has " + consumers + " consumers!");
        }
    } catch (UnreachableModuleException e) {
        throw e;
    } catch (Exception e) {
        throw new UnreachableModuleException("Queue not exists!");
    }
}

From source file:br.uff.labtempo.omcp.common.utils.RabbitComm.java

License:Apache License

public void checkHasOneConsumerOrDie(String queueName) throws UnreachableModuleException {
    if (!connection.isOpen()) {
        throw new UnreachableModuleException("Not connected to broker!");
    }//from  w  w  w .  java2s  . co  m
    try {
        Channel ch = connection.createChannel();
        AMQP.Queue.DeclareOk declare = ch.queueDeclarePassive(queueName);
        int consumers = declare.getConsumerCount();
        ch.close();
        if (consumers != 1) {
            throw new UnreachableModuleException("Has " + consumers + " consumers!");
        }
    } catch (UnreachableModuleException e) {
        throw e;
    } catch (Exception e) {
        throw new UnreachableModuleException("Queue not exists!");
    }
}

From source file:br.uff.labtempo.omcp.common.utils.RabbitComm.java

License:Apache License

public int countMessagesFromQueue(String queueName) throws UnreachableModuleException {
    try {//from   w w w  .  ja  va  2 s  . com
        Channel ch = connection.createChannel();
        AMQP.Queue.DeclareOk declare = ch.queueDeclarePassive(queueName);
        ch.close();
        return declare.getMessageCount();
    } catch (Exception e) {
        throw new UnreachableModuleException("Queue not exists!");
    }
}

From source file:br.uff.labtempo.omcp.common.utils.RabbitComm.java

License:Apache License

public void purgeQueueOrDie(String queueName) throws UnreachableModuleException {
    try {/*from w ww  .j  av  a2 s  .c om*/
        Channel ch = connection.createChannel();
        ch.queuePurge(queueName);
        ch.close();
    } catch (Exception e) {
        throw new UnreachableModuleException("Could not purge queue!");
    }
}

From source file:brooklyn.entity.messaging.rabbit.RabbitEc2LiveTest.java

License:Apache License

@Override
protected void doTest(Location loc) throws Exception {
    RabbitBroker rabbit = app.createAndManageChild(EntitySpec.create(RabbitBroker.class));
    rabbit.start(ImmutableList.of(loc));
    EntityTestUtils.assertAttributeEqualsEventually(rabbit, RabbitBroker.SERVICE_UP, true);

    byte[] content = "MessageBody".getBytes(Charsets.UTF_8);
    String queue = "queueName";
    Channel producer = null;
    Channel consumer = null;/*from   w  ww  .j av a  2s  .  c  o m*/
    try {
        producer = getAmqpChannel(rabbit);
        consumer = getAmqpChannel(rabbit);

        producer.queueDeclare(queue, true, false, false, Maps.<String, Object>newHashMap());
        producer.queueBind(queue, AmqpExchange.DIRECT, queue);
        producer.basicPublish(AmqpExchange.DIRECT, queue, null, content);

        QueueingConsumer queueConsumer = new QueueingConsumer(consumer);
        consumer.basicConsume(queue, true, queueConsumer);

        QueueingConsumer.Delivery delivery = queueConsumer.nextDelivery();
        assertEquals(delivery.getBody(), content);
    } finally {
        if (producer != null)
            producer.close();
        if (consumer != null)
            consumer.close();
    }
}

From source file:brooklyn.entity.messaging.rabbit.RabbitIntegrationTest.java

License:Apache License

/**
 * Closes the channel, guaranteeing the call won't hang this thread forever!
 * // ww w . j  a v a  2  s  .co m
 * Saw this during jenkins testing:
 * "main" prio=10 tid=0x00007f69c8008000 nid=0x5d70 in Object.wait() [0x00007f69d1318000]
 *         java.lang.Thread.State: WAITING (on object monitor)
 *         at java.lang.Object.wait(Native Method)
 *         - waiting on <0x00000000e0947cf8> (a com.rabbitmq.utility.BlockingValueOrException)
 *         at java.lang.Object.wait(Object.java:502)
 *         at com.rabbitmq.utility.BlockingCell.get(BlockingCell.java:50)
 *         - locked <0x00000000e0947cf8> (a com.rabbitmq.utility.BlockingValueOrException)
 *         at com.rabbitmq.utility.BlockingCell.get(BlockingCell.java:65)
 *         - locked <0x00000000e0947cf8> (a com.rabbitmq.utility.BlockingValueOrException)
 *         at com.rabbitmq.utility.BlockingCell.uninterruptibleGet(BlockingCell.java:111)
 *         - locked <0x00000000e0947cf8> (a com.rabbitmq.utility.BlockingValueOrException)
 *         at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:37)
 *         at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:349)
 *         at com.rabbitmq.client.impl.ChannelN.close(ChannelN.java:543)
 *         at com.rabbitmq.client.impl.ChannelN.close(ChannelN.java:480)
 *         at com.rabbitmq.client.impl.ChannelN.close(ChannelN.java:473)
 *         at com.rabbitmq.client.Channel$close.call(Unknown Source)
 *         at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
 *         at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
 *         at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
 *         at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callSafe(AbstractCallSite.java:75)
 *         at brooklyn.entity.messaging.rabbit.RabbitIntegrationTest.testClientConnection(RabbitIntegrationTest.groovy:107)
 */
private void closeSafely(final Channel channel, int timeoutMs) throws InterruptedException {
    if (channel == null)
        return;
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                channel.close();
            } catch (IOException e) {
                log.error("Error closing RabbitMQ Channel; continuing", e);
            }
        }
    });
    try {
        t.start();
        t.join(timeoutMs);

        if (t.isAlive()) {
            log.error("Timeout when closing RabbitMQ Channel " + channel + "; aborting close and continuing");
        }
    } finally {
        t.interrupt();
        t.join(1 * 1000);
        if (t.isAlive())
            t.stop();
    }
}

From source file:co.lp.arch.TestUtils.java

static void spawnThreadWithNewChannel(ConnectionFactory factory, ConsumeWithEx<Channel> f) {
    new Thread(() -> {
        try {//from   ww  w .j  av  a 2  s  . c om
            Connection conn = factory.newConnection();
            Channel chan = conn.createChannel();
            f.accept(chan);
            chan.close();
            conn.close();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }).start();
}

From source file:Colas.Colas.java

private String reciver(String enviar) {
    try {//from  w ww  .ja v a 2s .c  o m
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(IP);
        factory.setPort(5672);
        factory.setUsername("valencia");
        factory.setPassword("admin123");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare("SC", false, false, false, null);

        channel.basicPublish("", "SC", null, enviar.getBytes());
        System.out.println(" [x] Sent '" + enviar + "'");
        channel.close();
        connection.close();
    } catch (Exception e) {
        System.out.println("Error enviando ");
        e.printStackTrace();
    }
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setUsername("guest");
        factory.setPassword("admin123");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare("CF", false, false, false, null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume("CF", true, consumer);
        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            System.out.println(" [x] Received '" + message + "'");
            return message;

        }
    } catch (Exception e) {
        System.out.println("Error reciviendo ");
        e.printStackTrace();
    }
    return "Error";

}