Search in sources :

Example 1 with Properties

use of com.rabbitmq.stream.Properties in project rabbitmq-stream-java-client by rabbitmq.

the class QpidProtonCodec method encode.

@Override
public EncodedMessage encode(Message message) {
    org.apache.qpid.proton.message.Message qpidMessage;
    if (message instanceof QpidProtonAmqpMessageWrapper) {
        qpidMessage = ((QpidProtonAmqpMessageWrapper) message).message;
    } else {
        qpidMessage = org.apache.qpid.proton.message.Message.Factory.create();
        if (message.getProperties() != null) {
            Properties headers = message.getProperties();
            org.apache.qpid.proton.amqp.messaging.Properties properties = new org.apache.qpid.proton.amqp.messaging.Properties();
            boolean propertiesSet = false;
            if (headers.getMessageId() != null) {
                if (headers.getMessageId() instanceof String) {
                    properties.setMessageId(headers.getMessageIdAsString());
                } else if (headers.getMessageId().getClass().isPrimitive() || headers.getMessageId() instanceof Long) {
                    properties.setMessageId(new UnsignedLong(headers.getMessageIdAsLong()));
                } else if (headers.getMessageId().getClass().isArray()) {
                    properties.setMessageId(new Binary(headers.getMessageIdAsBinary()));
                } else if (headers.getMessageId() instanceof UUID) {
                    properties.setMessageId(headers.getMessageIdAsUuid());
                } else {
                    throw new IllegalStateException("Type not supported for message ID:" + properties.getMessageId().getClass());
                }
                propertiesSet = true;
            }
            if (headers.getUserId() != null) {
                properties.setUserId(new Binary(headers.getUserId()));
                propertiesSet = true;
            }
            if (headers.getTo() != null) {
                properties.setTo(headers.getTo());
                propertiesSet = true;
            }
            if (headers.getSubject() != null) {
                properties.setSubject(headers.getSubject());
                propertiesSet = true;
            }
            if (headers.getReplyTo() != null) {
                properties.setReplyTo(headers.getReplyTo());
                propertiesSet = true;
            }
            if (headers.getCorrelationId() != null) {
                if (headers.getCorrelationId() instanceof String) {
                    properties.setCorrelationId(headers.getCorrelationIdAsString());
                } else if (headers.getCorrelationId().getClass().isPrimitive() || headers.getCorrelationId() instanceof Long) {
                    properties.setCorrelationId(new UnsignedLong(headers.getCorrelationIdAsLong()));
                } else if (headers.getCorrelationId().getClass().isArray()) {
                    properties.setCorrelationId(new Binary(headers.getCorrelationIdAsBinary()));
                } else if (headers.getCorrelationId() instanceof UUID) {
                    properties.setCorrelationId(headers.getCorrelationIdAsUuid());
                } else {
                    throw new IllegalStateException("Type not supported for correlation ID:" + properties.getCorrelationId().getClass());
                }
                propertiesSet = true;
            }
            if (headers.getContentType() != null) {
                properties.setContentType(Symbol.getSymbol(headers.getContentType()));
                propertiesSet = true;
            }
            if (headers.getContentEncoding() != null) {
                properties.setContentEncoding(Symbol.getSymbol(headers.getContentEncoding()));
                propertiesSet = true;
            }
            if (headers.getAbsoluteExpiryTime() > 0) {
                properties.setAbsoluteExpiryTime(new Date(headers.getAbsoluteExpiryTime()));
                propertiesSet = true;
            }
            if (headers.getCreationTime() > 0) {
                properties.setCreationTime(new Date(headers.getCreationTime()));
                propertiesSet = true;
            }
            if (headers.getGroupId() != null) {
                properties.setGroupId(headers.getGroupId());
                propertiesSet = true;
            }
            if (headers.getGroupSequence() >= 0) {
                properties.setGroupSequence(UnsignedInteger.valueOf(headers.getGroupSequence()));
                propertiesSet = true;
            }
            if (headers.getReplyToGroupId() != null) {
                properties.setReplyToGroupId(headers.getReplyToGroupId());
                propertiesSet = true;
            }
            if (propertiesSet) {
                qpidMessage.setProperties(properties);
            }
        }
        if (message.getApplicationProperties() != null && !message.getApplicationProperties().isEmpty()) {
            Map<String, Object> applicationProperties = new LinkedHashMap<>(message.getApplicationProperties().size());
            for (Map.Entry<String, Object> entry : message.getApplicationProperties().entrySet()) {
                applicationProperties.put(entry.getKey(), convertToQpidType(entry.getValue()));
            }
            qpidMessage.setApplicationProperties(new ApplicationProperties(applicationProperties));
        }
        if (message.getMessageAnnotations() != null && !message.getMessageAnnotations().isEmpty()) {
            Map<Symbol, Object> messageAnnotations = new LinkedHashMap<>(message.getMessageAnnotations().size());
            for (Map.Entry<String, Object> entry : message.getMessageAnnotations().entrySet()) {
                messageAnnotations.put(Symbol.getSymbol(entry.getKey()), convertToQpidType(entry.getValue()));
            }
            qpidMessage.setMessageAnnotations(new MessageAnnotations(messageAnnotations));
        }
        if (message.getBodyAsBinary() != null) {
            qpidMessage.setBody(new Data(new Binary(message.getBodyAsBinary())));
        }
    }
    int bufferSize;
    if (qpidMessage.getBody() instanceof Data) {
        bufferSize = ((Data) qpidMessage.getBody()).getValue().getLength() * 2;
    } else {
        bufferSize = 8192;
    }
    ByteArrayWritableBuffer writableBuffer = new ByteArrayWritableBuffer(bufferSize);
    qpidMessage.encode(writableBuffer);
    return new EncodedMessage(writableBuffer.getArrayLength(), writableBuffer.getArray());
}
Also used : Properties(com.rabbitmq.stream.Properties) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) LinkedHashMap(java.util.LinkedHashMap) MessageAnnotations(org.apache.qpid.proton.amqp.messaging.MessageAnnotations) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) UUID(java.util.UUID) org.apache.qpid.proton.amqp(org.apache.qpid.proton.amqp) Data(org.apache.qpid.proton.amqp.messaging.Data) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with Properties

