Example usage for com.rabbitmq.client GetResponse getEnvelope

List of usage examples for com.rabbitmq.client GetResponse getEnvelope

Introduction

In this page you can find the example usage for com.rabbitmq.client GetResponse getEnvelope.

Prototype

public Envelope getEnvelope() 

Source Link

Document

Get the Envelope included in this response

Usage

From source file:net.nzcorp.hbase.tableevent_signaler.TableEventSignalerTest.java

License:Apache License

@Test
public void filterOnQualifiers() throws Exception {

    Map<String, String> kvs = configureHBase(primaryTableNameString, secondaryIdxTableNameString, "e", "eg",
            "a", amq_default_address, primaryTableNameString, "false", "one_key|some_key");
    setupHBase(kvs);/*from   w  w  w .  j av  a2s.  com*/

    //simulate population of secondary index as a result of the above
    Put idxPut = new Put("EFG1".getBytes());
    idxPut.addColumn("a".getBytes(), "EFB1".getBytes(), "".getBytes());
    secondaryIdxTable.put(idxPut);

    // Add a column to the primary table, which should trigger a data ripple to the downstream table
    Put tablePut = new Put("EFG1".getBytes());
    tablePut.addColumn("e".getBytes(), "some_key".getBytes(), "some_value".getBytes());
    primaryTable.put(tablePut);

    //check that values made it to the queue
    com.rabbitmq.client.ConnectionFactory factory = new com.rabbitmq.client.ConnectionFactory();
    factory.setUri(amq_default_address);
    com.rabbitmq.client.Connection conn = factory.newConnection();
    com.rabbitmq.client.Channel channel = conn.createChannel();
    System.out.println(String.format("Test: connecting to %s", primaryTableNameString));

    while (true) {
        GetResponse response = channel.basicGet(primaryTableNameString, false);
        if (response == null)//busy-wait until the message has made it through the MQ
        {
            continue;
        }
        String routingKey = response.getEnvelope().getRoutingKey();
        Assert.assertEquals("Routing key should be rowkey", "genome", routingKey);

        String contentType = response.getProps().getContentType();
        Assert.assertEquals("Content type should be preserved", "application/json", contentType);

        Map<String, Object> headers = response.getProps().getHeaders();
        Assert.assertEquals("An action should be set on the message", "put", headers.get("action").toString());

        byte[] body = response.getBody();

        JSONObject jo = new JSONObject(new String(body));
        String column_family = (String) jo.get("column_family");
        Assert.assertEquals("Column family should be preserved in the message body", "eg", column_family);

        String column_value = (String) jo.get("column_value");
        Assert.assertEquals("Column value is not sent by default", "", column_value);

        long deliveryTag = response.getEnvelope().getDeliveryTag();
        channel.basicAck(deliveryTag, false);
        break;
    }
}

From source file:net.nzcorp.hbase.tableevent_signaler.TableEventSignalerTest.java

License:Apache License

