Example usage for com.rabbitmq.client Envelope getRoutingKey

List of usage examples for com.rabbitmq.client Envelope getRoutingKey

Introduction

In this page you can find the example usage for com.rabbitmq.client Envelope getRoutingKey.

Prototype

public String getRoutingKey() 

Source Link

Document

Get the routing key included in this parameter envelope

Usage

From source file:com.analogmountains.flume.RabbitMQSource.java

License:Open Source License

@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
        throws IOException {
    Map<String, String> headers = eventHeadersFromBasicProperties(properties);
    headers.put("routingKey", envelope.getRoutingKey());
    headers.put("exchange", envelope.getExchange());

    Event event = EventBuilder.withBody(body);
    event.setHeaders(headers);//ww w  .ja  va2  s.  com
    getChannelProcessor().processEvent(event);

    long deliveryTag = envelope.getDeliveryTag();
    channel.basicAck(deliveryTag, false);
}

From source file:com.chat.UI.java

private void forNahid() throws IOException {
    Consumer consumer1 = new DefaultConsumer(channel) {

        @Override//w ww  .  ja  va2 s .  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(" [x] Received '" + message + "'");

            if (message.toLowerCase().startsWith(user.toLowerCase())) {
                return;
            }

            String routingKey = envelope.getRoutingKey();
            System.out.println(" [x] routingKey '" + routingKey + "'");

            if (routingKey.equalsIgnoreCase(Config.E_ALL)) {
                ALL_MSG += message + "\n";
                jTextArea1.setText(ALL_MSG);
            }
        }
    };

    Consumer consumer2 = 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 + "'");

            if (message.toLowerCase().startsWith(user.toLowerCase())) {
                return;
            }

            String routingKey = envelope.getRoutingKey();
            System.out.println(" [x] routingKey '" + routingKey + "'");

            if (routingKey.equalsIgnoreCase(Config.E_NAHID_SHOSHI)) {
                NAHID_SHOSHI_MSG += message + "\n";
                jTextArea6.setText(NAHID_SHOSHI_MSG);
            }
        }

    };

    Consumer consumer3 = 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 + "'");

            if (message.toLowerCase().startsWith(user.toLowerCase())) {
                return;
            }

            String routingKey = envelope.getRoutingKey();
            System.out.println(" [x] routingKey '" + routingKey + "'");

            if (routingKey.equalsIgnoreCase(Config.E_TANNEE_NAHID)) {
                TANNEE_NAHID_MSG += message + "\n";
                jTextArea5.setText(TANNEE_NAHID_MSG);

            }
        }

    };
    channel.basicConsume(Config.Q_NAHID_ALL, true, consumer1);
    channel.basicConsume(Config.Q_NAHID_SHOSHI, true, consumer2);
    channel.basicConsume(Config.Q_NAHID_TANNEE, true, consumer3);
}

From source file:com.chat.UI.java

