Example usage for com.rabbitmq.client Channel queueDeclare

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

Introduction

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

Prototype

Queue.DeclareOk queueDeclare() throws IOException;

Source Link

Document

Actively declare a server-named exclusive, autodelete, non-durable queue.

Usage

From source file:org.apache.airavata.datacat.agent.messageBroker.AiravataUpdateListener.java

License:Apache License

public void startBroker() {
    (new Thread(new Runnable() {
        @Override/*from  w  w w  . j a  v a2 s .co  m*/
        public void run() {
            try {
                ConnectionFactory factory = new ConnectionFactory();
                factory.setHost(RABBITMQ_HOST);

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

                channel.exchangeDeclare(EXCHANGE_NAME, "topic");
                String queueName = channel.queueDeclare().getQueue();

                channel.basicQos(1);
                channel.queueBind(queueName, EXCHANGE_NAME, BINDING_KEY);

                logger.debug("Waiting for messages. To exit press CTRL+C");

                QueueingConsumer consumer = new QueueingConsumer(channel);
                channel.basicConsume(queueName, true, consumer);

                while (runFileUpdateListener) {
                    QueueingConsumer.Delivery delivery = consumer.nextDelivery();

                    Message message = new Message();
                    ThriftUtils.createThriftFromBytes(delivery.getBody(), message);
                    TBase event = null;

                    if (message.getMessageType().equals(MessageType.EXPERIMENT_OUTPUT)) {

                        ExperimentOutputCreatedEvent experimentOutputCreatedEvent = new ExperimentOutputCreatedEvent();
                        ThriftUtils.createThriftFromBytes(message.getEvent(), experimentOutputCreatedEvent);

                        logger.debug(" Message Received with message id '" + message.getMessageId()
                                + "' and with message type '" + message.getMessageType()
                                + "'  with experiment name "
                                + experimentOutputCreatedEvent.getExperimentName());

                        event = experimentOutputCreatedEvent;

                        logger.debug(" [x] Received FileInfo Message'");
                        process(experimentOutputCreatedEvent, message.getUpdatedTime());
                        logger.debug(" [x] Done Processing FileInfo Message");
                    } else {
                        logger.debug("Recieved message of type ..." + message.getMessageType());
                    }
                }
            } catch (Exception e) {
                logger.error(e);
            }
        }

    })).start();

}

From source file:org.apache.airavata.gfac.monitor.impl.push.amqp.AMQPMonitor.java

License:Apache License

@Override
public boolean registerListener(MonitorID monitorID) throws AiravataMonitorException {
    // we subscribe to read user-host based subscription
    HostDescription host = monitorID.getHost();
    String hostAddress = host.getType().getHostAddress();
    // in amqp case there are no multiple jobs per each host, because once a job is put in to the queue it
    // will be picked by the Monitor, so jobs will not stay in this queueu but jobs will stay in finishQueue
    String channelID = CommonUtils.getChannelID(monitorID);
    if (availableChannels.get(channelID) == null) {
        try {//w ww  .j a va  2  s .co  m
            //todo need to fix this rather getting it from a file
            Connection connection = AMQPConnectionUtil.connect(amqpHosts, connectionName, proxyPath);
            Channel channel = null;
            channel = connection.createChannel();
            availableChannels.put(channelID, channel);
            String queueName = channel.queueDeclare().getQueue();

            BasicConsumer consumer = new BasicConsumer(new JSONMessageParser(), localPublisher); // here we use local publisher
            channel.basicConsume(queueName, true, consumer);
            String filterString = CommonUtils.getRoutingKey(monitorID.getUserName(), hostAddress);
            // here we queuebind to a particular user in a particular machine
            channel.queueBind(queueName, "glue2.computing_activity", filterString);
            logger.info("Using filtering string to monitor: " + filterString);
        } catch (IOException e) {
            logger.error("Error creating the connection to finishQueue the job:" + monitorID.getUserName());
        }
    }
    return true;
}

From source file:org.apache.airavata.gfac.monitor.impl.push.amqp.AMQPMonitor.java

License:Apache License