@Test
public void onlyPutWhenInQualifierFilter() throws Exception {

    Map<String, String> kvs = configureHBase(primaryTableNameString, secondaryIdxTableNameString, "e", "eg",
            "a", amq_default_address, primaryTableNameString, "false", "some_key");
    setupHBase(kvs);//  w w w .  j a va  2 s  . com

    //simulate population of secondary index as a result of the above
    Put idxPut = new Put("EFG1".getBytes());
    idxPut.addColumn("a".getBytes(), "EFB1".getBytes(), "".getBytes());
    secondaryIdxTable.put(idxPut);

    // Add a column to the primary table, which should trigger a data ripple to the downstream table
    Put tablePut = new Put("EFG1".getBytes());
    tablePut.addColumn("e".getBytes(), "some_key".getBytes(), "some_value".getBytes());
    tablePut.addColumn("e".getBytes(), "other_key".getBytes(), "some_value".getBytes());
    tablePut.addColumn("e".getBytes(), "third_key".getBytes(), "some_value".getBytes());
    primaryTable.put(tablePut);

    //check that values made it to the queue
    com.rabbitmq.client.ConnectionFactory factory = new com.rabbitmq.client.ConnectionFactory();
    factory.setUri(amq_default_address);
    com.rabbitmq.client.Connection conn = factory.newConnection();
    com.rabbitmq.client.Channel channel = conn.createChannel();
    System.out.println(String.format("Test: connecting to %s", primaryTableNameString));

    while (true) {
        int numMessages = channel.queueDeclarePassive(primaryTableNameString).getMessageCount();
        GetResponse response = channel.basicGet(primaryTableNameString, false);
        if (response == null || numMessages == 0)//busy-wait until the message has made it through the MQ
        {
            continue;
        }
        Assert.assertEquals("There should be only a single key in the queue", 1, numMessages);
        String routingKey = response.getEnvelope().getRoutingKey();
        Assert.assertEquals("Routing key should be rowkey", "genome", routingKey);

        String contentType = response.getProps().getContentType();
        Assert.assertEquals("Content type should be preserved", "application/json", contentType);

        Map<String, Object> headers = response.getProps().getHeaders();
        Assert.assertEquals("An action should be set on the message", "put", headers.get("action").toString());

        byte[] body = response.getBody();

        JSONObject jo = new JSONObject(new String(body));
        String column_family = (String) jo.get("column_family");
        Assert.assertEquals("Column family should be preserved in the message body", "eg", column_family);

        String column_value = (String) jo.get("column_value");
        Assert.assertEquals("Column value is not sent by default", "", column_value);

        long deliveryTag = response.getEnvelope().getDeliveryTag();
        channel.basicAck(deliveryTag, false);
        break;
    }
}

From source file:net.nzcorp.hbase.tableevent_signaler.TableEventSignalerTest.java

License:Apache License

@Test
public void preDeleteHappyCase() throws Exception {

    Map<String, String> kvs = configureHBase(primaryTableNameString, secondaryIdxTableNameString, "e", "eg",
            "a", amq_default_address, primaryTableNameString, "true", "");
    setupHBase(kvs);//from  w  ww  .j  a  v a  2  s.  co m
    com.rabbitmq.client.ConnectionFactory factory = new com.rabbitmq.client.ConnectionFactory();
    factory.setUri(amq_default_address);
    com.rabbitmq.client.Connection conn = factory.newConnection();
    com.rabbitmq.client.Channel channel = conn.createChannel();

    //simulate population of secondary index for a put on the downstreamTable
    Put idxPut = new Put("EFG1".getBytes());
    idxPut.addColumn("a".getBytes(), "EFB1".getBytes(), "".getBytes());
    secondaryIdxTable.put(idxPut);

    //simulate a data rippling performed by the data_rippler service consuming off of the rabbitmq queue
    Put tablePut = new Put("EFB1".getBytes());
    tablePut.addColumn("eg".getBytes(), "some_key".getBytes(), "some_value".getBytes());
    downstreamTable.put(tablePut);

    // since we made no active put to the queue from the prePut, we need to declare it explicitly here
    channel.queueDeclare(primaryTableNameString, true, false, false, null);

    // finished with the setup, we now issue a delete which should be caught by the rabbitmq
    Delete d = new Delete("EFG1".getBytes());
    primaryTable.delete(d);

    //check that values made it to the queue
    while (true) {
        GetResponse response = channel.basicGet(primaryTableNameString, false);
        if (response == null)//busy-wait until the message has made it through the MQ
        {
            continue;
        }
        String routingKey = response.getEnvelope().getRoutingKey();
        Assert.assertEquals("Routing key should be rowkey", "genome", routingKey);

        String contentType = response.getProps().getContentType();
        Assert.assertEquals("Content type should be preserved", "application/json", contentType);

        Map<String, Object> headers = response.getProps().getHeaders();
        Assert.assertEquals("An action should be set on the message", "delete",
                headers.get("action").toString());

        byte[] body = response.getBody();
        JSONObject jo = new JSONObject(new String(body));

        String column_qualifier = (String) jo.get("column_qualifier");
        Assert.assertEquals("Column qualifier should be empty, signalling a row delete", "", column_qualifier);

        long deliveryTag = response.getEnvelope().getDeliveryTag();
        channel.basicAck(deliveryTag, false);
        break;
    }
}

