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:rabbitmqapp.Recv.java

public static void main(String... args) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    String uri = "amqp://ytsoedex:Qu2LCiBJ5x9fhRUyLYkMhJqsURJ9dkSP@chicken.rmq.cloudamqp.com/ytsoedex";
    factory.setUri(uri);

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

    channel.queueDeclare(QUENAME, false, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    Consumer consumer = new DefaultConsumer(channel) {
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            System.out.println(String.format("The consumer tag is %s", consumerTag));
            ProductOrder message = (ProductOrder) RabbitUtility.getObject(body);

            //String message = new String(body, "UTF-8");
            System.out.println(" [x] Received name'" + message.getName() + "'");
            System.out.println(" [x] Received price'" + message.getPrice() + "'");
        }//from w  ww  . j  a v  a2  s .  c o m
    };
    channel.basicConsume(QUENAME, true, consumer);
}

From source file:rapture.exchange.rabbitmq.RabbitExchangeHandler.java

License:Open Source License

@Override
public synchronized void setConfig(Map<String, String> config) {
    // Attempt to bind to RabbitMQ
    ConnectionFactory factory = new ConnectionFactory();
    logger.info(Messages.getString("RabbitExchangeHandler.config")); //$NON-NLS-1$
    try {//  w  ww.ja v a  2  s  . c o  m
        String uri = MultiValueConfigLoader.getConfig("RABBITMQ-" //$NON-NLS-1$
                + instanceName);
        if (uri == null || uri.isEmpty()) {
            uri = "amqp://guest:guest@localhost:5672/%2f"; //$NON-NLS-1$
        }
        factory.setUri(uri);
        factory.setAutomaticRecoveryEnabled(true);
        logger.debug(Messages.getString("RabbitExchangeHandler.creatingChannel")); //$NON-NLS-1$
        connection = factory.newConnection();
        connection.addShutdownListener(new ShutdownListener() {
            @Override
            public void shutdownCompleted(ShutdownSignalException cause) {
                // This should theoretically be called when we disconnect
                // from RabbitMQ, but due to a bug in the client library it
                // instead gets invoked
                // when reconnecting. This may change in future versions of
                // amqp-client so may need to revisit
                logger.info("Reconnected to RabbitMQ");

            }
        });
        logger.debug(Messages.getString("RabbitExchangeHandler.connectionMade")); //$NON-NLS-1$
        channel = connection.createChannel();
        channel.addShutdownListener(new ShutdownListener() {
            @Override
            public void shutdownCompleted(ShutdownSignalException cause) {
                logger.info("Disconnected from RabbitMQ. Cause :" + cause.getMessage());
                logger.debug(ExceptionToString.format(cause));
            }
        });
        logger.debug(Messages.getString("RabbitExchangeHandler.channelCreated")); //$NON-NLS-1$
        channel.basicQos(100);
        channel.addReturnListener(new ReturnListener() {

            @Override
            public void handleReturn(int replyCode, String replyText, String exchange, String routingKey,
                    AMQP.BasicProperties properties, byte[] body) throws IOException {
                logger.debug(
                        String.format(Messages.getString("RabbitExchangeHandler.returnListener"), replyCode, //$NON-NLS-1$
                                replyText));
            }

        });
        channel.addFlowListener(new FlowListener() {

            @Override
            public void handleFlow(boolean active) throws IOException {
                logger.debug(String.format(Messages.getString("RabbitExchangeHandler.Flow"), active)); //$NON-NLS-1$
            }

        });

        replyQueueName = channel.queueDeclare().getQueue();
        logger.info("RPC reply queue is " + replyQueueName);
        consumer = new QueueingConsumer(channel);
        channel.basicConsume(replyQueueName, true, consumer);

    } catch (Exception e) {
        String message = Messages.getString("RabbitExchangeHandler.noConnect");
        throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_INTERNAL_ERROR, message, e);
    }
}

From source file:rmq.sender.impl.MQSender.java

License:Apache License

