Java Code Examples for org.apache.qpid.proton.message.Message#getMessageAnnotations()

The following examples show how to use org.apache.qpid.proton.message.Message#getMessageAnnotations() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AmqpMessage.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new AmqpMessage that wraps the information necessary to handle
 * an incoming delivery.
 *
 * @param receiver the AmqpReceiver that received this message.
 * @param message  the Proton message that was received.
 * @param delivery the Delivery instance that produced this message.
 */
@SuppressWarnings("unchecked")
public AmqpMessage(AmqpReceiver receiver, Message message, Delivery delivery) {
   this.receiver = receiver;
   this.message = message;
   this.delivery = delivery;

   if (message.getMessageAnnotations() != null) {
      messageAnnotationsMap = message.getMessageAnnotations().getValue();
   }

   if (message.getApplicationProperties() != null) {
      applicationPropertiesMap = message.getApplicationProperties().getValue();
   }

   if (message.getDeliveryAnnotations() != null) {
      deliveryAnnotationsMap = message.getDeliveryAnnotations().getValue();
   }
}
 
Example 2
Source File: MessageHelper.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns the value to which the specified key is mapped in the message annotations, or {@code null} if the message
 * annotations contain no mapping for the key.
 *
 * @param <T> the expected type of the property to read.
 * @param msg the message that contains the annotations.
 * @param key the name of the symbol to return a value for.
 * @param type the expected type of the value.
 * @return the annotation's value or {@code null} if no such annotation exists or its value is not of the expected
 *         type.
 */
@SuppressWarnings("unchecked")
public static <T> T getAnnotation(final Message msg, final String key, final Class<T> type) {
    final MessageAnnotations annotations = msg.getMessageAnnotations();
    if (annotations == null) {
        return null;
    } else {
        final Object value = annotations.getValue().get(Symbol.getSymbol(key));
        if (type.isInstance(value)) {
            return (T) value;
        } else {
            return null;
        }
    }
}
 
Example 3
Source File: AmqpRawMessageConverter.java    From strimzi-kafka-bridge with Apache License 2.0 5 votes vote down vote up
@Override
public KafkaProducerRecord<String, byte[]> toKafkaRecord(String kafkaTopic, Integer partition, Message message) {

    Object partitionFromMessage = null, key = null;
    byte[] value;
    byte[] buffer = new byte[AmqpRawMessageConverter.BUFFER_SIZE];

    // get topic and body from AMQP message
    String topic = (message.getAddress() == null) ?
            kafkaTopic :
            message.getAddress().replace('/', '.');

    int encoded = message.encode(buffer, 0, AmqpRawMessageConverter.BUFFER_SIZE);
    value = Arrays.copyOfRange(buffer, 0, encoded);

    // get partition and key from AMQP message annotations
    // NOTE : they are not mandatory
    MessageAnnotations messageAnnotations = message.getMessageAnnotations();

    if (messageAnnotations != null) {

        partitionFromMessage = messageAnnotations.getValue().get(Symbol.getSymbol(AmqpBridge.AMQP_PARTITION_ANNOTATION));
        key = messageAnnotations.getValue().get(Symbol.getSymbol(AmqpBridge.AMQP_KEY_ANNOTATION));

        if (partitionFromMessage != null && !(partitionFromMessage instanceof Integer))
            throw new IllegalArgumentException("The partition annotation must be an Integer");

        if (key != null && !(key instanceof String))
            throw new IllegalArgumentException("The key annotation must be a String");
    }

    // build the record for the KafkaProducer and then send it
    KafkaProducerRecord<String, byte[]> record = KafkaProducerRecord.create(topic, (String) key, value, (Integer) partitionFromMessage);
    return record;
}
 
