Search in sources :

Example 1 with ServerDestination

use of org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerDestination 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)

Aggregations

Map (java.util.Map)1 Set (java.util.Set)1 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)1 ServerDestination (org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerDestination)1 TypedProperties (org.apache.activemq.artemis.utils.collections.TypedProperties)1 Binary (org.apache.qpid.proton.amqp.Binary)1 ApplicationProperties (org.apache.qpid.proton.amqp.messaging.ApplicationProperties)1 Footer (org.apache.qpid.proton.amqp.messaging.Footer)1 Header (org.apache.qpid.proton.amqp.messaging.Header)1 MessageAnnotations (org.apache.qpid.proton.amqp.messaging.MessageAnnotations)1 Properties (org.apache.qpid.proton.amqp.messaging.Properties)1