Example usage for com.rabbitmq.client Channel queueBind

List of usage examples for com.rabbitmq.client Channel queueBind

Introduction

In this page you can find the example usage for com.rabbitmq.client Channel queueBind.

Prototype

Queue.BindOk queueBind(String queue, String exchange, String routingKey) throws IOException;

Source Link

Document

Bind a queue to an exchange, with no extra arguments.

Usage

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/* w  ww. j a  va2s. 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: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;//  ww w .jav a 2 s  .  c  om
    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:Release1.Controllers.AlarmController.java

/**
 * mtodo para recibir mensajes de los sensores de alarmas de ventana
 * dependiendo del mensaje recibido se envia la respuesta a los sensores
 * @throws IOException//from  w  w  w. j a v a2  s.c  o  m
 * @throws TimeoutException 
 */
private synchronized void receiveAlamrWindowSensorMessage() throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(HOST);

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

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

    Consumer consumer;
    consumer = new DefaultConsumer(channel) {
        @Override
        public synchronized void handleDelivery(String consumerTag, Envelope envelope,
                AMQP.BasicProperties properties, byte[] body) throws IOException {
            setMessage(new String(body, "UTF-8"));
            logger.info("Class ALARM WINDOW Controller --- RECEIVED from Sensor --- Value: "
                    + new String(body, "UTF-8"));
            if (Integer.parseInt(getMessage()) > MAX_VALUE_ALARMS) {//si el valor obtenido es mayor al permitido para simular las alarmas se activa la alarma
                try {
                    sendMessage(ID_CHANNEL_AWINDOW_CONTROLLER, ID_AWINDOW_ON);//envio de mensaje para encender alarma la alarma de ventnaa
                    logger.info("Class: ALARM CONTROLLER --- SEND --- ACTIVE ALARM WINDOW");
                } catch (TimeoutException ex) {
                    logger.error(ex);
                }
            } else {
                try {
                    sendMessage(ID_CHANNEL_AWINDOW_CONTROLLER, ID_AWINDOW_OFF);//envio de mensaje apra apagar la alarma de ventana
                } catch (TimeoutException ex) {
                    logger.error(ex);
                }
            }
        }
    };
    channel.basicConsume(queueName, true, consumer);
}

From source file:Release1.Controllers.AlarmController.java

/**
 * mtodo para recibir mensajes de los sensores de alarmas de puerta
 * dependiendo del mensaje recibido se envia la respuesta a los sensores
 * @throws IOException//from  w  w  w  .  j a  va2  s  . c o m
 * @throws TimeoutException 
 */
private void receiveAlarmDoorMessage() throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(HOST);
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

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

    Consumer consumer = new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            setMessage(new String(body, "UTF-8"));
            logger.info("Class Alarm DOOR Controller --- RECEIVED from Sensor --- Value: "
                    + new String(body, "UTF-8"));
            if (Integer.parseInt(getMessage()) > MAX_VALUE_ALARMS) {
                try {
                    sendMessage(ID_CHANNEL_ADOOR_CONTROLLER, ID_ADOOR_ON);//envio de mensaje para encender la alarma
                } catch (TimeoutException ex) {
                    logger.error(ex);
                }
            } else {
                try {
                    sendMessage(ID_CHANNEL_ADOOR_CONTROLLER, ID_ADOOR_OFF);//envio de mensajes para apagar la alarma
                } catch (TimeoutException ex) {
                    logger.error(ex);
                }
            }
        }
    };
    channel.basicConsume(queueName, true, consumer);
}

From source file:Release1.Controllers.AlarmController.java

/**
 * * mtodo para recibir mensajes de los sensores de alarmas de movimiento
 * dependiendo del mensaje recibido se envia la respuesta a los sensores
 * @throws IOException//from  w  w w. java2  s .  co  m
 * @throws TimeoutException 
 */
private void receiveMoveMessage() throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(HOST);

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

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

    Consumer consumer;
    consumer = new DefaultConsumer(channel) {
        @Override
        public synchronized void handleDelivery(String consumerTag, Envelope envelope,
                AMQP.BasicProperties properties, byte[] body) throws IOException {
            setMessage(new String(body, "UTF-8"));
            logger.info("Class ALARM MOVE Controller --- RECEIVED from Sensor --- Value: "
                    + new String(body, "UTF-8"));
            if (Integer.parseInt(getMessage()) > MAX_VALUE_ALARMS) {
                try {
                    sendMessage(ID_CHANNEL_AMOVE_CONTROLLER, ID_AMOVE_ON);//envio de mensajes para encender la alarma de movimiento
                    logger.info("Class: ALARM CONTROLLER --- SEND --- ACTIVE ALARM WINDOW");
                } catch (TimeoutException ex) {
                    logger.error(ex);
                }
            } else {
                try {
                    sendMessage(ID_CHANNEL_AMOVE_CONTROLLER, ID_AMOVE_OFF);//envio de mensajes para apagar la alarma de movimientos
                } catch (TimeoutException ex) {
                    logger.error(ex);
                }
            }
        }
    };
    channel.basicConsume(queueName, true, consumer);
}

