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:tugas4pat.RabbitMQClient.java

public RabbitMQClient() throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(authHost);
    factory.setUsername(authUsername);// w ww  . j  a  v  a 2 s  . c o  m
    factory.setPassword(authPassword);

    connection = factory.newConnection();
    channel = connection.createChannel();
    channel.exchangeDeclare(EXCHANGE_CHANNEL_NAME, "direct");
    channel.exchangeDeclare(EXCHANGE_USER_NAME, "fanout");

    queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, EXCHANGE_CHANNEL_NAME, "");

    queueUser = channel.queueDeclare().getQueue();
    channel.queueBind(queueUser, EXCHANGE_USER_NAME, "");

    Consumer messageConsumer = new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String message = new String(body, "UTF-8");
            System.out.println("[x] Received '" + message + "'");
        }
    };
    channel.basicConsume(queueName, true, messageConsumer);

    Consumer userConsumer = new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String message = new String(body, "UTF-8");
            switch (message.substring(0, 1)) {
            case "+":
                userList.add(message.substring(1));
                System.out.println("Add " + message.substring(1) + " to list");
                break;
            case "-":
                userList.remove(userList.indexOf(message.substring(1)));
                System.out.println("Remove " + message.substring(1) + " from list");
                break;
            }
            System.out.println("[x] New user : '" + message.substring(1) + "'");
        }
    };
    channel.basicConsume(queueUser, true, userConsumer);

}

From source file:uk.ac.sanger.cgp.wwdocker.messages.Messaging.java

License:Open Source License

public Connection getRmqConn() throws IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(config.getString("rabbit_host"));
    factory.setPort(config.getInt("rabbit_port", 5672));
    factory.setUsername(config.getString("rabbit_user"));
    factory.setPassword(config.getString("rabbit_pw"));
    factory.setNetworkRecoveryInterval(60000); // retry every 60 seconds
    factory.setAutomaticRecoveryEnabled(true);
    return factory.newConnection();
}

From source file:uk.ac.soton.itinnovation.experimedia.arch.ecc.amqpAPI.impl.amqp.AMQPConnectionFactory.java

public void connectToAMQPHost() throws Exception {
    // Safety first
    if (amqpHostIP == null)
        throw new Exception("AMQP Host IP not correct");
    if (amqpConnection != null)
        throw new Exception("Already connected to host");

    ConnectionFactory amqpFactory = new ConnectionFactory();
    amqpFactory.setHost(amqpHostIP.getHostAddress());
    amqpFactory.setPort(amqpPortNumber);

    // Select login credentials (if available)
    String selUserName = "guest";
    String selPassword = "guest";

    if (userName != null && userPass != null) {
        selUserName = userName;/*from w w w.ja  va  2  s  . c  om*/
        selPassword = userPass;
    }

    // Set login details
    amqpFactory.setUsername(selUserName);
    amqpFactory.setPassword(selPassword);
    factoryLog.info("Logging into RabbitMQ as \'" + userName + "\'");

    // Set heartbeat rate, if available
    if (heartbeatRate != null)
        amqpFactory.setRequestedHeartbeat(heartbeatRate);

    // Execute log-in
    try {
        amqpConnection = amqpFactory.newConnection();
    } catch (Exception ex) {
        throw new Exception("Could not create AMQP host connection", ex);
    }
}

From source file:uk.ac.soton.itinnovation.experimedia.arch.ecc.amqpAPI.impl.amqp.AMQPConnectionFactory.java

public void connectToAMQPSSLHost() throws Exception {
    // Safety first
    if (amqpHostIP == null)
        throw new Exception("AMQP Host IP not correct");
    if (amqpConnection != null)
        throw new Exception("Already connected to host");

    ConnectionFactory amqpFactory = new ConnectionFactory();
    amqpFactory.setHost(amqpHostIP.getHostAddress());
    amqpFactory.setPort(amqpPortNumber);
    amqpFactory.useSslProtocol();/*from  w w w  .j a v a2s .  c o m*/

    try {
        amqpConnection = amqpFactory.newConnection();
    } catch (Exception ex) {
        throw new Exception("Could not create AMQP host SSL connection: ", ex);
    }
}

From source file:uk.ac.soton.itinnovation.experimedia.arch.ecc.amqpAPI.impl.amqp.AMQPConnectionFactory.java

public void connectToVerifiedAMQPHost(InputStream keystore, String password) throws Exception {
    // Safety first
    if (amqpHostIP == null)
        throw new Exception("AMQP Host IP not correct");
    if (amqpConnection != null)
        throw new Exception("Already connected to host");
    if (password == null)
        throw new Exception("Password is null");

    char[] trustPassphrase = password.toCharArray();
    KeyStore tks = KeyStore.getInstance("JKS");
    try {//from  w w  w  .j  a  va  2  s.c  om
        tks.load(keystore, trustPassphrase);
    } catch (Exception ex) {
        factoryLog.error("Had problems loading keystore: " + ex.getMessage());
        throw ex;
    }

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

    SSLContext sslContext = SSLContext.getInstance("SSLv3");
    sslContext.init(null, tmf.getTrustManagers(), null);

    ConnectionFactory amqpFactory = new ConnectionFactory();
    amqpFactory.setHost(amqpHostIP.getHostAddress());
    amqpFactory.setPort(amqpPortNumber);
    amqpFactory.useSslProtocol(sslContext);

    try {
        amqpConnection = amqpFactory.newConnection();
    } catch (IOException ioe) {
        throw new Exception("Could not create secure AMQP host connection", ioe);
    }
}