@Override
public void start() {
    SSLContext c = null;//from ww  w  . j  av a2  s  . c o m
    try {
        char[] pass = "changeit".toCharArray();
        KeyStore tks = KeyStore.getInstance("JKS");
        tks.load(new FileInputStream(
                "/root/test-project/topology-current/" + "/src/main/resources/client/client_cacerts.jks"),
                pass);

        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
        tmf.init(tks);

        c = SSLContext.getInstance("TLSv1.2");
        c.init(null, tmf.getTrustManagers(), null);
    } catch (Exception e) {
        log.error(E_CREATE_CHAN, e);
    }
    ConnectionFactory factory = new ConnectionFactory();
    factory.setAutomaticRecoveryEnabled(true);
    factory.setNetworkRecoveryInterval(RECOVERY_INTERVAL);
    factory.useSslProtocol(c);
    try {
        factory.setUri(url);
        if (executorService != null) {
            conn = factory.newConnection(executorService);
        } else {
            conn = factory.newConnection();
        }
        channel = conn.createChannel();
        channel.exchangeDeclare(exchangeName, "topic", true);
        /*
         * Setting the following parameters to queue
         * durable    - true
         * exclusive  - false
         * autoDelete - false
         * arguments  - null
         */
        channel.queueDeclare(this.queueName, true, false, true, null);
        channel.queueBind(queueName, exchangeName, routingKey);
    } catch (Exception e) {
        log.error(E_CREATE_CHAN, e);
    }
    log.info("Connection started");
}

From source file:sonata.kernel.vimadaptor.messaging.RabbitMqConsumer.java

License:Open Source License

@Override
public void connectToBus() {
    Properties brokerConfig = parseConfigFile();
    Logger.info("Connecting to broker...");
    ConnectionFactory cf = new ConnectionFactory();
    if (!brokerConfig.containsKey("broker_url") || !brokerConfig.containsKey("exchange")) {
        Logger.error("Missing broker url configuration.");
        System.exit(1);/*  w w w . j  a va 2s  .c o m*/
    }
    try {

        Logger.info("Connecting to: " + brokerConfig.getProperty("broker_url"));
        cf.setUri(brokerConfig.getProperty("broker_url"));
        connection = cf.newConnection();
        channel = connection.createChannel();
        String exchangeName = brokerConfig.getProperty("exchange");
        channel.exchangeDeclare(exchangeName, "topic");
        queueName = exchangeName + "." + "InfraAbstract";
        channel.queueDeclare(queueName, true, false, false, null);
        Logger.info("Binding queue to topics...");

        channel.queueBind(queueName, exchangeName, "platform.management.plugin.register");
        Logger.info("Bound to topic \"platform.platform.management.plugin.register\"");

        channel.queueBind(queueName, exchangeName, "platform.management.plugin.deregister");
        Logger.info("Bound to topic \"platform.platform.management.plugin.deregister\"");

        channel.queueBind(queueName, exchangeName, "infrastructure.#");
        Logger.info("[northbound] RabbitMqConsumer - bound to topic \"infrastructure.#\"");

        consumer = new AdaptorDefaultConsumer(channel, this);
    } catch (TimeoutException e) {
        Logger.error(e.getMessage(), e);
    } catch (KeyManagementException e) {
        Logger.error(e.getMessage(), e);
    } catch (NoSuchAlgorithmException e) {
        Logger.error(e.getMessage(), e);
    } catch (URISyntaxException e) {
        Logger.error(e.getMessage(), e);
    } catch (IOException e) {
        Logger.error(e.getMessage(), e);
    }

}

From source file:sonata.kernel.vimadaptor.messaging.RabbitMqHelperSingleton.java

License:Open Source License

