Search in sources :

Example 11 with MessageAnnotations

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

the class AMQPMessage method setSymbol.

private void setSymbol(Symbol symbol, Object value) {
    MessageAnnotations annotations = getMessageAnnotations();
    if (annotations == null) {
        _messageAnnotations = new MessageAnnotations(new HashMap<>());
        annotations = _messageAnnotations;
    }
    Map<Symbol, Object> mapAnnotations = annotations != null ? annotations.getValue() : null;
    if (mapAnnotations != null) {
        mapAnnotations.put(symbol, value);
    }
}
Also used : HashMap(java.util.HashMap) MessageAnnotations(org.apache.qpid.proton.amqp.messaging.MessageAnnotations) Symbol(org.apache.qpid.proton.amqp.Symbol)

Example 12 with MessageAnnotations

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

the class AMQPMessage method getSymbol.

private Object getSymbol(Symbol symbol) {
    MessageAnnotations annotations = getMessageAnnotations();
    Map<Symbol, Object> mapAnnotations = annotations != null ? annotations.getValue() : null;
    if (mapAnnotations != null) {
        return mapAnnotations.get(symbol);
    }
    return null;
}
Also used : MessageAnnotations(org.apache.qpid.proton.amqp.messaging.MessageAnnotations) Symbol(org.apache.qpid.proton.amqp.Symbol)

Example 13 with MessageAnnotations

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

the class AmqpCoreConverter method populateMessage.