From source file:uk.org.openeyes.oink.hl7v2.test.Hl7ITSupport.java

License:Open Source License

protected static ConnectionFactory initRabbit(String host, int port, String uname, String pwd, String vhost) {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(host);
    factory.setPort(port);//from  ww  w.ja va 2  s .c om
    factory.setUsername(uname);
    factory.setPassword(pwd);
    factory.setVirtualHost(vhost);
    return factory;
}

From source file:uk.trainwatch.rabbitmq.RabbitConnection.java

License:Apache License

private void createConnection() throws IOException {
    if (connection != null) {
        close();/*  w w  w . j ava 2s  .c om*/
    }

    LOG.log(Level.FINE, "building connection");
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername(username);
    factory.setPassword(password);
    if (virtualHost != null) {
        factory.setVirtualHost(virtualHost);
    }
    factory.setHost(host);
    if (portNumber > 0) {
        factory.setPort(portNumber);
    }

    channels.clear();

    LOG.log(Level.FINE, "creating connection");
    try {
        connection = factory.newConnection();
    } catch (TimeoutException ex) {
        throw new IOException(ex);
    }
}

From source file:user.Client.java

public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("rabbitmq.akhfa.me");
    factory.setUsername(username);//  w  ww.  ja  v  a  2 s .  co  m
    factory.setPassword(password);
    factory.setVirtualHost("pat");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    ArrayList<String> daftarNick = getAllQueues();

    String nick = "";

    while (true) {
        Scanner in = new Scanner(System.in);

        System.out.print("Please enter your command: ");
        String command = in.nextLine();

        String[] com = command.split(" ", 2);
        try {
            switch (com[0]) {
            case "/NICK":
                if (!nick.equals("")) {
                    System.out.println("You have registered with nickname: " + nick);
                } else {
                    if (!daftarNick.contains(com[1])) {
                        channel.queueDeclare(com[1], false, false, true, null);
                        nick = com[1];
                        System.out.println("Your nickname is " + nick);

                        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

                        Consumer consumer = new DefaultConsumer(channel) {
                            @Override
                            public void handleDelivery(String consumerTag, Envelope envelope,
                                    AMQP.BasicProperties properties, byte[] body) throws IOException {
                                String message = new String(body, "UTF-8");
                                filterChannel(message);
                            }
                        };
                        channel.basicConsume(nick, true, consumer);
                    } else {
                        System.out.println("Nickname exists.");
                    }
                }
                break;
            case "/JOIN":
                channel.exchangeDeclare(com[1], "fanout", false, false, false, null);
                channel.queueBind(nick, com[1], "");
                System.out.println("You have successfully join " + com[1]);
                break;
            case "/LEAVE":
                channel.queueUnbind(nick, com[1], "");
                System.out.println("Leave " + com[1]);
                break;
            case "/EXIT":
                System.out.println("bye bye...  :D");
                System.exit(0);
            default:
                String message = nick + ' ' + command;
                channel.basicPublish(com[0].substring(1), "", null, message.getBytes("UTF-8"));
                break;
            }
        } catch (Exception e) {
            if (command.compareTo("/NICK") == 0) {
                //random nick
                String random;

                if (!nick.equals("")) {
                    System.out.println("You have registered with nickname: " + nick);
                } else {
                    do {
                        random = randomNick();
                    } while (daftarNick.contains(random));

                    nick = random;

                    channel.queueDeclare(random, false, false, true, null);
                    System.out.println("Your nickname is " + random);

                    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

                    Consumer consumer = new DefaultConsumer(channel) {
                        @Override
                        public void handleDelivery(String consumerTag, Envelope envelope,
                                AMQP.BasicProperties properties, byte[] body) throws IOException {
                            String message = new String(body, "UTF-8");
                            filterChannel(message);
                        }
                    };
                    channel.basicConsume(nick, true, consumer);
                }

            } else if ((command.compareTo("/JOIN") == 0) || (command.compareTo("/LEAVE") == 0)) {
                //error
                System.out.println("Please enter channel name!");
            } else if (command.charAt(0) == '@') {
                System.out.println("Please enter your command for the channel.");
            } else {
                System.out.println("Invalid command.");
            }
        }
    }
}

From source file:v2.APAdmin.java

public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("datdb.cphbusiness.dk");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

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

    channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);

    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    Consumer consumer = new DefaultConsumer(channel) {
        @Override//w w w  . j a  v a2 s  .  co m
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String message = new String(body, "UTF-8");
            System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + message + "'");
            //REPLY HERE
            String response = "You are in, breh";
            BasicProperties replyProps = new BasicProperties.Builder()
                    .correlationId(properties.getCorrelationId()).build();
            channel.basicPublish("", properties.getReplyTo(), replyProps, response.getBytes());
        }
    };
    channel.basicConsume(queueName, true, consumer);
}

From source file:v2.PBAAdmin.java

public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("datdb.cphbusiness.dk");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

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

    channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);

    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    Consumer consumer = new DefaultConsumer(channel) {
        @Override//from  w  w  w.j  ava 2  s. com
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String message = new String(body, "UTF-8");
            System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + message + "'");
        }
    };
    channel.basicConsume(queueName, true, consumer);
}