use of com.rabbitmq.stream.Properties in project spring-amqp by spring-projects.

the class DefaultStreamMessageConverter method toMessageProperties.

private void toMessageProperties(com.rabbitmq.stream.Message streamMessage, StreamMessageProperties mProps) {
    Properties properties = streamMessage.getProperties();
    if (properties != null) {
        JavaUtils.INSTANCE.acceptIfNotNull(properties.getMessageIdAsString(), mProps::setMessageId).acceptIfNotNull(properties.getUserId(), usr -> mProps.setUserId(new String(usr, this.charset))).acceptIfNotNull(properties.getTo(), mProps::setTo).acceptIfNotNull(properties.getSubject(), mProps::setSubject).acceptIfNotNull(properties.getReplyTo(), mProps::setReplyTo).acceptIfNotNull(properties.getCorrelationIdAsString(), mProps::setCorrelationId).acceptIfNotNull(properties.getContentType(), mProps::setContentType).acceptIfNotNull(properties.getContentEncoding(), mProps::setContentEncoding).acceptIfNotNull(properties.getAbsoluteExpiryTime(), exp -> mProps.setExpiration(Long.toString(exp))).acceptIfNotNull(properties.getCreationTime(), mProps::setCreationTime).acceptIfNotNull(properties.getGroupId(), mProps::setGroupId).acceptIfNotNull(properties.getGroupSequence(), mProps::setGroupSequence).acceptIfNotNull(properties.getReplyToGroupId(), mProps::setReplyToGroupId);
    }
    Map<String, Object> applicationProperties = streamMessage.getApplicationProperties();
    if (applicationProperties != null) {
        mProps.getHeaders().putAll(applicationProperties);
    }
}
Also used : StreamMessageProperties(org.springframework.rabbit.stream.support.StreamMessageProperties) Properties(com.rabbitmq.stream.Properties) ApplicationPropertiesBuilder(com.rabbitmq.stream.MessageBuilder.ApplicationPropertiesBuilder) PropertiesBuilder(com.rabbitmq.stream.MessageBuilder.PropertiesBuilder) Codec(com.rabbitmq.stream.Codec) MessageProperties(org.springframework.amqp.core.MessageProperties) UUID(java.util.UUID) Supplier(java.util.function.Supplier) StandardCharsets(java.nio.charset.StandardCharsets) WrapperMessageBuilder(com.rabbitmq.stream.codec.WrapperMessageBuilder) Charset(java.nio.charset.Charset) Map(java.util.Map) Message(org.springframework.amqp.core.Message) JavaUtils(org.springframework.amqp.utils.JavaUtils) Nullable(org.springframework.lang.Nullable) MessageBuilder(com.rabbitmq.stream.MessageBuilder) MessageConversionException(org.springframework.amqp.support.converter.MessageConversionException) Assert(org.springframework.util.Assert) StreamMessageProperties(org.springframework.rabbit.stream.support.StreamMessageProperties) Properties(com.rabbitmq.stream.Properties) MessageProperties(org.springframework.amqp.core.MessageProperties)