Example 4
Source File: AmqpSinkBridgeEndpointMockTest.java    From strimzi-kafka-bridge with Apache License 2.0 4 votes vote down vote up
@Test
@Disabled
public <K, V> void normalFlow_AtMostOnce() throws Exception {
    String topic = "my_topic";
    Vertx vertx = Vertx.vertx();
    MockRecordProducer recordProducer = new MockRecordProducer(topic, 0, 0L);
    AmqpSinkBridgeEndpoint<K, V> endpoint = (AmqpSinkBridgeEndpoint) new AmqpSinkBridgeEndpoint<>(vertx, BridgeConfig.fromMap(config),
            EmbeddedFormat.JSON, new StringDeserializer(), new ByteArrayDeserializer());
    endpoint.open();

    // Create a mock for the sender
    ProtonSender mockSender = mockSender(ProtonQoS.AT_MOST_ONCE, topic + "/group.id/my_group");

    // Call handle()
    endpoint.handle(new AmqpEndpoint(mockSender));

    // Now the consumer is set we can add a spy for it
    // ( so we can inspect KafkaConsumer.commit() )
    KafkaConsumer<K, V> consumerSpy = installConsumerSpy(endpoint);

    // Simulate vertx-kafka-client delivering a record
    Method handler = endpoint.getClass().getSuperclass().getDeclaredMethod("handleKafkaRecord", KafkaConsumerRecord.class);
    handler.setAccessible(true);
    handler.invoke(endpoint, recordProducer.mockRecord(null, () -> "Hello, world".getBytes()));

    ArgumentCaptor<Handler<AsyncResult<Void>>> handlerCap = ArgumentCaptor.forClass(Handler.class);

    verify(consumerSpy).commit(handlerCap.capture());
    handlerCap.getValue().handle(new AsyncResult<Void>() {

        @Override
        public Void result() {
            return null;
        }

        @Override
        public Throwable cause() {
            return null;
        }

        @Override
        public boolean succeeded() {
            return true;
        }

        @Override
        public boolean failed() {
            return false;
        }
    });

    // verify sender.send() was called and grab the arguments
    ArgumentCaptor<byte[]> tagCap = ArgumentCaptor.forClass(byte[].class);
    ArgumentCaptor<Message> messageCap = ArgumentCaptor.forClass(Message.class);
    verify(mockSender).send(tagCap.capture(), messageCap.capture());
    Message message = messageCap.getValue();
    // Assert the transformed message was as expected
    assertThat(topic + "/group.id/my_group", is(message.getAddress()));

    assertThat(((Data) message.getBody()).getValue().getArray(), is("Hello, world".getBytes()));
    MessageAnnotations messageAnnotations = message.getMessageAnnotations();
    assertThat(messageAnnotations.getValue().get(Symbol.valueOf(AmqpBridge.AMQP_TOPIC_ANNOTATION)), is(topic));
    assertThat(messageAnnotations.getValue().get(Symbol.valueOf(AmqpBridge.AMQP_PARTITION_ANNOTATION)), is(0));
    assertThat(messageAnnotations.getValue().get(Symbol.valueOf(AmqpBridge.AMQP_OFFSET_ANNOTATION)), is(0L));

    // TODO test closure (commit)
}
 
