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:pro.foundev.messaging.RabbitMQReceiver.java

License:Apache License

@Override
public void onStart() {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(host);
    factory = new ConnectionFactory();
    try {/*w  w  w .  java  2s .  com*/
        connection = factory.newConnection();
        channel = connection.createChannel();

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

            }
        };
        channel.basicConsume(queueName, true, consumer);

    } catch (IOException e) {
        restart("error connecting to message queue", e);
    }
}

From source file:pt.ua.ies.ControllerView.java

@PostConstruct
public void init() {
    System.out.println("init");
    recive = "";/*  w w w  .  j  av  a 2  s.  c  o m*/
    reciveBeforeRefresh = recive;
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");

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

        channel.exchangeDeclare(DisplayMessage.EXCHANGE_NAME, "fanout");
        String queueName = channel.queueDeclare().getQueue();
        channel.queueBind(queueName, DisplayMessage.EXCHANGE_NAME, "");

        MessageConsumer m = new MessageConsumer(channel, this);

        channel.basicConsume(queueName, true, m);
    } catch (Exception ex) {
        Logger.getLogger(ControllerView.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:pt.ua.ies.Main.java

public static void main(String[] args) {
    try {//from   w  ww .  j  a  v  a  2 s .co m
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");

        String message = "Random Message " + new Random().nextInt(1000);
        channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");

        /**
         * Close the channel and the connection
         */
        channel.close();
        connection.close();
    } catch (Exception ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:pubsub.RecieveLogs.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, "fanout");
    String queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, EXCHANGE_NAME, "");

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

    Consumer consumer = new DefaultConsumer(channel) {
        @Override//from ww  w  . jav  a 2  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 '" + message + "'");
        }
    };
    channel.basicConsume(queueName, true, consumer);
}

From source file:rabbitchat.RabbitChatClient.java

public RabbitChatClient(String host) throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(host);
    connection = factory.newConnection();
    channel = connection.createChannel();

    channel.exchangeDeclare(EXCHANGE_LOGS, "fanout");
    channel.exchangeDeclare(EXCHANGE_USERS, "fanout");
    channel.exchangeDeclare(EXCHANGE_CHANNELS, "direct");

    String logsQueue = channel.queueDeclare().getQueue();
    channel.queueBind(logsQueue, EXCHANGE_LOGS, "");

    String usersQueue = channel.queueDeclare().getQueue();
    channel.queueBind(usersQueue, EXCHANGE_USERS, "");

    Consumer logsConsumer = new DefaultConsumer(channel) {
        @Override/*from www.  ja  v a2  s .  com*/
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String sender = new String(body, "UTF-8");
            sender = sender.substring(0, sender.indexOf(": "));

            String message = new String(body, "UTF-8");
            message = message.substring(message.indexOf(": ") + 2);

            if (!sender.equals(username)) {
                System.out.println(message);
            }
        }
    };
    channel.basicConsume(logsQueue, true, logsConsumer);

    Consumer usersConsumer = new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String sign = new String(body, "UTF-8").substring(0, 1);
            String name = new String(body, "UTF-8").substring(1);

            switch (sign) {
            case "+":
                users.add(name);
                break;
            case "-":
                users.remove(name);
                break;
            }
        }
    };
    channel.basicConsume(usersQueue, true, usersConsumer);
}

From source file:rabbitirc.RabbitIRC.java