@Subscribe
public boolean unRegisterListener(MonitorID monitorID) throws AiravataMonitorException {
    Iterator<MonitorID> iterator = finishQueue.iterator();
    MonitorID next = null;/*from w w  w . j  a v a  2 s. c  o m*/
    while (iterator.hasNext()) {
        next = iterator.next();
        if (next.getJobID().endsWith(monitorID.getJobID())) {
            break;
        }
    }
    if (next == null) {
        logger.error("Job has removed from the queue, old obsolete message recieved");
        return false;
    }
    String channelID = CommonUtils.getChannelID(next);
    if (JobState.FAILED.equals(monitorID.getStatus()) || JobState.COMPLETE.equals(monitorID.getStatus())) {
        finishQueue.remove(next);

        // if this is the last job in the queue at this point with the same username and same host we
        // close the channel and close the connection and remove it from availableChannels
        if (CommonUtils.isTheLastJobInQueue(finishQueue, next)) {
            logger.info("There are no jobs to monitor for common ChannelID:" + channelID
                    + " , so we unsubscribe it" + ", incase new job created we do subscribe again");
            Channel channel = availableChannels.get(channelID);
            if (channel == null) {
                logger.error("Already Unregistered the listener");
                throw new AiravataMonitorException("Already Unregistered the listener");
            } else {
                try {
                    channel.queueUnbind(channel.queueDeclare().getQueue(), "glue2.computing_activity",
                            CommonUtils.getRoutingKey(next));
                    channel.close();
                    channel.getConnection().close();
                    availableChannels.remove(channelID);
                } catch (IOException e) {
                    logger.error("Error unregistering the listener");
                    throw new AiravataMonitorException("Error unregistering the listener");
                }
            }
        }
    }
    next.setStatus(monitorID.getStatus());
    JobIdentifier jobIdentity = new JobIdentifier(next.getJobID(), next.getTaskID(), next.getWorkflowNodeID(),
            next.getExperimentID(), next.getJobExecutionContext().getGatewayID());
    publisher.publish(new JobStatusChangeEvent(next.getStatus(), jobIdentity));
    return true;
}

From source file:org.apache.airavata.gfac.monitor.impl.push.amqp.UnRegisterWorker.java

License:Apache License

@Subscribe
private boolean unRegisterListener(JobStatusChangeEvent jobStatus, MonitorID monitorID)
        throws AiravataMonitorException {
    String channelID = CommonUtils.getChannelID(monitorID);
    if (JobState.FAILED.equals(jobStatus.getState()) || JobState.COMPLETE.equals(jobStatus.getState())) {
        Channel channel = availableChannels.get(channelID);
        if (channel == null) {
            logger.error("Already Unregistered the listener");
            throw new AiravataMonitorException("Already Unregistered the listener");
        } else {/*  w  w  w  .  j  a va2  s.  c om*/
            try {
                channel.queueUnbind(channel.queueDeclare().getQueue(), "glue2.computing_activity",
                        CommonUtils.getRoutingKey(monitorID));
                channel.close();
                channel.getConnection().close();
                availableChannels.remove(channelID);
            } catch (IOException e) {
                logger.error("Error unregistering the listener");
                throw new AiravataMonitorException("Error unregistering the listener");
            }
        }
    }
    return true;
}

From source file:org.apache.airavata.wsmg.client.amqp.rabbitmq.AMQPBroadcastReceiverImpl.java

License:Apache License

public void Subscribe() throws AMQPException {
    if (callback != null) {
        try {//from  ww  w . j  a v a 2  s. com
            Connection connection = connectionFactory.newConnection();

            Channel channel = connection.createChannel();
            channel.exchangeDeclare(AMQPUtil.EXCHANGE_NAME_FANOUT, AMQPUtil.EXCHANGE_TYPE_FANOUT);

            String queueName = channel.queueDeclare().getQueue();
            channel.queueBind(queueName, AMQPUtil.EXCHANGE_NAME_FANOUT, "");

            QueueingConsumer consumer = new QueueingConsumer(channel);
            channel.basicConsume(queueName, true, consumer);

            while (true) {
                QueueingConsumer.Delivery delivery = consumer.nextDelivery();
                String message = new String(delivery.getBody());

                callback.onMessage(message);
            }
        } catch (Exception e) {
            throw new AMQPException(e);
        }
    }
}

From source file:org.apache.airavata.wsmg.client.amqp.rabbitmq.AMQPReceiverImpl.java

License:Apache License

public void Subscribe(AMQPRoutingKey key) throws AMQPException {
    if (callback != null) {
        try {/*from   w  ww. ja  v a 2  s . com*/
            Connection connection = connectionFactory.newConnection();

            Channel channel = connection.createChannel();
            channel.exchangeDeclare(AMQPUtil.EXCHANGE_NAME_DIRECT, AMQPUtil.EXCHANGE_TYPE_DIRECT);

            String queueName = channel.queueDeclare().getQueue();
            channel.queueBind(queueName, AMQPUtil.EXCHANGE_NAME_DIRECT, key.getNativeKey());

            QueueingConsumer consumer = new QueueingConsumer(channel);
            channel.basicConsume(queueName, true, consumer);

            while (true) {
                QueueingConsumer.Delivery delivery = consumer.nextDelivery();
                String message = new String(delivery.getBody());

                callback.onMessage(message);
            }
        } catch (Exception e) {
            throw new AMQPException(e);
        }
    }
}

From source file:org.apache.airavata.wsmg.client.amqp.rabbitmq.AMQPTopicReceiverImpl.java

License:Apache License

