Example usage for com.rabbitmq.client Envelope getDeliveryTag

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

Introduction

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

Prototype

public long getDeliveryTag() 

Source Link

Document

Get the delivery tag included in this parameter envelope

Usage

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

License:Open Source License

public static void checkInboundProperties(final AmqpMessage amqpMessage, final MuleMessage muleMessage) {
    assertEquals(amqpMessage.getConsumerTag(),
            muleMessage.getProperty(AmqpConstants.CONSUMER_TAG, PropertyScope.INBOUND));

    final Envelope envelope = amqpMessage.getEnvelope();
    assertEquals(envelope.getDeliveryTag(),
            muleMessage.getProperty(AmqpConstants.DELIVERY_TAG, PropertyScope.INBOUND));
    assertEquals(envelope.isRedeliver(),
            muleMessage.getProperty(AmqpConstants.REDELIVER, PropertyScope.INBOUND));
    assertEquals(envelope.getExchange(),
            muleMessage.getProperty(AmqpConstants.EXCHANGE, PropertyScope.INBOUND));
    assertEquals(envelope.getRoutingKey(),
            muleMessage.getProperty(AmqpConstants.ROUTING_KEY, PropertyScope.INBOUND));

    final BasicProperties amqpProperties = amqpMessage.getProperties();
    assertEquals(amqpProperties.getAppId(),
            muleMessage.getProperty(AmqpConstants.APP_ID, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getContentEncoding(),
            muleMessage.getProperty(AmqpConstants.CONTENT_ENCODING, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getContentType(),
            muleMessage.getProperty(AmqpConstants.CONTENT_TYPE, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getCorrelationId(),
            muleMessage.getProperty(AmqpConstants.CORRELATION_ID, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getCorrelationId(), muleMessage.getCorrelationId());
    assertEquals(amqpProperties.getDeliveryMode(),
            muleMessage.getProperty(AmqpConstants.DELIVERY_MODE, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getExpiration(),
            muleMessage.getProperty(AmqpConstants.EXPIRATION, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getMessageId(),
            muleMessage.getProperty(AmqpConstants.MESSAGE_ID, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getMessageId(), muleMessage.getUniqueId());
    assertEquals(amqpProperties.getPriority(),
            muleMessage.getProperty(AmqpConstants.PRIORITY, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getReplyTo(),
            muleMessage.getProperty(AmqpConstants.REPLY_TO, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getReplyTo(), muleMessage.getReplyTo());
    assertEquals(amqpProperties.getTimestamp(),
            muleMessage.getProperty(AmqpConstants.TIMESTAMP, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getType(), muleMessage.getProperty(AmqpConstants.TYPE, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getUserId(),
            muleMessage.getProperty(AmqpConstants.USER_ID, PropertyScope.INBOUND));

    for (final Entry<String, Object> header : amqpProperties.getHeaders().entrySet()) {
        assertEquals(header.getValue(), muleMessage.getProperty(header.getKey(), PropertyScope.INBOUND));
    }/*from  w w w.  j  a  va 2s . c o m*/
}

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

License:Open Source License

@Override
public void handleDelivery(final String consumerTag, final Envelope envelope,
        final AMQP.BasicProperties properties, final byte[] body) throws IOException {
    if (received.get()) {
        // extra messages could have been received before basicCancel happens -> actively
        // reject them
        getChannel().basicReject(envelope.getDeliveryTag(), true);
    } else {//  w  w  w.  j ava2 s.  co m
        received.set(true);
        super.handleDelivery(consumerTag, envelope, properties, body);
    }
}

From source file:org.mule.transport.amqp.internal.domain.AmqpMuleMessageFactory.java

License:Open Source License

private void addEnvelopeProperties(final Envelope envelope, final Map<String, Object> messageProperties) {
    if (envelope == null)
        return;//from  w w w . ja va2 s.co  m

    putIfNonNull(AmqpConnector.MESSAGE_PROPERTY_DELIVERY_TAG, envelope.getDeliveryTag(), messageProperties);
    putIfNonNull(AmqpConnector.MESSAGE_PROPERTY_REDELIVER, envelope.isRedeliver(), messageProperties);
    putIfNonNull(AmqpConnector.EXCHANGE, envelope.getExchange(), messageProperties);
    putIfNonNull(AmqpConnector.MESSAGE_PROPERTY_ROUTING_KEY, envelope.getRoutingKey(), messageProperties);
}

From source file:org.mule.transport.amqp.internal.domain.AmqpMuleMessageFactoryTestCase.java

License:Open Source License

public static void checkInboundProperties(final AmqpMessage amqpMessage, final MuleMessage muleMessage) {
    assertEquals(amqpMessage.getConsumerTag(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_CONSUMER_TAG, PropertyScope.INBOUND));

    final Envelope envelope = amqpMessage.getEnvelope();
    assertEquals(envelope.getDeliveryTag(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_DELIVERY_TAG, PropertyScope.INBOUND));
    assertEquals(envelope.isRedeliver(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_REDELIVER, PropertyScope.INBOUND));
    assertEquals(envelope.getExchange(),
            muleMessage.getProperty(AmqpConnector.EXCHANGE, PropertyScope.INBOUND));
    assertEquals(envelope.getRoutingKey(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_ROUTING_KEY, PropertyScope.INBOUND));

    final BasicProperties amqpProperties = amqpMessage.getProperties();
    assertEquals(amqpProperties.getAppId(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_APP_ID, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getContentEncoding(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_CONTENT_ENCODING, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getContentType(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_CONTENT_TYPE, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getClusterId(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_CLUSTER_ID, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getCorrelationId(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_CORRELATION_ID, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getCorrelationId(), muleMessage.getCorrelationId());
    assertEquals(amqpProperties.getDeliveryMode(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_DELIVERY_MODE, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getExpiration(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_EXPIRATION, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getMessageId(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_MESSAGE_ID, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getMessageId(), muleMessage.getUniqueId());
    assertEquals(amqpProperties.getPriority(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_PRIORITY, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getReplyTo(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_REPLY_TO, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getReplyTo(), muleMessage.getReplyTo());
    assertEquals(amqpProperties.getTimestamp(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_TIMESTAMP, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getType(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_TYPE, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getUserId(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_USER_ID, PropertyScope.INBOUND));

    for (final Entry<String, Object> header : amqpProperties.getHeaders().entrySet()) {
        assertEquals(header.getValue(), muleMessage.getProperty(header.getKey(), PropertyScope.INBOUND));
    }//from w  w w  .j  a  v  a  2  s.  c o m
}

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

License:Open Source License

public void doStart() {
    //Create a listening consumer.
    consumer = new DefaultConsumer(channel) {
        @Override//from  w ww.j  a v  a 2s.c  o m
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            long deliveryTag = envelope.getDeliveryTag();

            MessageAdapter adapter;
            try {
                adapter = connector.getMessageAdapter(new Delivery(envelope, properties, body));
                routeMessage(new DefaultMuleMessage(adapter), endpoint.isSynchronous());
            } catch (MessagingException e) {
                logger.error(e.getMessage());
            } catch (MuleException e) {
                logger.error(e.getMessage());
            }

            this.getChannel().basicAck(deliveryTag, false);
        }
    };

    try {
        channel.basicConsume(queue, noAck, consumerTag, noLocal, cExclusive, consumer);
    } catch (IOException e) {
        logger.error(e.getMessage());
    }
}

From source file:org.ninjav.rabbitmq.TestReceive.java

@Test
public void canReceiveMessageFromQueue() throws IOException, TimeoutException {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare(QUEUE_NAME, false, false, false, null);

    Consumer consumer;//from   w  w w  .j a  v a  2 s  .c o  m
    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");
                System.out.println("Message Received: " + message);
            } finally {
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        }
    };
    boolean autoAck = false;
    channel.basicConsume(QUEUE_NAME, autoAck, consumer);

    try {
        Thread.sleep(100000);
    } catch (InterruptedException ex) {
        Logger.getLogger(TestReceive.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.openbaton.plugin.PluginListener.java

License:Apache License

@Override
public void run() {

    try {/*from  ww  w .j  av a2s  . c o m*/
        initRabbitMQ();
    } catch (IOException | TimeoutException e) {
        e.printStackTrace();
        return;
    }
    try {

        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                    byte[] body) throws IOException {
                AMQP.BasicProperties props = new AMQP.BasicProperties.Builder()
                        .correlationId(properties.getCorrelationId()).build();

                executor.execute(() -> {
                    String message;
                    try {
                        message = new String(body, "UTF-8");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                        return;
                    }
                    log.trace("Received message");
                    log.trace("Message content received: " + message);

                    PluginAnswer answer = new PluginAnswer();

                    try {
                        answer.setAnswer(executeMethod(message));
                    } catch (InvocationTargetException e) {
                        e.getTargetException().printStackTrace();
                        answer.setException(e.getTargetException());
                    } catch (Exception e) {
                        e.printStackTrace();
                        answer.setException(e);
                    }

                    String response;
                    try {
                        response = gson.toJson(answer);

                        log.trace("Answer is: " + response);
                        log.trace("Reply queue is: " + properties.getReplyTo());

                        channel.basicPublish(exchange, properties.getReplyTo(), props, response.getBytes());

                    } catch (Throwable e) {
                        e.printStackTrace();
                        answer.setException(e);
                        log.trace("Answer is: " + answer);
                        log.trace("Reply queue is: " + properties.getReplyTo());
                        try {
                            channel.basicPublish(exchange, properties.getReplyTo(), props,
                                    gson.toJson(answer).getBytes());

                        } catch (IOException ex) {
                            log.error(String.format("Thread %s got an exception: %s",
                                    Thread.currentThread().getName(), e.getMessage()));
                            e.printStackTrace();
                        }
                    }
                });

                channel.basicAck(envelope.getDeliveryTag(), false);
                log.trace(String.format("Ack %d", envelope.getDeliveryTag()));

                synchronized (this) {
                    this.notify();
                }
            }
        };

        channel.basicConsume(pluginId, false, consumer);

        // Wait and be prepared to consume the message from RPC client.
        while (true) {
            synchronized (consumer) {
                try {
                    consumer.wait();
                } catch (InterruptedException e) {
                    log.info("Ctrl-c received");
                    System.exit(0);
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (connection != null)
            try {
                connection.close();
            } catch (IOException ignored) {
            }
    }

    try {
        if (channel == null || connection == null)
            System.exit(3);
        channel.close();
        connection.close();
    } catch (IOException | TimeoutException e) {
        e.printStackTrace();
    }
}

From source file:org.pascani.dsl.lib.infrastructure.rabbitmq.RabbitMQConsumer.java

License:Open Source License

public void handleDelivery(final String consumerTag, final Envelope envelope, final BasicProperties props,
        final byte[] body) throws IOException {

    Event<?> event = (Event<?>) SerializationUtils.deserialize(body);
    internalDelegateHandling(event);//  w w  w  . j  av a 2s .  c o m

    // Acknowledge the received message after it has been handled
    this.endPoint.channel().basicAck(envelope.getDeliveryTag(), false);
}

From source file:org.smartdeveloperhub.harvesters.it.notification.NotificationConsumer.java

License:Apache License

/**
 * {@inheritDoc}//from  ww  w.  j  a v  a 2  s  .co m
 */
@Override
public void handleDelivery(final String consumerTag, final Envelope envelope, final BasicProperties properties,
        final byte[] body) throws IOException {
    final String payload = new String(body, "UTF-8");
    final String routingKey = envelope.getRoutingKey();
    final AcknowledgeableNotification notification = new AcknowledgeableNotification(super.getChannel(),
            envelope.getDeliveryTag());
    try {
        verifyHeader(properties);
        this.notifications.offer(findHandler(routingKey).suspend(notification, payload));
    } catch (final Exception e) {
        LOGGER.error("Discarding message:\n{}\nReason:\n", payload, e);
        notification.acknowledge();
    }
}

From source file:org.springframework.amqp.rabbit.support.DefaultMessagePropertiesConverter.java

License:Apache License

public MessageProperties toMessageProperties(final BasicProperties source, final Envelope envelope,
        final String charset) {
    MessageProperties target = new MessageProperties();
    Map<String, Object> headers = source.getHeaders();
    if (!CollectionUtils.isEmpty(headers)) {
        for (Map.Entry<String, Object> entry : headers.entrySet()) {
            Object value = entry.getValue();
            if (value instanceof LongString) {
                value = this.convertLongString((LongString) value, charset);
            }/*from  www. j a  v  a 2s .  c  om*/
            target.setHeader(entry.getKey(), value);
        }
    }
    target.setTimestamp(source.getTimestamp());
    target.setMessageId(source.getMessageId());
    target.setUserId(source.getUserId());
    target.setAppId(source.getAppId());
    target.setClusterId(source.getClusterId());
    target.setType(source.getType());
    Integer deliverMode = source.getDeliveryMode();
    if (deliverMode != null) {
        target.setDeliveryMode(MessageDeliveryMode.fromInt(deliverMode));
    }
    target.setExpiration(source.getExpiration());
    target.setPriority(source.getPriority());
    target.setContentType(source.getContentType());
    target.setContentEncoding(source.getContentEncoding());
    String correlationId = source.getCorrelationId();
    if (correlationId != null) {
        try {
            target.setCorrelationId(source.getCorrelationId().getBytes(charset));
        } catch (UnsupportedEncodingException ex) {
            throw new AmqpUnsupportedEncodingException(ex);
        }
    }
    String replyTo = source.getReplyTo();
    if (replyTo != null) {
        target.setReplyTo(replyTo);
    }
    if (envelope != null) {
        target.setReceivedExchange(envelope.getExchange());
        target.setReceivedRoutingKey(envelope.getRoutingKey());
        target.setRedelivered(envelope.isRedeliver());
        target.setDeliveryTag(envelope.getDeliveryTag());
    }
    return target;
}