public static void main(String[] argv) throws java.io.IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Random rnd = new Random();
    int indeks = rnd.nextInt(usernamelist.length);
    String Randusername = usernamelist[indeks] + rnd.nextInt(100);

    user = Randusername;//from  ww  w. ja va 2s .co  m
    channellist.add("lounge");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    String queueName = channel.queueDeclare().getQueue();
    channel.exchangeDeclare(EXCHANGE_NAME, "direct");

    Scanner sc = new Scanner(System.in);

    channel.queueBind(queueName, EXCHANGE_NAME, "lounge");
    channellist.add("lounge");
    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");
            System.out.println(" [x] Received :'" + message + "'");
        }
    };
    System.out.println(" Welcome " + user + " to Channel lounge");
    System.out.println(" Queries: ");
    System.out.println(" 1. /NICK <Username>            : Change username ");
    System.out.println(" 2. /JOIN <Channel Name>        : Join Channel");
    System.out.println(" 3. @<Channel Name> <Message>   : Send message to Spesific Channel");
    System.out.println(" 4. /LEAVE <Channel Name>       : Leave Channel");
    System.out.println(" 5. <Random text>               : BroadCast");
    System.out.println(" 6. /EXIT                       : Exit App");
    while (true) {
        channel.basicConsume(queueName, true, consumer);
        String input = sc.nextLine();
        String[] query;
        if ((query = CommandRegexes.NICK.match(input)) != null) {
            String Nickname = query[0];
            user = Nickname;
            System.out.println(" [x] Your Nickname '" + Nickname + "'");

        } else if ((query = CommandRegexes.JOIN.match(input)) != null) {
            String channel_name = query[0];
            channel.queueBind(queueName, EXCHANGE_NAME, channel_name);
            channellist.add(channel_name);
            System.out.println(" [x] you had Join '" + channel_name + "'");

        } else if ((query = CommandRegexes.LEAVE.match(input)) != null) {
            String channel_name = query[0];
            if (channellist.contains(channel_name)) {
                channellist.remove(channellist.indexOf(channel_name));
                channel.queueUnbind(queueName, EXCHANGE_NAME, channel_name);
                System.out.println(" [x] you had leave '" + channel_name);
            } else {
                System.out.println(" [x] you're not in the channel " + channel_name);
            }
            System.out.println(" [x] you had leave '" + channel_name + "'");

        } else if (CommandRegexes.EXIT.match(input) != null) {
            channel.close();
            connection.close();
            break;

        } else if ((query = CommandRegexes.MESSAGE_CHANNEL.match(input)) != null) {
            String channel_name = query[0];
            if (channellist.contains(channel_name)) {
                String message = "[" + channel_name + "] (" + user + ") " + query[1];
                channel.basicPublish(EXCHANGE_NAME, channel_name, null, message.getBytes());
                System.out.println(" [x] Sent '" + message + "'");
                System.out.println(" : to '" + channel_name + "'");
            } else {
                System.out.println(" [x] No Channel " + channel_name + " on your list");
            }
        } else {
            System.out.println(" [x] Broadcasting '" + input + "'");
            for (String channellist1 : channellist) {
                String messages = "[" + channellist1 + "] (" + user + ") " + input;
                channel.basicPublish(EXCHANGE_NAME, channellist1, null, messages.getBytes());
            }
            System.out.println(" [x] OK ;D");
        }
    }

}

From source file:RabbitmpTest.EndPoint.java

License:Apache License

public EndPoint(String endpointName) throws IOException {

    this.endPointName = endpointName;

    //Create a connection factory
    ConnectionFactory factory = new ConnectionFactory();

    //hostname of your rabbitmq server
    factory.setHost("localhost");
    //        factory.setHost("115.28.134.193");
    //        factory.setUsername("root");
    //        factory.setPassword("c23f0377");

    //getting a connection
    connection = factory.newConnection();

    //creating a channel
    channel = connection.createChannel();

    //declaring a queue for this channel. If queue does not exist,
    //it will be created on the server.
    channel.queueDeclare(endpointName, false, false, false, null);
}

From source file:rabbitMQ.Client.java

public Client(MessageTranslator translator) throws IOException, TimeoutException {
    _translator = translator;//from ww  w .  jav  a  2  s .  c om

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("54.69.168.245");
    _connection = factory.newConnection();
    _channel = _connection.createChannel();

    _channel.queueDeclare(QUEUE_NAME, false, false, false, null);

    Consumer consumer = new DefaultConsumer(_channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            ConsumeMessage(body);
        }
    };
    _channel.basicConsume(QUEUE_NAME, true, consumer);
}

From source file:rabbitmq.Producer.java

public void run() {
    try {//from www. jav a  2 s .c o m
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");

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

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);

        for (int i = 0; i < num; i++) {
            Mensaje persona = new Mensaje("RabbitMQ_Java", "Persona" + i, "ID" + i);
            Gson gson = new Gson();
            String message = gson.toJson(persona);

            channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
            System.out.println(" [" + id + "] Sent '" + message + "'");
        }

        channel.close();
        connection.close();
    } catch (IOException ex) {
        Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:rabbitmq_clienttest.Simple_receiver.java

public static void main(String[] argv) throws Exception {

    db = new Database_connector_sqlite();

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setUsername("es");
    factory.setPassword("a");
    //factory.setVirtualHost("/");
    factory.setPort(5672);/*from   w w w.j ava2s  .c o m*/
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    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");
            System.out.println(" [x] Received '" + message + "'");
            doWork(message);

        }
    };
    channel.basicConsume(QUEUE_NAME, true, consumer);

}