From source file:net.nzcorp.hbase.tableevent_signaler.TableEventSignalerTest.java

License:Apache License

@Test
public void discernNewPutFromUpdate() throws Exception {
    Map<String, String> kvs = configureHBase(primaryTableNameString, secondaryIdxTableNameString, "e", "eg",
            "a", amq_default_address, primaryTableNameString, "true", "");
    setupHBase(kvs);//w w  w.  j av a 2 s .c o  m

    //simulate population of secondary index as a result of the above
    Put idxPut = new Put("EFG1".getBytes());
    idxPut.addColumn("a".getBytes(), "EFB1".getBytes(), "".getBytes());
    secondaryIdxTable.put(idxPut);

    /*
    The first put should be registered as a "put" action, while the second should be registered as an "update"
    action, thereby signalling different action to be taken by the consumers
     */

    Put tablePut = new Put("EFG1".getBytes());
    tablePut.addColumn("e".getBytes(), "some_key".getBytes(), "some_value".getBytes());
    primaryTable.put(tablePut);

    tablePut = new Put("EFG1".getBytes());
    tablePut.addColumn("e".getBytes(), "some_other_key".getBytes(), "some_value".getBytes());
    primaryTable.put(tablePut);

    //check that values made it to the queue
    com.rabbitmq.client.ConnectionFactory factory = new com.rabbitmq.client.ConnectionFactory();
    factory.setUri(amq_default_address);
    com.rabbitmq.client.Connection conn = factory.newConnection();
    com.rabbitmq.client.Channel channel = conn.createChannel();
    int msgs_to_consume = 2;
    while (msgs_to_consume > 0) {
        System.out.println(String.format("Messages to get: %s", msgs_to_consume));
        GetResponse response = channel.basicGet(primaryTableNameString, false);
        if (response == null)//busy-wait until the message has made it through the MQ
        {
            continue;
        }
        String routingKey = response.getEnvelope().getRoutingKey();
        Assert.assertEquals("Routing key should be rowkey", "genome", routingKey);

        String contentType = response.getProps().getContentType();
        Assert.assertEquals("Content type should be preserved", "application/json", contentType);

        Map<String, Object> headers = response.getProps().getHeaders();
        byte[] body = response.getBody();

        JSONObject jo = new JSONObject(new String(body));
        String column_family = (String) jo.get("column_family");
        Assert.assertEquals("Column family should be preserved in the message body", "eg", column_family);

        String column_value = (String) jo.get("column_qualifier");

        if (headers.get("action").toString().equals("update")) {
            Assert.assertEquals("Column value should be preserved in the message body", "some_other_key",
                    column_value);
        } else {
            Assert.assertEquals("Column value should be preserved in the message body", "some_key",
                    column_value);
        }

        long deliveryTag = response.getEnvelope().getDeliveryTag();
        channel.basicAck(deliveryTag, false);
        msgs_to_consume--;
    }
}

From source file:org.apache.flume.rabbitmq.source.RabbitMQSource.java

License:Apache License

private Event readEvent() throws IOException {
    GetResponse response;
    response = channel.getChannel().basicGet(queueName, false);
    if (response == null) {
        logger.debug("No event to read from MQ");
        counterGroup.incrementAndGet(COUNTER_EMPTY_MQ_GET);
        return null;
    }/* w w  w .j  a v  a 2  s.com*/
    logger.info("received event: {} bytes", response.getBody().length);
    if (response.getEnvelope() != null) {
        logger.debug("Envelope: {}", response.getEnvelope());
    }
    if (response.getProps() != null && response.getProps().getHeaders() != null) {
        logger.debug("Header: {}", response.getProps().getHeaders().toString());
    }
    logger.debug("Message: {}", new String(response.getBody()));
    counterGroup.incrementAndGet(COUNTER_SUCCESS_MQ_GET);
    isMoreMessagesOnServer = (response.getMessageCount() > 0);
    Map<String, String> eventHeader = createEventHeader(response);
    Event event = EventBuilder.withBody(response.getBody(), eventHeader);
    return event;
}