Example 5
Source File: AmqpSinkBridgeEndpointMockTest.java    From strimzi-kafka-bridge with Apache License 2.0 4 votes vote down vote up
@Test
public <K, V> void normalFlow_AtLeastOnce() throws Exception {
    String topic = "my_topic";
    Vertx vertx = Vertx.vertx();
    MockRecordProducer recordProducer = new MockRecordProducer(topic, 0, 0L);
    AmqpSinkBridgeEndpoint<K, V> endpoint = (AmqpSinkBridgeEndpoint) new AmqpSinkBridgeEndpoint<>(vertx, BridgeConfig.fromMap(config),
            EmbeddedFormat.JSON, new StringDeserializer(), new ByteArrayDeserializer());
    endpoint.open();

    // Create a mock for the sender
    ProtonSender mockSender = mockSender(ProtonQoS.AT_LEAST_ONCE, topic + "/group.id/my_group");

    // Call handle()
    endpoint.handle(new AmqpEndpoint(mockSender));

    // Now the consumer is set we can add a spy for it
    // ( so we can inspect KafkaConsumer.commit() )
    KafkaConsumer<K, V> consumerSpy = installConsumerSpy(endpoint);

    // Simulate vertx-kafka-client delivering a batch
    Method batchHandler = endpoint.getClass().getSuperclass().getDeclaredMethod("handleKafkaBatch", KafkaConsumerRecords.class);
    batchHandler.setAccessible(true);
    KafkaConsumerRecords<String, byte[]> mockRecords = mockRecords();

    // Simulate vertx-kafka-client delivering a record
    Method handler = endpoint.getClass().getSuperclass().getDeclaredMethod("handleKafkaRecord", KafkaConsumerRecord.class);
    handler.setAccessible(true);

    // Kafka batch of 1
    batchHandler.invoke(endpoint, mockRecords);
    handler.invoke(endpoint, recordProducer.mockRecord(null, () -> "Hello, world".getBytes()));

    // verify sender.send() was called and grab the arguments
    ArgumentCaptor<byte[]> tagCap = ArgumentCaptor.forClass(byte[].class);
    ArgumentCaptor<Message> messageCap = ArgumentCaptor.forClass(Message.class);
    ArgumentCaptor<Handler<ProtonDelivery>> handlerCap = ArgumentCaptor.forClass(Handler.class);
    verify(mockSender).send(tagCap.capture(), messageCap.capture(), handlerCap.capture());
    Message message = messageCap.getValue();

    // Assert the transformed message was as expected
    assertThat(message.getAddress(), is(topic + "/group.id/my_group"));
    assertThat(((Data) message.getBody()).getValue().getArray(), is("Hello, world".getBytes()));
    MessageAnnotations messageAnnotations = message.getMessageAnnotations();
    assertThat(messageAnnotations.getValue().get(Symbol.valueOf(AmqpBridge.AMQP_TOPIC_ANNOTATION)), is(topic));
    assertThat(messageAnnotations.getValue().get(Symbol.valueOf(AmqpBridge.AMQP_PARTITION_ANNOTATION)), is(0));
    assertThat(messageAnnotations.getValue().get(Symbol.valueOf(AmqpBridge.AMQP_OFFSET_ANNOTATION)), is(0L));

    // Simulate Proton delivering settlement
    ProtonDelivery mockDelivery = mock(ProtonDelivery.class);
    when(mockDelivery.getTag()).thenReturn(tagCap.getValue());
    handlerCap.getValue().handle(mockDelivery);

    // We now have to deliver another batch
    // because the AMQP delivery callback for the first message
    // fires after commitOffsets() is called for the last message of the first batch

    // Kafka batch of 1
    batchHandler.invoke(endpoint, mockRecords);
    handler.invoke(endpoint, recordProducer.mockRecord(null, () -> "Hello, world".getBytes()));

    ArgumentCaptor<Map<TopicPartition, OffsetAndMetadata>> commitMapCap = ArgumentCaptor.forClass(Map.class);
    verify(consumerSpy).commit(commitMapCap.capture(), any(Handler.class));

    // TODO test closure (commit)
}
 
Example 6
Source File: AMQPMessageSupport.java    From activemq-artemis with Apache License 2.0 3 votes vote down vote up
/**
 * Safe way to access message annotations which will check internal structure and either
 * return the annotation if it exists or null if the annotation or any annotations are
 * present.
 *
 * @param key
 *        the String key to use to lookup an annotation.
 * @param message
 *        the AMQP message object that is being examined.
 *
 * @return the given annotation value or null if not present in the message.
 */
public static Object getMessageAnnotation(String key, Message message) {
   if (message != null && message.getMessageAnnotations() != null) {
      Map<Symbol, Object> annotations = message.getMessageAnnotations().getValue();
      return annotations.get(AMQPMessageSupport.getSymbol(key));
   }

   return null;
}