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:acromusashi.stream.component.rabbitmq.QueueSizeCallBack.java

License:Open Source License

/**
 * {@inheritDoc}//w w w.ja  va 2s.  c  om
 */
@Override
public Integer doInRabbit(Channel channel) throws Exception {
    return channel.queueDeclarePassive(this.queueName).getMessageCount();
}

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

License:Apache License

public void checkQueueOrDie(String queueName) throws UnreachableModuleException {
    if (!connection.isOpen()) {
        throw new UnreachableModuleException("Not connected to broker!");
    }/*from   ww w  .jav  a2s.  c o  m*/
    try {
        Channel ch = connection.createChannel();
        AMQP.Queue.DeclareOk declare = ch.queueDeclarePassive(queueName);
        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 w w  . j a  va  2  s . c  o m*/
    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!");
    }//w  ww  .j  av a2  s. com
    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!");
    }//  w  w  w.  j  ava2s . c  o  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 ww . j  av  a2 s. co m
        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:com.DeadLetterReceiver.java

License:Open Source License

public static void main(String[] args)
        throws IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(HOSTNAME);//from  w w  w  .  j av a2 s .  c o  m
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    //creating reply queue
    try {
        channel.queueDeclarePassive(QUEUE_NAME);
    } catch (java.io.IOException e) {
        if (!channel.isOpen())
            channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    }

    System.out.println(" [*] Waiting for responses. To exit press CTRL+C");
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(QUEUE_NAME, true, consumer);

    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        BasicProperties props = delivery.getProperties();
        String message = new String(delivery.getBody());
        System.out.println(" [x] Response received '" + message + "'");
        System.out.println("Correlation id : " + props.getCorrelationId());
    }
}

From source file:com.DeadLetterSender.java

License:Open Source License

public static void main(String[] argv) throws Exception {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(HOSTNAME);//from w  ww  . ja  va  2  s . c o m
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    try {
        channel.exchangeDeclarePassive(EXCHANGE_NAME);
    } catch (java.io.IOException e) {
        if (!channel.isOpen())
            channel = connection.createChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, "direct", true);
    }

    try {
        channel.queueDeclarePassive(QUEUE_NAME);
    } catch (java.io.IOException e) {
        if (!channel.isOpen())
            channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    }
    channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, QUEUE_NAME);

    // Request Message
    String message = "<soapenv:Envelope" + " xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
            + "<soapenv:Body>\n" + "  <p:greet xmlns:p=\"http://service.wso2.org\">\n" + "     <in>" + "IBM"
            + "</in>\n" + "  </p:greet>\n" + "</soapenv:Body>\n" + "</soapenv:Envelope>";

    // Adding Message Properties
    AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties().builder();
    builder.messageId("007");
    builder.contentType("text/xml");
    builder.correlationId("1111");
    builder.replyTo(REPLY_QUEUE_NAME);
    builder.contentEncoding("UTF-8");

    // Custom user properties
    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put("SOAP_ACTION", "getQuote");
    builder.headers(headers);

    // Publish the message to exchange
    channel.basicPublish(EXCHANGE_NAME, QUEUE_NAME, builder.build(), message.getBytes());
    channel.close();
    connection.close();
}

From source file:com.digispherecorp.enterprise.rabbitmq.ra.work.QueueRabbitMQWork.java

@Override
public void run() {
    try {/*from   w  w w .jav a2 s. c  o m*/

        Logger.getLogger(QueueRabbitMQWork.class.getName()).log(Level.INFO,
                "Work Schedule Polling started @ ".concat(uuid));

        final Channel channel;
        RabbitMQConnectionFactoryFacade instance = RabbitMQConnectionFactoryFacade.getInstance();
        if (instance.getConnectionRequestInfo(activationSpec) == null) {
            instance.setConnectionRequestInfo(activationSpec,
                    new RabbitMQConnectionRequestInfo(((RabbitMQActivationSpec) activationSpec).getUser(),
                            ((RabbitMQActivationSpec) activationSpec).getPassword(),
                            ((RabbitMQActivationSpec) activationSpec).getHost(),
                            ((RabbitMQActivationSpec) activationSpec).getPort(),
                            ((RabbitMQActivationSpec) activationSpec).getVirtualHost()));
        }
        try {
            connection = instance.getConnection();
            channel = connection.createChannel();
            channel.queueDeclarePassive(((RabbitMQActivationSpec) activationSpec).getQueueName());
            channel.basicQos(0);

            final Consumer consumer = new DefaultConsumer(channel) {

                @Override
                public void handleConsumeOk(String consumerTag) {
                    super.handleConsumeOk(consumerTag);
                }

                @Override
                public void handleDelivery(String consumerTag, Envelope envelope,
                        AMQP.BasicProperties properties, byte[] body) throws IOException {
                    super.handleDelivery(consumerTag, envelope, properties, body);

                    if (!envelope.isRedeliver()) {
                        try {
                            RabbitMQMessage mQMessage = new RabbitMQMessage();
                            mQMessage.getMessages().add(body);
                            try {
                                receiveMessages(mQMessage);
                            } catch (ResourceException ex) {
                                Logger.getLogger(QueueRabbitMQWork.class.getName()).log(Level.SEVERE,
                                        ex.getLocalizedMessage(), ex);
                            }
                        } finally {
                            channel.basicAck(envelope.getDeliveryTag(), true);
                        }
                    }
                }
            };
            channel.basicConsume(((RabbitMQActivationSpec) activationSpec).getQueueName(), false, consumer);
        } catch (IOException ex) {
            Logger.getLogger(RabbitMQResourceAdapter.class.getName()).log(Level.SEVERE,
                    ex.getLocalizedMessage(), ex);
        }
    } catch (Exception ex) {
        Logger.getLogger(QueueRabbitMQWork.class.getName()).log(Level.SEVERE, ex.getLocalizedMessage(), ex);
    }
}

From source file:com.shopwiki.roger.rpc.RpcServer.java

License:Apache License

/**
 * 1. Creates {@link RpcWorker}s using the {@link WorkerFactory} provided to the constructor.
 * 2. Declares queues & binds routing-keys if a (@link QueueDeclarator} was provided to the constructor.
 * 3. Starts each {@link RpcWorker}./*from  w  w  w  .java  2s. c o  m*/
 *
 * @throws IOException
 */
public void start() throws IOException {
    RpcWorkers workers = workerFactory.createWorkers(queuePrefix);

    Connection conn = null;
    try {
        conn = workers.getConnection();
        conn.addShutdownListener(reconnector);
        Channel channel = conn.createChannel();

        if (queueDeclarator != null) {
            for (RpcWorker worker : workers) {
                queueDeclarator.declareQueue(channel, worker);
                queueDeclarator.bindQueue(channel, worker);
            }
        }

        for (RpcWorker worker : workers) {
            worker.setPostProcessors(postProcessors);
            String queueName = worker.getQueueName();

            System.out.println(channel + " - Starting Worker for queue: " + queueName);
            channel.queueDeclarePassive(queueName); // make sure the handler's queue exists
            worker.start();
        }
    } catch (IOException e) {
        RabbitConnector.closeConnection(conn);
        throw e;
    } catch (RuntimeException e) {
        RabbitConnector.closeConnection(conn);
        throw e;
    } catch (Error e) {
        RabbitConnector.closeConnection(conn);
        throw e;
    }
}