Search in sources :

Example 1 with MessageAnnotations

use of org.apache.qpid.proton.amqp.messaging.MessageAnnotations in project hono by eclipse.

the class MessageHelper method addAnnotation.

/**
 * Adds a value for a symbol to an AMQP 1.0 message's <em>annotations</em>.
 *
 * @param msg the message to add the symbol to.
 * @param key the name of the symbol to add a value for.
 * @param value the value to add.
 */
public static void addAnnotation(final Message msg, final String key, final Object value) {
    MessageAnnotations annotations = msg.getMessageAnnotations();
    if (annotations == null) {
        annotations = new MessageAnnotations(new HashMap<>());
        msg.setMessageAnnotations(annotations);
    }
    annotations.getValue().put(Symbol.getSymbol(key), value);
}
Also used : HashMap(java.util.HashMap) MessageAnnotations(org.apache.qpid.proton.amqp.messaging.MessageAnnotations)

Example 2 with MessageAnnotations

use of org.apache.qpid.proton.amqp.messaging.MessageAnnotations in project hono by eclipse.

the class RequestResponseApiConstants method getAmqpReply.

/**
 * Creates an AMQP message from a response to a service invocation.
 *
 * @param endpoint The service endpoint that the operation has been invoked on.
 * @param response The response message.
 * @return The AMQP message.
 * @throws NullPointerException if endpoint is {@code null}.
 */
public static final Message getAmqpReply(final String endpoint, final EventBusMessage response) {
    Objects.requireNonNull(endpoint);
    Objects.requireNonNull(response);
    final Object correlationId = response.getCorrelationId();
    if (correlationId == null) {
        throw new IllegalArgumentException("response must contain correlation ID");
    } else {
        final String tenantId = response.getTenant();
        final String deviceId = response.getDeviceId();
        final Integer status = response.getStatus();
        final boolean isApplCorrelationId = response.isAppCorrelationId();
        final String cacheDirective = response.getCacheDirective();
        final JsonObject payload = response.getJsonPayload();
        final ResourceIdentifier address = ResourceIdentifier.from(endpoint, tenantId, deviceId);
        final Message message = ProtonHelper.message();
        message.setMessageId(UUID.randomUUID().toString());
        message.setCorrelationId(correlationId);
        message.setAddress(address.toString());
        final Map<String, Object> map = new HashMap<>();
        map.put(MessageHelper.APP_PROPERTY_TENANT_ID, tenantId);
        map.put(MessageHelper.APP_PROPERTY_STATUS, status);
        if (deviceId != null) {
            map.put(MessageHelper.APP_PROPERTY_DEVICE_ID, deviceId);
        }
        if (cacheDirective != null) {
            map.put(MessageHelper.APP_PROPERTY_CACHE_CONTROL, cacheDirective);
        }
        message.setApplicationProperties(new ApplicationProperties(map));
        if (isApplCorrelationId) {
            final Map<Symbol, Object> annotations = new HashMap<>();
            annotations.put(Symbol.valueOf(MessageHelper.ANNOTATION_X_OPT_APP_CORRELATION_ID), true);
            message.setMessageAnnotations(new MessageAnnotations(annotations));
        }
        if (payload != null) {
            message.setContentType(CONTENT_TYPE_APPLICATION_JSON);
            message.setBody(new AmqpValue(payload.encode()));
        }
        return message;
    }
}
Also used : Message(org.apache.qpid.proton.message.Message) HashMap(java.util.HashMap) Symbol(org.apache.qpid.proton.amqp.Symbol) JsonObject(io.vertx.core.json.JsonObject) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) MessageAnnotations(org.apache.qpid.proton.amqp.messaging.MessageAnnotations) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) JsonObject(io.vertx.core.json.JsonObject)

Example 3 with MessageAnnotations

use of org.apache.qpid.proton.amqp.messaging.MessageAnnotations in project azure-service-bus-java by Azure.