private RabbitMqHelperSingleton() {
    Properties brokerConfig = parseConfigFile();
    Logger.info("Connecting to broker...");
    ConnectionFactory cf = new ConnectionFactory();
    if (!brokerConfig.containsKey("broker_url") || !brokerConfig.containsKey("exchange")) {
        Logger.error("Missing broker url configuration.");
        System.exit(1);// w  w w  .  j  a v a 2s  .c o  m
    }
    try {

        Logger.info("Connecting to: " + brokerConfig.getProperty("broker_url"));
        cf.setUri(brokerConfig.getProperty("broker_url"));
        connection = cf.newConnection();
        channel = connection.createChannel();
        exchangeName = brokerConfig.getProperty("exchange");
        channel.exchangeDeclare(exchangeName, "topic");
        queueName = exchangeName + "." + "InfraAbstract";
        channel.queueDeclare(queueName, true, false, false, null);
        Logger.info("Binding queue to topics...");

        channel.queueBind(queueName, exchangeName, "platform.management.plugin.register");
        Logger.info("Bound to topic \"platform.platform.management.plugin.register\"");

        channel.queueBind(queueName, exchangeName, "platform.management.plugin.deregister");
        Logger.info("Bound to topic \"platform.platform.management.plugin.deregister\"");

        channel.queueBind(queueName, exchangeName, "infrastructure.#");
        Logger.info("[northbound] RabbitMqConsumer - bound to topic \"infrastructure.#\"");

    } catch (TimeoutException e) {
        Logger.error(e.getMessage(), e);
    } catch (KeyManagementException e) {
        Logger.error(e.getMessage(), e);
    } catch (NoSuchAlgorithmException e) {
        Logger.error(e.getMessage(), e);
    } catch (URISyntaxException e) {
        Logger.error(e.getMessage(), e);
    } catch (IOException e) {
        Logger.error(e.getMessage(), e);
    }

}

From source file:sonata.kernel.vimadaptor.messaging.RabbitMqProducer.java

License:Open Source License

@Override
public void connectToBus() {
    brokerConfig = parseConfigFile();/*from   ww  w .  j  a  v  a2  s .  c om*/

    ConnectionFactory cf = new ConnectionFactory();
    if (!brokerConfig.containsKey("broker_url") || !brokerConfig.containsKey("exchange")) {
        Logger.error("Missing broker url configuration.");
        System.exit(1);
    }

    try {
        cf.setUri(brokerConfig.getProperty("broker_url"));
    } catch (KeyManagementException e) {
        Logger.error(e.getMessage(), e);
    } catch (NoSuchAlgorithmException e) {
        Logger.error(e.getMessage(), e);
    } catch (URISyntaxException e) {
        Logger.error(e.getMessage(), e);
    }

    try {
        connection = cf.newConnection();
    } catch (IOException e) {
        Logger.error(e.getMessage(), e);
    } catch (TimeoutException e) {
        Logger.error(e.getMessage(), e);
    }
}

From source file:sonata.kernel.WimAdaptor.messaging.RabbitMqConsumer.java

License:Open Source License