From source file:org.apache.flume.rabbitmq.source.RabbitMQSource.java

License:Apache License

private Map<String, String> createEventHeader(GetResponse response) {
    Map<String, String> eventHeader = Maps.newHashMap();
    Map<String, Object> mqHeader = Maps.newHashMap();
    if (response.getProps() != null && response.getProps().getHeaders() != null)
        mqHeader = response.getProps().getHeaders();
    for (Map.Entry<String, Object> entry : mqHeader.entrySet()) {
        eventHeader.put(entry.getKey(), entry.getValue().toString());
    }/* w ww  . ja  v a2s.c  o m*/
    Long receivingTimestamp = System.currentTimeMillis();
    String timestamp = Joiner.on(",").useForNull("").join(eventHeader.get(HEADER_FLUME_TIMESTAMP_KEY),
            receivingTimestamp.toString());
    eventHeader.put(HEADER_FLUME_TIMESTAMP_KEY, timestamp);
    eventHeader.put(HEADER_DELIVERYTAG_KEY, Long.toString(response.getEnvelope().getDeliveryTag()));
    return eventHeader;
}

From source file:org.apache.james.queue.rabbitmq.Dequeuer.java

License:Apache License

private RabbitMQMailQueueItem loadItem(GetResponse response) throws MailQueue.MailQueueException {
    Mail mail = loadMail(response);/*from   www . jav  a 2 s .  c  o m*/
    ThrowingConsumer<Boolean> ack = ack(response.getEnvelope().getDeliveryTag(), mail);
    return new RabbitMQMailQueueItem(ack, mail);
}

From source file:org.apache.synapse.message.store.impl.rabbitmq.RabbitMQConsumer.java

License:Open Source License

public MessageContext receive() {
    if (!checkConnection()) {
        if (!reconnect()) {
            if (logger.isDebugEnabled()) {
                logger.debug(getId() + " cannot receive message from store. Can not reconnect.");
            }/*from w ww.j  a  va 2  s.  c  om*/
            return null;
        } else {
            logger.info(getId() + " reconnected to store.");
            isReceiveError = false;
        }
    }
    //setting channel
    if (channel != null) {
        if (!channel.isOpen()) {
            if (!setChannel()) {
                logger.info(getId() + " unable to create the channel.");
                return null;
            }
        }
    } else {
        if (!setChannel()) {
            logger.info(getId() + " unable to create the channel.");
            return null;
        }
    }
    //receive messages
    try {

        GetResponse delivery = null;
        delivery = channel.basicGet(queueName, false);

        if (delivery != null) {
            //deserilizing message
            StorableMessage storableMessage = null;
            ByteArrayInputStream bis = new ByteArrayInputStream(delivery.getBody());
            ObjectInput in = new ObjectInputStream(bis);
            try {
                storableMessage = (StorableMessage) in.readObject();
            } catch (ClassNotFoundException e) {
                logger.error(getId() + "unable to read the stored message" + e);
                channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
            }

            bis.close();
            in.close();
            org.apache.axis2.context.MessageContext axis2Mc = store.newAxis2Mc();
            MessageContext synapseMc = store.newSynapseMc(axis2Mc);
            synapseMc = MessageConverter.toMessageContext(storableMessage, axis2Mc, synapseMc);
            updateCache(delivery, synapseMc, null, false);
            if (logger.isDebugEnabled()) {
                logger.debug(getId() + " Received MessageId:" + delivery.getProps().getMessageId());
            }
            return synapseMc;
        }
    } catch (ShutdownSignalException sse) {
        logger.error(getId() + " connection error when receiving messages" + sse);
    } catch (IOException ioe) {
        logger.error(getId() + " connection error when receiving messages" + ioe);
    }
    return null;
}