@SuppressWarnings("unchecked")
protected static ServerJMSMessage populateMessage(ServerJMSMessage jms, org.apache.qpid.proton.message.Message amqp) throws Exception {
    Header header = amqp.getHeader();
    if (header != null) {
        jms.setBooleanProperty(JMS_AMQP_HEADER, true);
        if (header.getDurable() != null) {
            jms.setBooleanProperty(JMS_AMQP_HEADER_DURABLE, true);
            jms.setJMSDeliveryMode(header.getDurable().booleanValue() ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT);
        } else {
            jms.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
        }
        if (header.getPriority() != null) {
            jms.setBooleanProperty(JMS_AMQP_HEADER_PRIORITY, true);
            jms.setJMSPriority(header.getPriority().intValue());
        } else {
            jms.setJMSPriority(javax.jms.Message.DEFAULT_PRIORITY);
        }
        if (header.getFirstAcquirer() != null) {
            jms.setBooleanProperty(JMS_AMQP_FIRST_ACQUIRER, header.getFirstAcquirer());
        }
        if (header.getDeliveryCount() != null) {
            // AMQP Delivery Count counts only failed delivers where JMS
            // Delivery Count should include the original delivery in the count.
            jms.setLongProperty("JMSXDeliveryCount", header.getDeliveryCount().longValue() + 1);
        }
    } else {
        jms.setJMSPriority((byte) javax.jms.Message.DEFAULT_PRIORITY);
        jms.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
    }
    final MessageAnnotations ma = amqp.getMessageAnnotations();
    if (ma != null) {
        for (Map.Entry<?, ?> entry : ma.getValue().entrySet()) {
            String key = entry.getKey().toString();
            if ("x-opt-delivery-time".equals(key) && entry.getValue() != null) {
                long deliveryTime = ((Number) entry.getValue()).longValue();
                jms.setLongProperty(HDR_SCHEDULED_DELIVERY_TIME.toString(), deliveryTime);
            } else if ("x-opt-delivery-delay".equals(key) && entry.getValue() != null) {
                long delay = ((Number) entry.getValue()).longValue();
                if (delay > 0) {
                    jms.setLongProperty(HDR_SCHEDULED_DELIVERY_TIME.toString(), System.currentTimeMillis() + delay);
                }
            }
            setProperty(jms, JMS_AMQP_MESSAGE_ANNOTATION_PREFIX + key, entry.getValue());
        }
    }
    final ApplicationProperties ap = amqp.getApplicationProperties();
    if (ap != null) {
        for (Map.Entry<String, Object> entry : (Set<Map.Entry<String, Object>>) ap.getValue().entrySet()) {
            setProperty(jms, entry.getKey(), entry.getValue());
        }
    }
    final Properties properties = amqp.getProperties();
    if (properties != null) {
        if (properties.getMessageId() != null) {
            jms.setJMSMessageID(AMQPMessageIdHelper.INSTANCE.toMessageIdString(properties.getMessageId()));
        }
        Binary userId = properties.getUserId();
        if (userId != null) {
            jms.setStringProperty("JMSXUserID", new String(userId.getArray(), userId.getArrayOffset(), userId.getLength(), StandardCharsets.UTF_8));
        }
        if (properties.getTo() != null) {
            jms.setJMSDestination(new ServerDestination(properties.getTo()));
        }
        if (properties.getSubject() != null) {
            jms.setJMSType(properties.getSubject());
        }
        if (properties.getReplyTo() != null) {
            jms.setJMSReplyTo(new ServerDestination(properties.getReplyTo()));
        }
        if (properties.getCorrelationId() != null) {
            jms.setJMSCorrelationID(AMQPMessageIdHelper.INSTANCE.toCorrelationIdString(properties.getCorrelationId()));
        }
        if (properties.getContentType() != null) {
            jms.setStringProperty(JMS_AMQP_CONTENT_TYPE, properties.getContentType().toString());
        }
        if (properties.getContentEncoding() != null) {
            jms.setStringProperty(JMS_AMQP_CONTENT_ENCODING, properties.getContentEncoding().toString());
        }
        if (properties.getCreationTime() != null) {
            jms.setJMSTimestamp(properties.getCreationTime().getTime());
        }
        if (properties.getGroupId() != null) {
            jms.setStringProperty("_AMQ_GROUP_ID", properties.getGroupId());
        }
        if (properties.getGroupSequence() != null) {
            jms.setIntProperty("JMSXGroupSeq", properties.getGroupSequence().intValue());
        }
        if (properties.getReplyToGroupId() != null) {
            jms.setStringProperty(JMS_AMQP_REPLYTO_GROUP_ID, properties.getReplyToGroupId());
        }
        if (properties.getAbsoluteExpiryTime() != null) {
            jms.setJMSExpiration(properties.getAbsoluteExpiryTime().getTime());
        }
    }
    // If the jms expiration has not yet been set...
    if (header != null && jms.getJMSExpiration() == 0) {
        // Then lets try to set it based on the message ttl.
        long ttl = javax.jms.Message.DEFAULT_TIME_TO_LIVE;
        if (header.getTtl() != null) {
            ttl = header.getTtl().longValue();
        }
        if (ttl == 0) {
            jms.setJMSExpiration(0);
        } else {
            jms.setJMSExpiration(System.currentTimeMillis() + ttl);
        }
    }
    final Footer fp = amqp.getFooter();
    if (fp != null) {
        for (Map.Entry<Object, Object> entry : (Set<Map.Entry<Object, Object>>) fp.getValue().entrySet()) {
            String key = entry.getKey().toString();
            setProperty(jms, JMS_AMQP_FOOTER_PREFIX + key, entry.getValue());
        }
    }
    return jms;
}
Also used : Set(java.util.Set) ServerDestination(org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerDestination) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) TypedProperties(org.apache.activemq.artemis.utils.collections.TypedProperties) Properties(org.apache.qpid.proton.amqp.messaging.Properties) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) Header(org.apache.qpid.proton.amqp.messaging.Header) MessageAnnotations(org.apache.qpid.proton.amqp.messaging.MessageAnnotations) Footer(org.apache.qpid.proton.amqp.messaging.Footer) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) Binary(org.apache.qpid.proton.amqp.Binary) Map(java.util.Map)

Example 14 with MessageAnnotations

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

