Search in sources :

Example 1 with DeclareOk

use of com.rabbitmq.client.AMQP.Queue.DeclareOk 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)

Example 2 with DeclareOk

use of com.rabbitmq.client.AMQP.Queue.DeclareOk in project camel by apache.

the class TemporaryQueueReplyManager method createListenerContainer.

@Override
protected Connection createListenerContainer() throws Exception {
    log.debug("Creating connection");
    Connection conn = endpoint.connect(executorService);
    log.debug("Creating channel");
    Channel channel = conn.createChannel();
    // setup the basicQos
    if (endpoint.isPrefetchEnabled()) {
        channel.basicQos(endpoint.getPrefetchSize(), endpoint.getPrefetchCount(), endpoint.isPrefetchGlobal());
    }
    //Let the server pick a random name for us
    DeclareOk result = channel.queueDeclare();
    log.info("Using temporary queue name: {}", result.getQueue());
    setReplyTo(result.getQueue());
    //TODO check for the RabbitMQConstants.EXCHANGE_NAME header
    channel.queueBind(getReplyTo(), endpoint.getExchangeName(), getReplyTo());
    consumer = new RabbitConsumer(this, channel);
    consumer.start();
    return conn;
}
Also used : DeclareOk(com.rabbitmq.client.AMQP.Queue.DeclareOk) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection)

Aggregations

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