private void forTannee() throws IOException {
    Consumer consumer1 = new DefaultConsumer(channel) {

        @Override/*from  w  w w .  j  ava  2s.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(" [x] Received '" + message + "'");

            if (message.toLowerCase().startsWith(user.toLowerCase())) {
                return;
            }

            String routingKey = envelope.getRoutingKey();
            System.out.println(" [x] routingKey '" + routingKey + "'");

            if (routingKey.equalsIgnoreCase(Config.E_ALL)) {
                ALL_MSG += message + "\n";
                jTextArea1.setText(ALL_MSG);
            }
        }
    };

    Consumer consumer2 = 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 + "'");

            if (message.toLowerCase().startsWith(user.toLowerCase())) {
                return;
            }

            String routingKey = envelope.getRoutingKey();
            System.out.println(" [x] routingKey '" + routingKey + "'");

            if (routingKey.equalsIgnoreCase(Config.E_TANNEE_SHOSHI)) {
                TANNEE_SHOSHI_MSG += message + "\n";
                jTextArea5.setText(TANNEE_SHOSHI_MSG);
            }
        }

    };

    Consumer consumer3 = 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 + "'");

            if (message.toLowerCase().startsWith(user.toLowerCase())) {
                return;
            }

            String routingKey = envelope.getRoutingKey();
            System.out.println(" [x] routingKey '" + routingKey + "'");

            if (routingKey.equalsIgnoreCase(Config.E_TANNEE_NAHID)) {
                TANNEE_NAHID_MSG += message + "\n";
                jTextArea6.setText(TANNEE_NAHID_MSG);
            }
        }

    };

    channel.basicConsume(Config.Q_TANNEE_ALL, true, consumer1);
    channel.basicConsume(Config.Q_TANNEE_SHOSHI, true, consumer2);
    channel.basicConsume(Config.Q_TANNEE_NAHID, true, consumer3);
}

From source file:com.chat.UI.java

private void forShoshi() throws IOException {
    Consumer consumer1 = new DefaultConsumer(channel) {

        @Override//from  w  w  w.j a v  a  2  s .c  om
        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 + "'");

            if (message.toLowerCase().startsWith(user.toLowerCase())) {
                return;
            }

            String routingKey = envelope.getRoutingKey();
            System.out.println(" [x] routingKey '" + routingKey + "'");

            if (routingKey.equalsIgnoreCase(Config.E_ALL)) {
                ALL_MSG += message + "\n";
                jTextArea1.setText(ALL_MSG);
            }
        }
    };

    Consumer consumer2 = 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 + "'");

            if (message.toLowerCase().startsWith(user.toLowerCase())) {
                return;
            }

            String routingKey = envelope.getRoutingKey();
            System.out.println(" [x] routingKey '" + routingKey + "'");

            if (routingKey.equalsIgnoreCase(Config.E_NAHID_SHOSHI)) {
                NAHID_SHOSHI_MSG += message + "\n";
                jTextArea6.setText(NAHID_SHOSHI_MSG);
            }
        }

    };

    Consumer consumer3 = 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 + "'");

            if (message.toLowerCase().startsWith(user.toLowerCase())) {
                return;
            }

            String routingKey = envelope.getRoutingKey();
            System.out.println(" [x] routingKey '" + routingKey + "'");

            if (routingKey.equalsIgnoreCase(Config.E_TANNEE_SHOSHI)) {
                TANNEE_SHOSHI_MSG += message + "\n";
                jTextArea5.setText(TANNEE_SHOSHI_MSG);
            }
        }

    };

    channel.basicConsume(Config.Q_SHOSHI_ALL, true, consumer1);
    channel.basicConsume(Config.Q_SHOSHI_TANNEE, true, consumer3);
    channel.basicConsume(Config.Q_SHOSHI_NAHID, true, consumer2);
}

From source file:com.es.dashboard.ConnectionBroker.java

@PostConstruct
public void init() {

    bigBaite = new LinkedList<>();
    readings = new ArrayList<Reading>();
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("systembus"); // RabbitMQ IP
    Connection connection = null;
    try {//from w ww.j  a  v  a 2s  . c o m
        connection = factory.newConnection();
    } catch (IOException ex) {
        logger.error(ex);
    } catch (TimeoutException ex) {
        logger.error(ex);
    }
    Channel channel = null;
    try {
        channel = connection.createChannel();
    } catch (IOException ex) {
        logger.error(ex);
    }

    try {
        channel.exchangeDeclare("sensors", "topic"); // sensors is the name of the exchange
    } catch (IOException ex) {
        logger.error(ex);
    }
    AMQP.Queue.DeclareOk result = null;
    try {
        result = channel.queueDeclare("", false, true, false, null);
    } catch (IOException ex) {
        logger.error(ex);
    }
    String queuename = result.getQueue();
    try {
        channel.queueBind(queuename, "sensors", "realtime.sensordata"); // Binding key is #, this will consume all messages
        channel.queueBind(queuename, "sensors", "realtime.alarms"); // Binding key is #, this will consume all messages
    } catch (IOException ex) {
        logger.error(ex);
    }

    logger.info(" [*] 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");
            EventBus eventBus = EventBusFactory.getDefault().eventBus();
            Gson gson = new Gson();
            Type type = new TypeToken<Map<String, String>>() {
            }.getType();
            Map<String, String> myMap = gson.fromJson(message, type);
            if (envelope.getRoutingKey().equals("realtime.alarms")) {
                eventBus.publish("/channel", message);
                return;
            }
            logger.info(envelope.getRoutingKey());

            readings.add(new Reading(new Date(Long.parseLong(myMap.get("timestamp"))), myMap.get("data"),
                    myMap.get("name"), myMap.get("room")));
            eventBus.publish("/channel", message);

        }
    };
    try {
        //RequestContext.getCurrentInstance().execute("function rpt() {$(\".qualquer\").first().trigger(\"click\")}"
        //        + "var timeoutdummy = window.setInterval(rpt,2000);");
        channel.basicConsume(queuename, true, consumer);
    } catch (IOException ex) {
        ex.printStackTrace();
        logger.error(ex);
    }

}

From source file:com.es.dashboard.ConnectionBrokerJPA.java

@PostConstruct
public void init() {
    bigBaite = new LinkedList<>();
    readings = new ArrayList<Reading>();
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("systembus"); // RabbitMQ IP
    Connection connection = null;
    try {//from  w w  w  . j  a  v  a2 s .c o m
        connection = factory.newConnection();
    } catch (IOException ex) {
        logger.error(ex);
    } catch (TimeoutException ex) {
        logger.error(ex);
    }
    Channel channel = null;
    try {
        channel = connection.createChannel();
    } catch (IOException ex) {
        logger.error(ex);
    }

    try {
        channel.exchangeDeclare("sensors", "topic"); // sensors is the name of the exchange
    } catch (IOException ex) {
        logger.error(ex);
    }
    AMQP.Queue.DeclareOk result = null;
    try {
        result = channel.queueDeclare("", false, true, false, null);
    } catch (IOException ex) {
        logger.error(ex);
    }
    String queuename = result.getQueue();
    try {
        channel.queueBind(queuename, "sensors", "dashboard.response"); // Binding key is #, this will consume all messages
    } catch (IOException ex) {
        logger.error(ex);
    }

    logger.info(" [*] 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");

            EventBus eventBus = EventBusFactory.getDefault().eventBus();
            Gson gson = new Gson();
            Type type = new TypeToken<Map<String, String>>() {
            }.getType();
            Map<String, String> myMap = gson.fromJson(message, type);
            if (envelope.getRoutingKey().equals("realtime.alarms")) {
                eventBus.publish("/jpa", message);
                return;
            }
            System.out.println(envelope.getRoutingKey());
            logger.info(envelope.getRoutingKey());

            //readings.add(new Reading(new Date(Long.parseLong(myMap.get("timestamp"))), myMap.get("value"), myMap.get("name"), myMap.get("room")));
            //System.out.println("publishing " + msg_to_publish);
            eventBus.publish("/jpa", message);
            //System.out.println("published");

        }
    };
    try {
        //RequestContext.getCurrentInstance().execute("function rpt() {$(\".qualquer\").first().trigger(\"click\")}"
        //        + "var timeoutdummy = window.setInterval(rpt,2000);");
        channel.basicConsume(queuename, true, consumer);
    } catch (IOException ex) {
        ex.printStackTrace();
        logger.error(ex);
    }

}

From source file:com.lang.pat.rabbitirc.Consumer.java

public void consume() {
    com.rabbitmq.client.Consumer consumer = new DefaultConsumer(channel) {
        @Override//from  w ww.ja v a  2 s. c o m
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String message = new String(body, "UTF-8");
            JSONParser parse = new JSONParser();
            SimpleDateFormat formatDate = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
            try {
                JSONObject JSONMessage = (JSONObject) parse.parse(message);

                Date sendDat = new Date();
                sendDat.setTime((long) JSONMessage.get("timestamp"));
                System.out.println("[" + envelope.getRoutingKey() + "] " + "[" + JSONMessage.get("username")
                        + "] " + JSONMessage.get("message") + " || " + formatDate.format(sendDat));

                if (JSONMessage.get("username").toString().equals(ClientMain.USERNAME)) {
                    if (!JSONMessage.get("token").toString().equals(ClientMain.token)) {
                        System.out.print("! Duplicate username, please change to avoid conflict!");
                    }
                }

                System.out.print("> ");

            } catch (ParseException ex) {
                Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    };
    try {
        channel.basicConsume(ClientMain.QUEUENAME, true, consumer);
    } catch (IOException ex) {
        Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.mycompany.javateste.queues.routing.ReceiveLogsDirect.java

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

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

    if (argv.length < 1) {
        System.err.println("Usage: ReceiveLogsDirect [info] [warning] [error]");
        System.exit(1);/*from  www .  j  a v a  2s .  co m*/
    }

    for (String severity : argv) {
        channel.queueBind(queueName, EXCHANGE_NAME, severity);
    }
    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 '" + envelope.getRoutingKey() + "':'" + message + "'");
        }
    };
    channel.basicConsume(queueName, true, consumer);
}

