Example usage for com.rabbitmq.client ConnectionFactory setUri

List of usage examples for com.rabbitmq.client ConnectionFactory setUri

Introduction

In this page you can find the example usage for com.rabbitmq.client ConnectionFactory setUri.

Prototype

public void setUri(String uriString)
        throws URISyntaxException, NoSuchAlgorithmException, KeyManagementException 

Source Link

Document

Convenience method for setting the fields in an AMQP URI: host, port, username, password and virtual host.

Usage

From source file:net.nzcorp.hbase.tableevent_signaler.TableEventSignalerTest.java

License:Apache License

@Test
public void discernNewPutFromUpdate() throws Exception {
    Map<String, String> kvs = configureHBase(primaryTableNameString, secondaryIdxTableNameString, "e", "eg",
            "a", amq_default_address, primaryTableNameString, "true", "");
    setupHBase(kvs);/*from   ww  w  .jav a 2 s  .  c  o m*/

    //simulate population of secondary index as a result of the above
    Put idxPut = new Put("EFG1".getBytes());
    idxPut.addColumn("a".getBytes(), "EFB1".getBytes(), "".getBytes());
    secondaryIdxTable.put(idxPut);

    /*
    The first put should be registered as a "put" action, while the second should be registered as an "update"
    action, thereby signalling different action to be taken by the consumers
     */

    Put tablePut = new Put("EFG1".getBytes());
    tablePut.addColumn("e".getBytes(), "some_key".getBytes(), "some_value".getBytes());
    primaryTable.put(tablePut);

    tablePut = new Put("EFG1".getBytes());
    tablePut.addColumn("e".getBytes(), "some_other_key".getBytes(), "some_value".getBytes());
    primaryTable.put(tablePut);

    //check that values made it to the queue
    com.rabbitmq.client.ConnectionFactory factory = new com.rabbitmq.client.ConnectionFactory();
    factory.setUri(amq_default_address);
    com.rabbitmq.client.Connection conn = factory.newConnection();
    com.rabbitmq.client.Channel channel = conn.createChannel();
    int msgs_to_consume = 2;
    while (msgs_to_consume > 0) {
        System.out.println(String.format("Messages to get: %s", msgs_to_consume));
        GetResponse response = channel.basicGet(primaryTableNameString, false);
        if (response == null)//busy-wait until the message has made it through the MQ
        {
            continue;
        }
        String routingKey = response.getEnvelope().getRoutingKey();
        Assert.assertEquals("Routing key should be rowkey", "genome", routingKey);

        String contentType = response.getProps().getContentType();
        Assert.assertEquals("Content type should be preserved", "application/json", contentType);

        Map<String, Object> headers = response.getProps().getHeaders();
        byte[] body = response.getBody();

        JSONObject jo = new JSONObject(new String(body));
        String column_family = (String) jo.get("column_family");
        Assert.assertEquals("Column family should be preserved in the message body", "eg", column_family);

        String column_value = (String) jo.get("column_qualifier");

        if (headers.get("action").toString().equals("update")) {
            Assert.assertEquals("Column value should be preserved in the message body", "some_other_key",
                    column_value);
        } else {
            Assert.assertEquals("Column value should be preserved in the message body", "some_key",
                    column_value);
        }

        long deliveryTag = response.getEnvelope().getDeliveryTag();
        channel.basicAck(deliveryTag, false);
        msgs_to_consume--;
    }
}

From source file:org.adslabs.adsfulltext.Worker.java

License:Open Source License