the class AmqpsSenderLinkHandler method iotHubMessageToProtonMessage.

MessageImpl iotHubMessageToProtonMessage(Message message) {
    log.trace("Converting IoT Hub message to proton message for {} sender link with address {} and link correlation id {}. IoT Hub message correlationId {}", getLinkInstanceType(), this.senderLinkAddress, this.linkCorrelationId, message.getCorrelationId());
    MessageImpl outgoingMessage = (MessageImpl) Proton.message();
    Properties properties = new Properties();
    if (message.getMessageId() != null) {
        properties.setMessageId(message.getMessageId());
    }
    if (message.getCorrelationId() != null) {
        properties.setCorrelationId(message.getCorrelationId());
    }
    if (message.getContentType() != null) {
        properties.setContentType(Symbol.valueOf(message.getContentType()));
    }
    if (message.getContentEncoding() != null) {
        properties.setContentEncoding(Symbol.valueOf(message.getContentEncoding()));
    }
    outgoingMessage.setProperties(properties);
    Map<String, Object> userProperties = new HashMap<>();
    if (message.getProperties().length > 0) {
        for (MessageProperty messageProperty : message.getProperties()) {
            if (!MessageProperty.RESERVED_PROPERTY_NAMES.contains(messageProperty.getName())) {
                userProperties.put(messageProperty.getName(), messageProperty.getValue());
            }
        }
    }
    if (message.getConnectionDeviceId() != null) {
        userProperties.put(MessageProperty.CONNECTION_DEVICE_ID, message.getConnectionDeviceId());
    }
    if (message.getConnectionModuleId() != null) {
        userProperties.put(MessageProperty.CONNECTION_MODULE_ID, message.getConnectionModuleId());
    }
    if (message.getCreationTimeUTC() != null) {
        userProperties.put(MessageProperty.IOTHUB_CREATION_TIME_UTC, message.getCreationTimeUTCString());
    }
    ApplicationProperties applicationProperties = new ApplicationProperties(userProperties);
    outgoingMessage.setApplicationProperties(applicationProperties);
    Map<Symbol, Object> messageAnnotationsMap = new HashMap<>();
    if (message.isSecurityMessage()) {
        messageAnnotationsMap.put(Symbol.valueOf(MessageProperty.IOTHUB_SECURITY_INTERFACE_ID), MessageProperty.IOTHUB_SECURITY_INTERFACE_ID_VALUE);
    }
    if (message.getComponentName() != null && !message.getComponentName().isEmpty()) {
        messageAnnotationsMap.put(Symbol.valueOf(MessageProperty.COMPONENT_ID), message.getComponentName());
    }
    MessageAnnotations messageAnnotations = new MessageAnnotations(messageAnnotationsMap);
    outgoingMessage.setMessageAnnotations(messageAnnotations);
    Binary binary = new Binary(message.getBytes());
    Section section = new Data(binary);
    outgoingMessage.setBody(section);
    return outgoingMessage;
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Symbol(org.apache.qpid.proton.amqp.Symbol) Data(org.apache.qpid.proton.amqp.messaging.Data) Properties(org.apache.qpid.proton.amqp.messaging.Properties) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) Section(org.apache.qpid.proton.amqp.messaging.Section) MessageAnnotations(org.apache.qpid.proton.amqp.messaging.MessageAnnotations) MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) Binary(org.apache.qpid.proton.amqp.Binary) MessageImpl(org.apache.qpid.proton.message.impl.MessageImpl)

Example 15 with MessageAnnotations

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

the class AMQPMessageSupportTest method testGetMessageAnnotationWhenMessageHasEmptyAnnotationsMap.

@Test
public void testGetMessageAnnotationWhenMessageHasEmptyAnnotationsMap() {
    Map<Symbol, Object> messageAnnotationsMap = new HashMap<>();
    Message message = Proton.message();
    message.setMessageAnnotations(new MessageAnnotations(messageAnnotationsMap));
    assertNull(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