Example usage for com.rabbitmq.client ConnectionFactory setHost

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

Introduction

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

Prototype

public void setHost(String host) 

Source Link

Usage

From source file:ch.icclab.cyclops.consume.RabbitMQListener.java

License:Open Source License

/**
 * Will return channel for RabbitMQ connection
 * @return channel reference or null//from   w  w w  .  j  a v  a2  s  . c  o m
 */
private Channel getChannel() {
    // connect to the RabbitMQ based on settings from Load
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername(credentials.getConsumerUsername());
    factory.setPassword(credentials.getConsumerPassword());
    factory.setHost(credentials.getConsumerHost());
    factory.setPort(credentials.getConsumerPort());
    factory.setVirtualHost(credentials.getConsumerVirtualHost());
    factory.setAutomaticRecoveryEnabled(true);

    Channel chan;

    try {
        // create new connection
        connection = factory.newConnection();

        // create/connect to the channel
        chan = connection.createChannel();

        logger.trace(String.format("RabbitMQ Consumer connected to %s:%d", credentials.getConsumerHost(),
                credentials.getConsumerPort()));

    } catch (Exception e) {
        logger.error(String.format("RabbitMQ Consumer couldn't be created: %s", e.getMessage()));
        connection = null;
        chan = null;
    }

    // return channel reference, or null
    return chan;
}

From source file:ch.icclab.cyclops.publish.RabbitMQPublisher.java

License:Open Source License

/**
 * Initialise connection to RabbitMQ/*ww w.j  ava  2  s.  c o  m*/
 * @return status
 */
private Boolean initialiseConnection() {
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername(credentials.getPublisherUsername());
        factory.setPassword(credentials.getPublisherPassword());
        factory.setHost(credentials.getPublisherHost());
        factory.setPort(credentials.getPublisherPort());
        factory.setVirtualHost(credentials.getPublisherVirtualHost());
        factory.setAutomaticRecoveryEnabled(true);

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

        // declare exchange to be used (we want it to be durable and based on routing key)
        channel.exchangeDeclare(credentials.getPublisherDispatchExchange(), "direct", true);
        channel.exchangeDeclare(credentials.getPublisherBroadcastExchange(), "fanout", true);

        logger.trace(String.format("RabbitMQ Publisher connected to %s:%d", credentials.getPublisherHost(),
                credentials.getPublisherPort()));
        logger.trace(String.format(
                "RabbitMQ Publisher will dispatch to \"%s\" and broadcast to \"%s\" exchanges",
                credentials.getPublisherDispatchExchange(), credentials.getPublisherBroadcastExchange()));

        return true;
    } catch (Exception e) {
        logger.error(String.format("RabbitMQ Publisher couldn't be created: %s", e.getMessage()));
        return false;
    }
}

From source file:ch.icclab.cyclops.rabbitmq.RabbitMQAbstract.java

License:Open Source License

/**
 * Will return channel for RabbitMQ connection
 * @return channel reference or null// w  w w.  j  av  a  2 s  .  c o m
 */
protected Channel getChannel() {
    // connect to the RabbitMQ based on settings from Load
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername(credentials.getRabbitMQUsername());
    factory.setPassword(credentials.getRabbitMQPassword());
    factory.setHost(credentials.getRabbitMQHost());
    factory.setPort(credentials.getRabbitMQPort());
    factory.setVirtualHost(credentials.getRabbitMQVirtualHost());

    Channel chan;

    try {

        logger.trace("Creating connection to RabbitMQ for host: " + credentials.getRabbitMQHost()
                + " and port: " + credentials.getRabbitMQPort());

        // create new connection
        connection = factory.newConnection();

        logger.trace("Creating and connecting to RabbitMQ channel");

        // create/connect to the channel
        chan = connection.createChannel();

    } catch (Exception ex) {
        logger.error("Couldn't start Rabbit MQ: " + ex.getMessage());
        connection = null;
        chan = null;
    }

    // return channel reference, or null
    return chan;
}

From source file:ch.icclab.cyclops.usecases.mcn.rabbitMQClient.McnRabbitMQClient.java

License:Open Source License

/**
 * Will return channel for RabbitMQ connection
 *
 * @return channel reference or null/*  w  w  w.  ja  va2 s . c om*/
 */
private Channel getChannel() {
    // connect to the RabbitMQ based on settings from Load
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername(rabbitMQSettings.getRabbitMQUsername());
    factory.setPassword(rabbitMQSettings.getRabbitMQPassword());
    factory.setVirtualHost(rabbitMQSettings.getRabbitMQVirtualHost());
    factory.setHost(rabbitMQSettings.getRabbitMQHost());
    factory.setPort(rabbitMQSettings.getRabbitMQPort());
    try {
        // create new connection
        connection = factory.newConnection();

        // create/connect to the channel
        channel = connection.createChannel();
        channel.queueDeclare(queueName, true, false, false, null);
    } catch (Exception ex) {
        logger.error("Couldn't start Rabbit MQ: " + ex.getMessage());
        ex.printStackTrace();
    }

    // return channel reference, or null
    return channel;
}

From source file:Colas.Colas.java

private String reciver(String enviar) {
    try {//from   w  ww .j  a  va2  s  .  co 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";

}

From source file:Colas.Colas.java

private void sender(String string) {
    try {/*from  w  w w  .  jav a  2s.  c  o m*/
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        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, string.getBytes());
        System.out.println(" [x] Sent '" + string + "'");
        channel.close();
        connection.close();
    } catch (Exception e) {
        System.out.println("Error enviando ");
        e.printStackTrace();
    }
}

From source file:Colas.Colas.java

@Override
public void run() {
    try {/*from w w  w.ja  v  a2 s . c o m*/
        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 + "'");
            String[] resp = message.split(";");
            String p = resp[0];
            if (p.equals("Q1")) {
                pregunta1(resp[1]);
            }
            if (p.equals("Q2")) {
                pregunta2(resp[1]);
            }
            if (p.equals("Q3")) {
                pregunta3(resp[1]);
            }
            if (p.equals("Q4")) {
                pregunta4(resp[1]);
            }

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

From source file:com.adr.data.rabbitmq.RabbitServer.java

License:Apache License

protected Connection connect() throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(host);
    factory.setPort(port);/*from w  w  w .j a  v  a  2 s  .  c o m*/
    return factory.newConnection();
}

From source file:com.adr.data.testlinks.DataQueryLinkMQ.java

License:Apache License

public DataQueryLinkMQ(String host, String dataexchange, String queryexchange) {

    this.dataexchange = dataexchange;
    this.queryexchange = queryexchange;

    try {/*from   w ww . ja  va2 s  . c o  m*/
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(host);
        connection = factory.newConnection();
    } catch (IOException | TimeoutException ex) {
        LOG.log(Level.SEVERE, null, ex);
        throw new RuntimeException(ex);
    }
}

From source file:com.adr.data.testlinks.DataQueryLinkMQAsync.java

License:Apache License

public DataQueryLinkMQAsync(String host, String dataexchange, String queryexchange) {

    this.dataexchange = dataexchange;
    this.queryexchange = queryexchange;

    try {//  ww  w  .j  a v  a  2s.co  m
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(host);
        connection = factory.newConnection();
    } catch (IOException | TimeoutException ex) {
        LOG.log(Level.SEVERE, null, ex);
        throw new RuntimeException(ex);
    }
}