public boolean connect() {

    ConnectionFactory rabbitMQInstance;

    // This creates a connection object, that allows connections to be open to the same
    // RabbitMQ instance running
    ///*ww w.j a v a2 s  . c  o m*/
    // There seems to be a lot of catches, but each one should list the reason for it's raise
    //
    // It is not necessary to disconnect the connection, it should be dropped when the program exits
    // See the API guide for more info: https://www.rabbitmq.com/api-guide.html

    try {

        rabbitMQInstance = new ConnectionFactory();
        logger.info("Connecting to RabbitMQ instance: {}", this.config.data.RABBITMQ_URI);
        rabbitMQInstance.setUri(this.config.data.RABBITMQ_URI);
        this.connection = rabbitMQInstance.newConnection();
        this.channel = this.connection.createChannel();

        // This tells RabbitMQ not to give more than one message to a worker at a time.
        // Or, in other words, don't dispatch a new message to a worker until it has processed
        // and acknowledged the previous one.
        this.channel.basicQos(this.prefetchCount);

        return true;

    } catch (java.net.URISyntaxException error) {

        logger.error("URI error: {}", error.getMessage());
        return false;

    } catch (java.io.IOException error) {

        logger.error("IO Error, is RabbitMQ running???: {}", error.getMessage());
        try {
            logger.error("Sleeping before exit");
            TimeUnit.SECONDS.sleep(5);
            logger.error("Finished sleeping.");
        } catch (java.lang.InterruptedException errorTime) {
            logger.error("Interrupt: {}", errorTime.getMessage());
        }
        return false;

    } catch (java.security.NoSuchAlgorithmException error) {

        logger.error("Most likely an SSL related error: ", error.getMessage());
        return false;

    } catch (java.security.KeyManagementException error) {

        logger.error("Most likely an SSL related error: ", error.getMessage());
        return false;

    }
}

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

License:Apache License

public void listen() {
    try {/*from  w  ww.j a  va  2s .c o  m*/
        String queueName = ServerSettings.getSetting(Constants.GFAC_SERVER_PORT, "8950");
        String uri = "amqp://localhost";

        ConnectionFactory connFactory = new ConnectionFactory();
        connFactory.setUri(uri);
        Connection conn = connFactory.newConnection();
        logger.info("--------Created the connection to Rabbitmq server successfully-------");

        final Channel ch = conn.createChannel();

        logger.info("--------Created the channel with Rabbitmq server successfully-------");

        ch.queueDeclare(queueName, false, false, false, null);

        logger.info("--------Declare the queue " + queueName + " in Rabbitmq server successfully-------");

        final QueueingConsumer consumer = new QueueingConsumer(ch);
        ch.basicConsume(queueName, consumer);
        (new Thread() {
            public void run() {
                try {
                    while (true) {
                        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
                        String message = new String(delivery.getBody());
                        logger.info(
                                "---------------- Job Finish message received:" + message + " --------------");
                        synchronized (completedJobsFromPush) {
                            completedJobsFromPush.add(message);
                        }
                        ch.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
                    }
                } catch (Exception ex) {
                    logger.error("--------Cannot connect to a RabbitMQ Server--------", ex);
                }
            }

        }).start();
    } catch (Exception ex) {
        logger.error("Cannot connect to a RabbitMQ Server: ", ex);
        logger.info("------------- Push monitoring for HPC jobs is disabled -------------");
    }
}

From source file:org.apache.airavata.messaging.core.impl.RabbitMQConsumer.java

License:Apache License

private void createConnection() throws AiravataException {
    try {//from   w  w w  .j ava 2s.c o m
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri(url);
        connection = connectionFactory.newConnection();
        connection.addShutdownListener(new ShutdownListener() {
            public void shutdownCompleted(ShutdownSignalException cause) {
            }
        });
        log.info("connected to rabbitmq: " + connection + " for " + exchangeName);

        channel = connection.createChannel();
        channel.exchangeDeclare(exchangeName, "topic", false);

    } catch (Exception e) {
        String msg = "could not open channel for exchange " + exchangeName;
        log.error(msg);
        throw new AiravataException(msg, e);
    }
}

From source file:org.apache.airavata.messaging.core.impl.RabbitMQProcessConsumer.java

License:Apache License

private void createConnection() throws AiravataException {
    try {//  w  w w  . ja v  a 2s .c  o m
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri(url);
        connection = connectionFactory.newConnection();
        connection.addShutdownListener(new ShutdownListener() {
            public void shutdownCompleted(ShutdownSignalException cause) {
            }
        });
        log.info("connected to rabbitmq: " + connection + " for default");

        channel = connection.createChannel();
        //            channel.exchangeDeclare(taskLaunchExchangeName, "fanout");

    } catch (Exception e) {
        String msg = "could not open channel for exchange default";
        log.error(msg);
        throw new AiravataException(msg, e);
    }
}

From source file:org.apache.airavata.messaging.core.impl.RabbitMQProcessLaunchConsumer.java

License:Apache License

