Search in sources :

Example 1 with BasicProperties

use of com.rabbitmq.client.AMQP.BasicProperties in project flux by eclipse.

the class RabbitMQMessageConnector method createInbox.

private String createInbox() throws IOException {
    DeclareOk ok = this.channel.queueDeclare("", /*durable*/
    false, /*exclusive*/
    false, /*autoDelete*/
    true, null);
    final String inbox = ok.getQueue();
    console.log("Inbox created: " + inbox);
    channel.basicConsume(inbox, new DefaultConsumer(channel) {

        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException {
            try {
                JSONObject obj = JSON.parse(body);
                if (!isSelfOriginated(obj)) {
                    handleIncomingMessage(obj.getString("type"), obj.getJSONObject("data"));
                }
            } catch (Exception e) {
                console.log(e);
            }
        }

        /**
			 * Tests whether an incoming message originated from the same MessageConnector that
			 * is receiving it. (Such messages are skipped in keeping with how socketio does the same
			 * thing).
			 */
        private boolean isSelfOriginated(JSONObject obj) {
            try {
                String origin = obj.getString("origin");
                return inbox.equals(origin);
            } catch (Exception e) {
                console.log(e);
            }
            return false;
        }
    });
    return inbox;
}
Also used : DeclareOk(com.rabbitmq.client.AMQP.Queue.DeclareOk) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) JSONObject(org.json.JSONObject) BasicProperties(com.rabbitmq.client.AMQP.BasicProperties) IOException(java.io.IOException) Envelope(com.rabbitmq.client.Envelope) IOException(java.io.IOException)

Aggregations

BasicProperties (com.rabbitmq.client.AMQP.BasicProperties)1 DeclareOk (com.rabbitmq.client.AMQP.Queue.DeclareOk)1 DefaultConsumer (com.rabbitmq.client.DefaultConsumer)1 Envelope (com.rabbitmq.client.Envelope)1 IOException (java.io.IOException)1 JSONObject (org.json.JSONObject)1