From source file:Release2.Controllers.AlarmFireController.java

private synchronized void receivedAlarmFireSensorMessage() throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(HOST);//from  www . j  ava  2 s.co  m

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

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

    Consumer consumer;
    consumer = new DefaultConsumer(channel) {
        @Override
        public synchronized void handleDelivery(String consumerTag, Envelope envelope,
                AMQP.BasicProperties properties, byte[] body) throws IOException {
            setMessage(new String(body, "UTF-8"));
            logger.info("Class ALARM FIRE Controller --- RECEIVED from Sensor --- Value: "
                    + new String(body, "UTF-8"));
            if (Integer.parseInt(getMessage()) > MAX_VALUE_ALARMS) {//si el valor obtenido es mayor al permitido para simular las alarmas se activa la alarma
                try {
                    sendMessage(ID_CHANNEL_AFIRE_CONTROLLER, ID_AFIRE_ON);//envio de mensaje para encender alarma la alarma de ventnaa
                    logger.info("Class: ALARM CONTROLLER --- SEND --- ACTIVE ALARM WINDOW");
                } catch (TimeoutException ex) {
                    logger.error(ex);
                }
            } else {
                try {
                    sendMessage(ID_CHANNEL_AFIRE_CONTROLLER, ID_AFIRE_OFF);//envio de mensaje apra apagar la alarma de ventana
                } catch (TimeoutException ex) {
                    logger.error(ex);
                }
            }
        }
    };
    channel.basicConsume(queueName, true, consumer);
}

From source file:se.kodiak.logging.appenders.util.MQListener.java

License:Open Source License

public MQListener() {
    try {//from  ww  w.  ja  va2 s.co  m
        ConnectionFactory factory = new ConnectionFactory();
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.basicQos(1);
        channel.exchangeDeclare("test", "topic");
        String queueName = channel.queueDeclare().getQueue();
        channel.queueBind(queueName, "test", "log.*");
        consumer = new QueueingConsumer(channel);
        channel.basicConsume(queueName, true, consumer);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:translators.ToJsonSchool.java

public static void main(String[] args) throws Exception {
    final String replyQueueName = "teachersJsonReply";
    final String EXCHANGE_NAME_SCHOOL = "cphbusiness.bankJSON";
    final String exchangeName = ExchangeName.GLOBAL;

    RabbitConnection rabbitConnection = new RabbitConnection();

    Channel channel = rabbitConnection.makeConnection();

    channel.exchangeDeclare(exchangeName, "direct");
    String queueName = channel.queueDeclare().getQueue();

    channel.queueBind(queueName, exchangeName, "keyBankJSON");

    //get banks from queue. "Get banks" component
    QueueingConsumer consumer = new QueueingConsumer(channel) {
        @Override//from ww w .  ja v a2s .  c o  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("Received msg: " + message);
            Message messageFromJson = getFromJson(message);

            sendMsgToBank(messageFromJson, properties.getCorrelationId(), EXCHANGE_NAME_SCHOOL, replyQueueName);
        }
    };
    channel.basicConsume(queueName, true, consumer);
}

From source file:translators.ToOurJsonBank.java

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

    RabbitConnection rabbitConnection = new RabbitConnection();
    Channel channel = rabbitConnection.makeConnection();
    channel.exchangeDeclare(ExchangeName.GLOBAL, "direct");
    String queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, ExchangeName.GLOBAL, RoutingKeys.OUR_JSON_BANK);

    QueueingConsumer consumer = new QueueingConsumer(channel) {
        @Override/*from  w  ww  .ja v 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("Received msg: " + message);
            Message messageFromJson = getFromJson(message);

            sendMsgToBank(messageFromJson, properties.getCorrelationId(),
                    ExchangeName.OUTPUT_TRANSLATOR_TO_OUR_JSON_BANK, REPLY_QUEUE_NAME);
        }
    };

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

From source file:translators.ToOurXmlBank.java

public static void main(String[] args) throws IOException, TimeoutException {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("datdb.cphbusiness.dk");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(exchangeName, "direct");
    String queueName = channel.queueDeclare().getQueue();

    channel.queueBind(queueName, exchangeName, "ourBankSoapXML");

    //get banks from queue. "Get banks" component
    QueueingConsumer consumer = new QueueingConsumer(channel) {
        @Override//from   w  ww  .j av a2  s.  com
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String message = new String(body, "UTF-8");
            Message messageFromJson = getFromJson(message);
            connectToWebService(messageFromJson, properties.getCorrelationId(), EXCHANGE_NAME_SCHOOL,
                    replyQueueName);

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

}