private void createConnection() throws AiravataException {
    try {/*from   ww w .  j  ava 2s . com*/
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri(url);
        connectionFactory.setAutomaticRecoveryEnabled(true);
        connection = connectionFactory.newConnection();
        connection.addShutdownListener(new ShutdownListener() {
            public void shutdownCompleted(ShutdownSignalException cause) {
            }
        });
        log.info("connected to rabbitmq: " + connection + " for " + taskLaunchExchangeName);

        channel = connection.createChannel();
        channel.basicQos(prefetchCount);

        //            channel.exchangeDeclare(taskLaunchExchangeName, "fanout");

    } catch (Exception e) {
        String msg = "could not open channel for exchange " + taskLaunchExchangeName;
        log.error(msg);
        throw new AiravataException(msg, e);
    }
}

From source file:org.apache.airavata.messaging.core.impl.RabbitMQProducer.java

License:Apache License

private Connection createConnection() throws IOException {
    try {//from  w w  w  .  j a  va 2 s . co  m
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri(url);
        connectionFactory.setAutomaticRecoveryEnabled(true);
        Connection connection = connectionFactory.newConnection();
        connection.addShutdownListener(new ShutdownListener() {
            public void shutdownCompleted(ShutdownSignalException cause) {
            }
        });
        log.info("connected to rabbitmq: " + connection + " for " + exchangeName);
        return connection;
    } catch (Exception e) {
        log.info("connection failed to rabbitmq: " + connection + " for " + exchangeName);
        return null;
    }
}

From source file:org.apache.airavata.messaging.core.impl.RabbitMQPublisher.java

License:Apache License

private void connect() throws AiravataException {
    try {/*from w  ww  . ja v  a2  s .co  m*/
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri(properties.getBrokerUrl());
        connectionFactory.setAutomaticRecoveryEnabled(properties.isAutoRecoveryEnable());
        connection = connectionFactory.newConnection();
        connection.addShutdownListener(new ShutdownListener() {
            public void shutdownCompleted(ShutdownSignalException cause) {
            }
        });
        log.info("connected to rabbitmq: " + connection + " for " + properties.getExchangeName());
        channel = connection.createChannel();
        if (properties.getPrefetchCount() > 0) {
            channel.basicQos(properties.getPrefetchCount());
        }

        if (properties.getExchangeName() != null) {
            channel.exchangeDeclare(properties.getExchangeName(), properties.getExchangeType(), true); //durable
        }
    } catch (Exception e) {
        String msg = "RabbitMQ connection issue for exchange : " + properties.getExchangeName();
        log.error(msg);
        throw new AiravataException(msg, e);
    }

}

From source file:org.apache.airavata.messaging.core.impl.RabbitMQStatusConsumer.java

License:Apache License

private void createConnection() throws AiravataException {
    try {//from  w w w .j a  v a  2 s. c  om
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri(url);
        connectionFactory.setAutomaticRecoveryEnabled(true);
        connection = connectionFactory.newConnection();
        connection.addShutdownListener(new ShutdownListener() {
            public void shutdownCompleted(ShutdownSignalException cause) {
            }
        });
        log.info("connected to rabbitmq: " + connection + " for " + exchangeName);

        channel = connection.createChannel();
        channel.basicQos(prefetchCount);
        channel.exchangeDeclare(exchangeName, EXCHANGE_TYPE, false);

    } catch (Exception e) {
        String msg = "could not open channel for exchange " + exchangeName;
        log.error(msg);
        throw new AiravataException(msg, e);
    }
}

From source file:org.apache.airavata.messaging.core.impl.RabbitMQSubscriber.java

License:Apache License

private void createConnection() throws AiravataException {
    try {//from w ww  . j a va2  s. co m
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri(properties.getBrokerUrl());
        connectionFactory.setAutomaticRecoveryEnabled(properties.isAutoRecoveryEnable());
        connection = connectionFactory.newConnection();
        addShutdownListener();
        log.info("connected to rabbitmq: " + connection + " for " + properties.getExchangeName());
        channel = connection.createChannel();
        channel.basicQos(properties.getPrefetchCount());
        channel.exchangeDeclare(properties.getExchangeName(), properties.getExchangeType(), true); // durable
    } catch (Exception e) {
        String msg = "could not open channel for exchange " + properties.getExchangeName();
        log.error(msg);
        throw new AiravataException(msg, e);
    }
}