Example 3 with Properties

use of com.rabbitmq.stream.Properties in project spring-amqp by spring-projects.

the class DefaultStreamMessageConverterTests method toAndFrom.

@Test
void toAndFrom() {
    StreamMessageProperties smp = new StreamMessageProperties(mock(Context.class));
    smp.setMessageId("test");
    smp.setUserId("user");
    smp.setTo("to");
    smp.setSubject("subject");
    smp.setReplyTo("replyTo");
    smp.setCorrelationId("correlation");
    smp.setContentType("application/json");
    smp.setContentEncoding("UTF-8");
    smp.setExpiration("42");
    smp.setCreationTime(43L);
    smp.setGroupId("groupId");
    smp.setGroupSequence(44L);
    smp.setReplyToGroupId("replyGroupId");
    smp.setHeader("foo", "bar");
    DefaultStreamMessageConverter converter = new DefaultStreamMessageConverter();
    Message msg = new Message("foo".getBytes(), smp);
    com.rabbitmq.stream.Message streamMessage = converter.fromMessage(msg);
    Properties props = streamMessage.getProperties();
    assertThat(props.getMessageIdAsString()).isEqualTo("test");
    assertThat(props.getUserId()).isEqualTo("user".getBytes());
    assertThat(props.getTo()).isEqualTo("to");
    assertThat(props.getSubject()).isEqualTo("subject");
    assertThat(props.getReplyTo()).isEqualTo("replyTo");
    assertThat(props.getCorrelationIdAsString()).isEqualTo("correlation");
    assertThat(props.getContentType()).isEqualTo("application/json");
    assertThat(props.getContentEncoding()).isEqualTo("UTF-8");
    assertThat(props.getAbsoluteExpiryTime()).isEqualTo(42L);
    assertThat(props.getCreationTime()).isEqualTo(43L);
    assertThat(props.getGroupId()).isEqualTo("groupId");
    assertThat(props.getGroupSequence()).isEqualTo(44L);
    assertThat(props.getReplyToGroupId()).isEqualTo("replyGroupId");
    assertThat(streamMessage.getApplicationProperties().get("foo")).isEqualTo("bar");
    StreamMessageProperties smp2 = new StreamMessageProperties(mock(Context.class));
    msg = converter.toMessage(streamMessage, smp2);
    assertThat(msg.getMessageProperties()).isSameAs(smp2);
    assertThat(smp2).isEqualTo(smp);
}
Also used : Context(com.rabbitmq.stream.MessageHandler.Context) Message(org.springframework.amqp.core.Message) StreamMessageProperties(org.springframework.rabbit.stream.support.StreamMessageProperties) StreamMessageProperties(org.springframework.rabbit.stream.support.StreamMessageProperties) Properties(com.rabbitmq.stream.Properties) Test(org.junit.jupiter.api.Test)

Aggregations

Properties (com.rabbitmq.stream.Properties)3 Map (java.util.Map)2 UUID (java.util.UUID)2 Message (org.springframework.amqp.core.Message)2 StreamMessageProperties (org.springframework.rabbit.stream.support.StreamMessageProperties)2 Codec (com.rabbitmq.stream.Codec)1 MessageBuilder (com.rabbitmq.stream.MessageBuilder)1 ApplicationPropertiesBuilder (com.rabbitmq.stream.MessageBuilder.ApplicationPropertiesBuilder)1 PropertiesBuilder (com.rabbitmq.stream.MessageBuilder.PropertiesBuilder)1 Context (com.rabbitmq.stream.MessageHandler.Context)1 WrapperMessageBuilder (com.rabbitmq.stream.codec.WrapperMessageBuilder)1 Charset (java.nio.charset.Charset)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Date (java.util.Date)1 LinkedHashMap (java.util.LinkedHashMap)1 Supplier (java.util.function.Supplier)1 org.apache.qpid.proton.amqp (org.apache.qpid.proton.amqp)1 ApplicationProperties (org.apache.qpid.proton.amqp.messaging.ApplicationProperties)1 Data (org.apache.qpid.proton.amqp.messaging.Data)1 MessageAnnotations (org.apache.qpid.proton.amqp.messaging.MessageAnnotations)1