public void Subscribe(AMQPRoutingKey topic) throws AMQPException {
    if (callback != null) {
        try {/*from   ww  w.j a  v a 2  s  .  co m*/
            Connection connection = connectionFactory.newConnection();

            Channel channel = connection.createChannel();
            channel.exchangeDeclare(AMQPUtil.EXCHANGE_NAME_TOPIC, AMQPUtil.EXCHANGE_TYPE_TOPIC);

            String queueName = channel.queueDeclare().getQueue();
            channel.queueBind(queueName, AMQPUtil.EXCHANGE_NAME_TOPIC, topic.getNativeKey());

            QueueingConsumer consumer = new QueueingConsumer(channel);
            channel.basicConsume(queueName, true, consumer);

            while (true) {
                QueueingConsumer.Delivery delivery = consumer.nextDelivery();
                String message = new String(delivery.getBody());

                callback.onMessage(message);
            }
        } catch (Exception e) {
            throw new AMQPException(e);
        }
    }
}

From source file:org.apache.beam.sdk.io.rabbitmq.RabbitMqIOTest.java

License:Apache License

@Test
public void testWriteExchange() throws Exception {
    final int maxNumRecords = 1000;
    List<RabbitMqMessage> data = generateRecords(maxNumRecords).stream()
            .map(bytes -> new RabbitMqMessage(bytes)).collect(Collectors.toList());
    p.apply(Create.of(data)).apply(/* w  w w  .j  a v a2  s.com*/
            RabbitMqIO.write().withUri("amqp://guest:guest@localhost:" + port).withExchange("WRITE", "fanout"));

    final List<String> received = new ArrayList<>();
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.setUri("amqp://guest:guest@localhost:" + port);
    Connection connection = null;
    Channel channel = null;
    try {
        connection = connectionFactory.newConnection();
        channel = connection.createChannel();
        channel.exchangeDeclare("WRITE", "fanout");
        String queueName = channel.queueDeclare().getQueue();
        channel.queueBind(queueName, "WRITE", "");
        Consumer consumer = new TestConsumer(channel, received);
        channel.basicConsume(queueName, true, consumer);

        p.run();

        while (received.size() < maxNumRecords) {
            Thread.sleep(500);
        }

        assertEquals(maxNumRecords, received.size());
        for (int i = 0; i < maxNumRecords; i++) {
            assertTrue(received.contains("Test " + i));
        }
    } finally {
        if (channel != null) {
            channel.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}

From source file:org.apache.camel.component.rabbitmq.reply.TemporaryQueueReplyManager.java

License:Apache License

@Override
protected Connection createListenerContainer() throws Exception {

    log.debug("Creating connection");
    Connection conn = endpoint.connect(executorService);

    log.debug("Creating channel");
    Channel channel = conn.createChannel();
    // setup the basicQos
    if (endpoint.isPrefetchEnabled()) {
        channel.basicQos(endpoint.getPrefetchSize(), endpoint.getPrefetchCount(), endpoint.isPrefetchGlobal());
    }/*w  w  w.  j a  va2  s.c  o  m*/

    //Let the server pick a random name for us
    DeclareOk result = channel.queueDeclare();
    log.debug("Temporary queue name {}", result.getQueue());
    setReplyTo(result.getQueue());

    //TODO check for the RabbitMQConstants.EXCHANGE_NAME header 
    channel.queueBind(getReplyTo(), endpoint.getExchangeName(), getReplyTo());

    consumer = new RabbitConsumer(this, channel);
    consumer.start();

    return conn;
}

From source file:org.apache.flume.amqp.AmqpConsumer.java

License:Apache License

/**
 * This method declares the exchange, queue and bindings needed by this consumer.
 * The method returns the queue name that will be used by this consumer.
 *
 * @param channel channel used to issue AMQP commands
 * @return queue that will have messages consumed from
 * @throws IOException thrown if there is any communication exception
 *///from   ww w  . ja  v  a  2  s  .co m
@VisibleForTesting
protected String declarationsForChannel(Channel channel) throws IOException {
    // setup exchange, queue and binding
    if (prefetchSize > 0) {
        channel.basicQos(prefetchSize);
    }

    // if exchange is provided
    if (exchangeName != null) {
        channel.exchangeDeclare(exchangeName, exchangeType, durableExchange);

        // named queue or server generated
        if (queueName == null) {
            queueName = channel.queueDeclare().getQueue();
        } else {
            channel.queueDeclare(queueName, durableQueue, exclusiveQueue, autoDeleteQueue, null);
        }

        if (bindings.length > 0) {
            // multiple bindings
            for (String binding : bindings) {
                channel.queueBind(queueName, exchangeName, binding);
            }
        } else {
            // no binding given - this could be the case if it is a fanout exchange
            channel.queueBind(queueName, exchangeName, Constants.AMQP.SERVER_GENERATED_QUEUE_NAME);
        }
    }

    return queueName;
}