From source file:org.hp.samples.ProcessMessage.java

License:Open Source License

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/plain");
    response.setStatus(200);/*from  w  w  w .j  av a  2  s  .  c om*/
    PrintWriter writer = response.getWriter();
    writer.println("Here's your message:");

    // Pull out the RABBITMQ_URL environment variable
    String uri = System.getenv("RABBITMQ_URL");

    ConnectionFactory factory = new ConnectionFactory();
    try {
        factory.setUri(uri);
    } catch (KeyManagementException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

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

    // Create the queue
    channel.queueDeclare("hello", false, false, false, null);

    String routingKey = "thekey";
    String exchangeName = "exchange";

    // Declare an exchange and bind it to the queue
    channel.exchangeDeclare(exchangeName, "direct", true);
    channel.queueBind("hello", exchangeName, routingKey);

    // Grab the message from the HTML form and publish it to the queue
    String message = request.getParameter("message");
    channel.basicPublish(exchangeName, routingKey, null, message.getBytes());
    writer.println(" Message sent to queue '" + message + "'");

    boolean autoAck = false;

    // Get the response message
    GetResponse responseMsg = channel.basicGet("hello", autoAck);

    if (responseMsg == null) {
        // No message retrieved.
    } else {
        byte[] body = responseMsg.getBody();
        // Since getBody() returns a byte array, convert to a string for
        // the user.
        String bodyString = new String(body);
        long deliveryTag = responseMsg.getEnvelope().getDeliveryTag();

        writer.println("Message received: " + bodyString);

        // Acknowledge that we received the message so that the queue
        // removes the message so that it's not sent to us again.
        channel.basicAck(deliveryTag, false);
    }

    writer.close();
}

From source file:org.mule.transport.amqp.internal.client.MessageConsumer.java

License:Open Source License

public AmqpMessage consumeMessage(final Channel channel, final String queue, final boolean autoAck,
        final long timeout) throws IOException, InterruptedException {
    final long startTime = System.currentTimeMillis();

    // try first with a basic get to potentially quickly retrieve a pending message
    final GetResponse getResponse = channel.basicGet(queue, autoAck);

    // if timeout is zero or if a message has been fetched don't go any further
    if ((timeout == 0) || (getResponse != null)) {
        return getResponse == null ? null
                : new AmqpMessage(null, getResponse.getEnvelope(), getResponse.getProps(),
                        getResponse.getBody());
    }/*from w w w.  j a  va 2  s  .  com*/

    // account for the time taken to perform the basic get
    final long elapsedTime = System.currentTimeMillis() - startTime;
    final long actualTimeOut = timeout - elapsedTime;
    if (actualTimeOut < 0) {
        return null;
    }

    final QueueingConsumer consumer = new SingleMessageQueueingConsumer(channel);

    // false -> no AMQP-level autoAck with the SingleMessageQueueingConsumer
    final String consumerTag = channel.basicConsume(queue, false, consumer);
    try {
        final QueueingConsumer.Delivery delivery = consumer.nextDelivery(actualTimeOut);

        if (delivery == null) {
            return null;
        } else {
            if (autoAck) {
                // ack only if auto-ack was requested, otherwise it's up to the caller to ack
                channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
            }

            return new AmqpMessage(consumerTag, delivery.getEnvelope(), delivery.getProperties(),
                    delivery.getBody());
        }
    } finally {
        try {
            channel.basicCancel(consumerTag);
        } catch (IOException e) {
            /**
             * The broker could decide to cancel a subscription on certain situations
             */
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Subscription to channel with consumerTag "
                        + StringUtils.defaultString(consumerTag) + " could not be closed.", e);
            }
        }
    }
}