@Override
public void connectToBus() {
    Properties brokerConfig = parseConfigFile();
    Logger.info("[northbound] RabbitMqConsumer - connecting to broker...");
    ConnectionFactory cf = new ConnectionFactory();
    if (!brokerConfig.containsKey("broker_url") || !brokerConfig.containsKey("exchange")) {
        System.err.println("Missing broker url configuration.");
        System.exit(1);// ww  w .j av  a 2s.c o  m
    }
    try {

        Logger.info("RabbitMqConsumer - connecting to: " + brokerConfig.getProperty("broker_url"));
        cf.setUri(brokerConfig.getProperty("broker_url"));
        connection = cf.newConnection();
        channel = connection.createChannel();
        String exchangeName = brokerConfig.getProperty("exchange");
        channel.exchangeDeclare(exchangeName, "topic");
        queueName = exchangeName + "." + "WimAdaptor";
        channel.queueDeclare(queueName, true, false, false, null);
        Logger.info("[northbound] RabbitMqConsumer - binding queue to topics...");
        channel.queueBind(queueName, exchangeName, "platform.management.plugin.register");
        Logger.info("[northbound] RabbitMqConsumer - bound to topic "
                + "\"platform.platform.management.plugin.register\"");
        channel.queueBind(queueName, exchangeName, "platform.management.plugin.deregister");
        Logger.info("[northbound] RabbitMqConsumer - bound to topic "
                + "\"platform.platform.management.plugin.deregister\"");
        channel.queueBind(queueName, exchangeName, "infrastructure.wan.#");
        Logger.info("[northbound] RabbitMqConsumer - bound to topic \"infrastructure.wan.#\"");
        consumer = new AdaptorDefaultConsumer(channel, this);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (TimeoutException e) {
        e.printStackTrace();
    } catch (KeyManagementException e1) {
        e1.printStackTrace();
    } catch (NoSuchAlgorithmException e1) {
        e1.printStackTrace();
    } catch (URISyntaxException e1) {
        e1.printStackTrace();
    }

}

From source file:sonata.kernel.WimAdaptor.messaging.RabbitMqHelperSingleton.java

License:Open Source License

private RabbitMqHelperSingleton() {
    Properties brokerConfig = parseConfigFile();
    Logger.info("Connecting to broker...");
    ConnectionFactory cf = new ConnectionFactory();
    if (!brokerConfig.containsKey("broker_url") || !brokerConfig.containsKey("exchange")) {
        Logger.error("Missing broker url configuration.");
        System.exit(1);//from   w  w w  .  j a v  a2 s  .co m
    }
    try {

        Logger.info("Connecting to: " + brokerConfig.getProperty("broker_url"));
        cf.setUri(brokerConfig.getProperty("broker_url"));
        connection = cf.newConnection();
        channel = connection.createChannel();
        exchangeName = brokerConfig.getProperty("exchange");
        channel.exchangeDeclare(exchangeName, "topic");
        queueName = exchangeName + "." + "WimAdaptor";
        channel.queueDeclare(queueName, true, false, false, null);
        Logger.info("Binding queue to topics...");

        channel.queueBind(queueName, exchangeName, "platform.management.plugin.register");
        Logger.info("Bound to topic \"platform.platform.management.plugin.register\"");

        channel.queueBind(queueName, exchangeName, "platform.management.plugin.deregister");
        Logger.info("Bound to topic \"platform.platform.management.plugin.deregister\"");

        channel.queueBind(queueName, exchangeName, "infrastructure.#.wan.#");
        Logger.info("[northbound] RabbitMqConsumer - bound to topic \"infrastructure.#.wan.#\"");

    } catch (TimeoutException e) {
        Logger.error(e.getMessage(), e);
    } catch (KeyManagementException e) {
        Logger.error(e.getMessage(), e);
    } catch (NoSuchAlgorithmException e) {
        Logger.error(e.getMessage(), e);
    } catch (URISyntaxException e) {
        Logger.error(e.getMessage(), e);
    } catch (IOException e) {
        Logger.error(e.getMessage(), e);
    }

}

From source file:sonata.kernel.WimAdaptor.messaging.RabbitMqProducer.java

License:Open Source License

@Override
public void connectToBus() {
    brokerConfig = parseConfigFile();// w  ww  .  j  a va  2s  .  c o m

    ConnectionFactory cf = new ConnectionFactory();
    if (!brokerConfig.containsKey("broker_url") || !brokerConfig.containsKey("exchange")) {
        Logger.error("Missing broker url configuration.");
        System.exit(1);
    }

    try {
        cf.setUri(brokerConfig.getProperty("broker_url"));
    } catch (KeyManagementException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (NoSuchAlgorithmException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (URISyntaxException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        connection = cf.newConnection();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TimeoutException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:sqlfire.callbacks.RabbitAsyncEventListener.java

License:Open Source License

/**
 * Creates a connection RabbitMQ that will be used to queue modified rows
 *
 * @param   rabbitURI   will take the form of "amqp://guest:guest@localhost:5672"
 *//*from  www. j  a v  a  2s .com*/
public void init(String rabbitURI) {
    ConnectionFactory factory = new ConnectionFactory();

    /**
     * Throw a RunTimeException if connecting to RabbitMQ fails for any reason
     */
    try {
        factory.setUri(rabbitURI);
    } catch (java.net.URISyntaxException e) {
        throw new RuntimeException(e);
    } catch (java.security.NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (java.security.KeyManagementException e) {
        throw new RuntimeException(e);
    }

    try {
        this.connection = factory.newConnection();
        this.channel = this.connection.createChannel();
        this.channel.exchangeDeclare(EXCHANGE_NAME, "topic");
    } catch (java.io.IOException e) {
        throw new RuntimeException(e);
    }

    return;
}