Example usage for com.rabbitmq.client Delivery getEnvelope

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

Introduction

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

Prototype

public Envelope getEnvelope() 

Source Link

Document

Retrieve the message envelope.

Usage

From source file:org.apache.pig.piggybank.squeal.metrics.RMQMetricsTransport.java

License:Apache License

private void runListener(String queueName, final OutputStream os) throws Exception {
    if (queueName != null) {
        channel.queueDeclare(queueName, true, false, false, null);
    } else {// w  w  w.ja v a  2 s.c  om
        queueName = channel.queueDeclare().getQueue();
    }

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

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(queueName, false, consumer);
    int c = 0;

    while (true) {
        Delivery d = consumer.nextDelivery(5000);
        c += 1;
        if (d != null) {
            os.write(d.getBody());
            channel.basicAck(d.getEnvelope().getDeliveryTag(), true);
        }
        if (d == null || c > 1000) {
            os.flush();
            c = 0;
        }
    }
}

From source file:org.apache.pig.piggybank.squeal.spout.RMQSpout.java

License:Apache License

public void nextTuple() {
    try {//ww w .j a  v  a2 s  . c  o  m
        Delivery d = consumer.nextDelivery(0);
        if (d != null) {
            long tag = d.getEnvelope().getDeliveryTag();
            int an_id = r.nextInt();
            out_id.put(an_id, tag);
            collector.emit(new Values(d.getBody()), an_id);
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}

From source file:org.eclipse.ditto.services.connectivity.messaging.rabbitmq.RabbitMQConsumerActor.java

License:Open Source License

private void handleDelivery(final Delivery delivery) {
    final BasicProperties properties = delivery.getProperties();
    final Envelope envelope = delivery.getEnvelope();
    final byte[] body = delivery.getBody();
    final String hashKey = envelope.getExchange() + ":" + envelope.getRoutingKey();

    Map<String, String> headers = null;
    try {/*ww  w .  jav a2 s.  c  o m*/
        final String correlationId = properties.getCorrelationId();
        LogUtil.enhanceLogWithCorrelationId(log, correlationId);
        if (log.isDebugEnabled()) {
            log.debug("Received message from RabbitMQ ({}//{}): {}", envelope, properties,
                    new String(body, StandardCharsets.UTF_8));
        }
        headers = extractHeadersFromMessage(properties, envelope);
        final ExternalMessageBuilder externalMessageBuilder = ExternalMessageFactory
                .newExternalMessageBuilder(headers);
        final String contentType = properties.getContentType();
        final String text = new String(body, CharsetDeterminer.getInstance().apply(contentType));
        if (shouldBeInterpretedAsBytes(contentType)) {
            externalMessageBuilder.withBytes(body);
        } else {
            externalMessageBuilder.withTextAndBytes(text, body);
        }
        externalMessageBuilder.withAuthorizationContext(authorizationContext);
        externalMessageBuilder.withEnforcement(headerEnforcementFilterFactory.getFilter(headers));
        externalMessageBuilder.withHeaderMapping(headerMapping);
        externalMessageBuilder.withSourceAddress(sourceAddress);
        final ExternalMessage externalMessage = externalMessageBuilder.build();
        inboundMonitor.success(externalMessage);
        forwardToMappingActor(externalMessage, hashKey);
    } catch (final DittoRuntimeException e) {
        log.warning("Processing delivery {} failed: {}", envelope.getDeliveryTag(), e.getMessage());
        if (headers != null) {
            // send response if headers were extracted successfully
            forwardToMappingActor(e.setDittoHeaders(DittoHeaders.of(headers)), hashKey);
            inboundMonitor.failure(headers, e);
        } else {
            inboundMonitor.failure(e);
        }
    } catch (final Exception e) {
        log.warning("Processing delivery {} failed: {}", envelope.getDeliveryTag(), e.getMessage());
        if (headers != null) {
            inboundMonitor.exception(headers, e);
        } else {
            inboundMonitor.exception(e);
        }
    }
}

From source file:org.mule.transport.amqp.AmqpConnector.java

License:Open Source License

public AmqpMessage consume(final Channel channel, final String queue, final boolean autoAck, final long timeout)
        throws IOException, InterruptedException {
    final QueueingConsumer consumer = new QueueingConsumer(channel);
    final String consumerTag = channel.basicConsume(queue, autoAck, consumer);
    final Delivery delivery = consumer.nextDelivery(timeout);
    channel.basicCancel(consumerTag);//w  w w.j  a v a2 s.c  o  m

    if (delivery == null)
        return null;

    return new AmqpMessage(consumerTag, delivery.getEnvelope(), delivery.getProperties(), delivery.getBody());
}

From source file:org.mule.transport.rmq.RmqMessagePollingReceiver.java

License:Open Source License

public void poll() throws Exception {

    //Wait for a new message to arrive, then send it to mule for handling.

    Delivery delivery = null;

    if (timeout == -1) {
        delivery = consumer.nextDelivery();
    } else {// ww  w  .  j a  v  a  2  s  .c  om
        delivery = consumer.nextDelivery(timeout);
    }

    channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);

    MessageAdapter adapter = connector.getMessageAdapter(delivery);
    routeMessage(new DefaultMuleMessage(adapter), endpoint.isSynchronous());
}

From source file:org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.java

License:Apache License

/**
 * If this is a non-POISON non-null delivery simply return it.
 * If this is POISON we are in shutdown mode, throw
 * shutdown. If delivery is null, we may be in shutdown mode. Check and see.
 * @throws InterruptedException/*from  w ww. j  a  v  a 2s  .co m*/
 */
private Message handle(Delivery delivery) throws InterruptedException {
    if ((delivery == null && shutdown != null)) {
        throw shutdown;
    }
    if (delivery == null) {
        return null;
    }
    byte[] body = delivery.getBody();
    Envelope envelope = delivery.getEnvelope();

    MessageProperties messageProperties = this.messagePropertiesConverter
            .toMessageProperties(delivery.getProperties(), envelope, "UTF-8");
    messageProperties.setMessageCount(0);
    Message message = new Message(body, messageProperties);
    if (logger.isDebugEnabled()) {
        logger.debug("Received message: " + message);
    }
    deliveryTags.add(messageProperties.getDeliveryTag());
    return message;
}

From source file:org.springframework.amqp.rabbit.listener.UnackedRawIntegrationTests.java

License:Apache License

@Test
public void testOnePublishUnackedRequeued() throws Exception {

    noTxChannel.basicPublish("", "test.queue", null, "foo".getBytes());

    QueueingConsumer callback = new QueueingConsumer(txChannel);
    txChannel.basicConsume("test.queue", callback);
    Delivery next = callback.nextDelivery(1000L);
    assertNotNull(next);/*from   ww  w  .j a  va 2 s  . c  om*/
    txChannel.basicReject(next.getEnvelope().getDeliveryTag(), true);
    txChannel.txRollback();

    GetResponse get = noTxChannel.basicGet("test.queue", true);
    assertNotNull(get);

}

From source file:org.springframework.amqp.rabbit.listener.UnackedRawIntegrationTests.java

License:Apache License

@Test
public void testFourPublishUnackedRequeued() throws Exception {

    noTxChannel.basicPublish("", "test.queue", null, "foo".getBytes());
    noTxChannel.basicPublish("", "test.queue", null, "bar".getBytes());
    noTxChannel.basicPublish("", "test.queue", null, "one".getBytes());
    noTxChannel.basicPublish("", "test.queue", null, "two".getBytes());

    QueueingConsumer callback = new QueueingConsumer(txChannel);
    txChannel.basicConsume("test.queue", callback);
    Delivery next = callback.nextDelivery(1000L);
    assertNotNull(next);//from w w w  . j  av  a2 s .  c om
    txChannel.basicReject(next.getEnvelope().getDeliveryTag(), true);
    txChannel.txRollback();

    GetResponse get = noTxChannel.basicGet("test.queue", true);
    assertNotNull(get);

}

From source file:org.zenoss.amqp.impl.ConsumerImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
private Message<T> createMessage(Delivery delivery) throws AmqpException {
    final T body;
    final byte[] rawBody;
    final MessageProperties properties = new BasicPropertiesWrapper(delivery.getProperties());
    final MessageEnvelope envelope = new EnvelopeWrapper(delivery.getEnvelope());
    if ("deflate".equalsIgnoreCase(properties.getContentEncoding())) {
        try {/* w ww.  j  a  va  2  s . c  o m*/
            rawBody = deflateDecompress(delivery.getBody());
        } catch (IOException e) {
            logger.warn("Failed to decompress message", e);
            // Throw MessageDecoderException so we don't loop attempting to read invalid message
            throw new MessageDecoderException(
                    DefaultMessage.newMessage(delivery.getBody(), properties, envelope), e);
        }
    } else {
        rawBody = delivery.getBody();
    }
    if (converter == null) {
        body = (T) rawBody;
    } else {
        try {
            body = converter.fromBytes(rawBody, properties);
        } catch (Exception e) {
            /* Throw exception with original received message on failure */
            throw new MessageDecoderException(DefaultMessage.newMessage(rawBody, properties, envelope), e);
        }
        /* Throw exception if we failed to convert the message */
        if (body == null) {
            throw new MessageDecoderException(DefaultMessage.newMessage(rawBody, properties, envelope));
        }
    }
    return DefaultMessage.newMessage(body, properties, envelope);
}