the class MessageConverter method convertAmqpMessageToBrokeredMessage.

public static Message convertAmqpMessageToBrokeredMessage(org.apache.qpid.proton.message.Message amqpMessage, byte[] deliveryTag) {
    Message brokeredMessage;
    Section body = amqpMessage.getBody();
    if (body != null) {
        if (body instanceof Data) {
            Binary messageData = ((Data) body).getValue();
            brokeredMessage = new Message(messageData.getArray());
        } else {
            // TODO: handle other types of message body
            brokeredMessage = new Message();
        }
    } else {
        brokeredMessage = new Message();
    }
    // Application properties
    ApplicationProperties applicationProperties = amqpMessage.getApplicationProperties();
    if (applicationProperties != null) {
        brokeredMessage.setProperties(applicationProperties.getValue());
    }
    // Header
    brokeredMessage.setTimeToLive(Duration.ofMillis(amqpMessage.getTtl()));
    brokeredMessage.setDeliveryCount(amqpMessage.getDeliveryCount());
    // Properties
    brokeredMessage.setMessageId(amqpMessage.getMessageId().toString());
    brokeredMessage.setContentType(amqpMessage.getContentType());
    Object correlationId = amqpMessage.getCorrelationId();
    if (correlationId != null) {
        brokeredMessage.setCorrelationId(amqpMessage.getCorrelationId().toString());
    }
    brokeredMessage.setLabel(amqpMessage.getSubject());
    brokeredMessage.setTo(amqpMessage.getProperties().getTo());
    brokeredMessage.setReplyTo(amqpMessage.getReplyTo());
    brokeredMessage.setReplyToSessionId(amqpMessage.getReplyToGroupId());
    brokeredMessage.setSessionId(amqpMessage.getGroupId());
    // Message Annotations
    MessageAnnotations messageAnnotations = amqpMessage.getMessageAnnotations();
    if (messageAnnotations != null) {
        Map<Symbol, Object> messageAnnotationsMap = messageAnnotations.getValue();
        if (messageAnnotationsMap != null) {
            for (Map.Entry<Symbol, Object> entry : messageAnnotationsMap.entrySet()) {
                String entryName = entry.getKey().toString();
                switch(entryName) {
                    case ClientConstants.ENQUEUEDTIMEUTCNAME:
                        brokeredMessage.setEnqueuedTimeUtc(((Date) entry.getValue()).toInstant());
                        break;
                    case ClientConstants.SCHEDULEDENQUEUETIMENAME:
                        brokeredMessage.setScheduledEnqueuedTimeUtc(((Date) entry.getValue()).toInstant());
                        break;
                    case ClientConstants.SEQUENCENUBMERNAME:
                        brokeredMessage.setSequenceNumber((long) entry.getValue());
                        break;
                    case ClientConstants.LOCKEDUNTILNAME:
                        brokeredMessage.setLockedUntilUtc(((Date) entry.getValue()).toInstant());
                        break;
                    case ClientConstants.PARTITIONKEYNAME:
                        brokeredMessage.setPartitionKey((String) entry.getValue());
                        break;
                    case ClientConstants.DEADLETTERSOURCENAME:
                        brokeredMessage.setDeadLetterSource((String) entry.getValue());
                        break;
                }
            }
        }
    }
    if (deliveryTag != null && deliveryTag.length == ClientConstants.LOCKTOKENSIZE) {
        UUID lockToken = Util.convertDotNetBytesToUUID(deliveryTag);
        brokeredMessage.setLockToken(lockToken);
    } else {
        brokeredMessage.setLockToken(ClientConstants.ZEROLOCKTOKEN);
    }
    brokeredMessage.setDeliveryTag(deliveryTag);
    return brokeredMessage;
}
Also used : Symbol(org.apache.qpid.proton.amqp.Symbol) Data(org.apache.qpid.proton.amqp.messaging.Data) Section(org.apache.qpid.proton.amqp.messaging.Section) MessageAnnotations(org.apache.qpid.proton.amqp.messaging.MessageAnnotations) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) Binary(org.apache.qpid.proton.amqp.Binary) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with MessageAnnotations