From source file:com.mycompany.javateste.queues.topic.ReceiveLogsTopic.java

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

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

    if (argv.length < 1) {
        System.err.println("Usage: ReceiveLogsTopic [binding_key]...");
        System.exit(1);//  ww  w .j  a v a 2  s . c  o m
    }

    for (String bindingKey : argv) {
        channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);
    }

    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 '" + envelope.getRoutingKey() + "':'" + message + "'");
        }
    };
    channel.basicConsume(queueName, true, consumer);
}

From source file:com.mycompany.net.Run.java

/**
 * @param args the command line arguments
 * @throws java.io.IOException/*from   ww  w  . j a  va  2 s  .  co  m*/
 * @throws java.util.concurrent.TimeoutException
 */
public static void main(String[] args) throws IOException, TimeoutException {
    // TODO code application logic here

    JPA jpa = new JPA();
    jpa.initDB();

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("systembus"); // RabbitMQ IP
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare("sensors", "topic"); // sensors is the name of the exchange
    AMQP.Queue.DeclareOk result = channel.queueDeclare("", false, true, false, null);
    String queuename = result.getQueue();
    //bind to sensor info
    channel.queueBind(queuename, "sensors", "gateway.data"); // Binding key is #, this will consume all messages

    //bind to the dashboard
    channel.queueBind(queuename, "sensors", "dashboard.request");

    //bind to Processing units output
    channel.queueBind(queuename, "sensors", "database.put");

    logger.info(" [*] 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 {

            try {
                String message = new String(body, "UTF-8");
                Gson gson = new Gson();
                Type type = new TypeToken<Map<String, String>>() {
                }.getType();
                Map<String, String> myMap = gson.fromJson(message, type);
                logger.info(" [x] " + envelope.getRoutingKey() + " - Received '" + message + "'");

                String routing_key = envelope.getRoutingKey();
                if ("gateway.data".equals(routing_key)) {
                    jpa.saveRawToDB(myMap);
                } else if ("database.put".equals(routing_key)) {
                    jpa.saveAlertToDB(myMap);
                } else if ("dashboard.request".equals(routing_key)) {
                    Thread.sleep(1000);
                    jpa.processRequest(myMap, channel);
                } else
                    logger.error("NOT A VALID MESSAGE!");

            } catch (Exception e) {
                logger.error(e.toString());
            }

        }
    };

    channel.basicConsume(queuename, true, consumer);

    //channel.close();
    //connection.close();

    //entityManager.close();
    //entityManagerFactory.close();

}