use of org.apache.qpid.proton.amqp.messaging.MessageAnnotations in project azure-service-bus-java by Azure.

the class Util method getDataSerializedSize.

// Remove this.. Too many cases, too many types...
public static int getDataSerializedSize(Message amqpMessage) {
    if (amqpMessage == null) {
        return 0;
    }
    int payloadSize = getPayloadSize(amqpMessage);
    // EventData - accepts only PartitionKey - which is a String & stuffed into MessageAnnotation
    MessageAnnotations messageAnnotations = amqpMessage.getMessageAnnotations();
    ApplicationProperties applicationProperties = amqpMessage.getApplicationProperties();
    int annotationsSize = 0;
    int applicationPropertiesSize = 0;
    if (messageAnnotations != null) {
        annotationsSize += Util.sizeof(messageAnnotations.getValue());
    }
    if (applicationProperties != null) {
        applicationPropertiesSize += Util.sizeof(applicationProperties.getValue());
    }
    return annotationsSize + applicationPropertiesSize + payloadSize;
}
Also used : MessageAnnotations(org.apache.qpid.proton.amqp.messaging.MessageAnnotations) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties)

Example 5 with MessageAnnotations

use of org.apache.qpid.proton.amqp.messaging.MessageAnnotations in project activemq-artemis by apache.

the class AMQPMessageSupportTest method testGetMessageAnnotationWhenMessageHasAnnotationsMap.

// ---------- getMessageAnnotation ----------------------------------------//
@Test
public void testGetMessageAnnotationWhenMessageHasAnnotationsMap() {
    Map<Symbol, Object> messageAnnotationsMap = new HashMap<>();
    messageAnnotationsMap.put(Symbol.valueOf("x-opt-test"), Boolean.TRUE);
    Message message = Proton.message();
    message.setMessageAnnotations(new MessageAnnotations(messageAnnotationsMap));
    assertNotNull(AMQPMessageSupport.getMessageAnnotation("x-opt-test", message));
}
Also used : Message(org.apache.qpid.proton.message.Message) HashMap(java.util.HashMap) Symbol(org.apache.qpid.proton.amqp.Symbol) MessageAnnotations(org.apache.qpid.proton.amqp.messaging.MessageAnnotations) Test(org.junit.Test)

Aggregations

MessageAnnotations (org.apache.qpid.proton.amqp.messaging.MessageAnnotations)24 Symbol (org.apache.qpid.proton.amqp.Symbol)19 HashMap (java.util.HashMap)15 ApplicationProperties (org.apache.qpid.proton.amqp.messaging.ApplicationProperties)11 Message (org.apache.qpid.proton.message.Message)10 AMQPMessage (org.apache.activemq.artemis.protocol.amqp.broker.AMQPMessage)6 AmqpValue (org.apache.qpid.proton.amqp.messaging.AmqpValue)6 ICoreMessage (org.apache.activemq.artemis.api.core.ICoreMessage)5 ServerJMSMessage (org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSMessage)5 Binary (org.apache.qpid.proton.amqp.Binary)5 Properties (org.apache.qpid.proton.amqp.messaging.Properties)5 ServerJMSBytesMessage (org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSBytesMessage)4 ServerJMSMapMessage (org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSMapMessage)4 ServerJMSObjectMessage (org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSObjectMessage)4 ServerJMSStreamMessage (org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSStreamMessage)4 ServerJMSTextMessage (org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSTextMessage)4 Section (org.apache.qpid.proton.amqp.messaging.Section)4 Map (java.util.Map)3 Data (org.apache.qpid.proton.amqp.messaging.Data)3 Header (org.apache.qpid.proton.amqp.messaging.Header)3