|
28 | 28 | import com.rabbitmq.stream.MessageBuilder.PropertiesBuilder; |
29 | 29 | import com.rabbitmq.stream.Properties; |
30 | 30 | import com.rabbitmq.stream.codec.WrapperMessageBuilder; |
| 31 | +import org.jspecify.annotations.Nullable; |
31 | 32 |
|
32 | 33 | import org.springframework.amqp.core.Message; |
33 | 34 | import org.springframework.amqp.core.MessageProperties; |
34 | 35 | import org.springframework.amqp.support.converter.MessageConversionException; |
35 | 36 | import org.springframework.amqp.utils.JavaUtils; |
36 | 37 | import org.springframework.rabbit.stream.support.StreamMessageProperties; |
37 | 38 | import org.springframework.util.Assert; |
| 39 | +import org.springframework.util.StringUtils; |
38 | 40 |
|
39 | 41 | /** |
40 | 42 | * Default {@link StreamMessageConverter}. |
41 | 43 | * |
42 | 44 | * @author Gary Russell |
43 | 45 | * @author Ngoc Nhan |
| 46 | + * @author Artem Bilan |
| 47 | + * |
44 | 48 | * @since 2.4 |
45 | 49 | * |
46 | 50 | */ |
@@ -91,30 +95,37 @@ public com.rabbitmq.stream.Message fromMessage(Message message) throws MessageCo |
91 | 95 | Assert.isInstanceOf(StreamMessageProperties.class, props); |
92 | 96 | StreamMessageProperties mProps = (StreamMessageProperties) props; |
93 | 97 | JavaUtils.INSTANCE |
94 | | - .acceptIfNotNull(mProps.getMessageId(), propsBuilder::messageId) // TODO different types |
| 98 | + .acceptIfNotNull(mProps.getMessageId(), propsBuilder::messageId) |
95 | 99 | .acceptIfNotNull(mProps.getUserId(), usr -> propsBuilder.userId(usr.getBytes(this.charset))) |
96 | 100 | .acceptIfNotNull(mProps.getTo(), propsBuilder::to) |
97 | 101 | .acceptIfNotNull(mProps.getSubject(), propsBuilder::subject) |
98 | 102 | .acceptIfNotNull(mProps.getReplyTo(), propsBuilder::replyTo) |
99 | | - .acceptIfNotNull(mProps.getCorrelationId(), propsBuilder::correlationId) // TODO different types |
| 103 | + .acceptIfNotNull(mProps.getCorrelationId(), propsBuilder::correlationId) |
100 | 104 | .acceptIfNotNull(mProps.getContentType(), propsBuilder::contentType) |
101 | 105 | .acceptIfNotNull(mProps.getContentEncoding(), propsBuilder::contentEncoding) |
102 | | - .acceptIfNotNull(mProps.getExpiration(), exp -> propsBuilder.absoluteExpiryTime(Long.parseLong(exp))) |
103 | 106 | .acceptIfNotNull(mProps.getCreationTime(), propsBuilder::creationTime) |
104 | 107 | .acceptIfNotNull(mProps.getGroupId(), propsBuilder::groupId) |
105 | 108 | .acceptIfNotNull(mProps.getGroupSequence(), propsBuilder::groupSequence) |
106 | 109 | .acceptIfNotNull(mProps.getReplyToGroupId(), propsBuilder::replyToGroupId); |
107 | 110 | ApplicationPropertiesBuilder appPropsBuilder = builder.applicationProperties(); |
108 | | - if (!mProps.getHeaders().isEmpty()) { |
109 | | - mProps.getHeaders().forEach((key, val) -> { |
110 | | - mapProp(key, val, appPropsBuilder); |
111 | | - }); |
| 111 | + |
| 112 | + long creationTime = mProps.getCreationTime(); |
| 113 | + if (creationTime <= 0) { |
| 114 | + creationTime = System.currentTimeMillis(); |
| 115 | + } |
| 116 | + propsBuilder.creationTime(creationTime); |
| 117 | + |
| 118 | + String expiration = mProps.getExpiration(); |
| 119 | + if (StringUtils.hasText(expiration)) { |
| 120 | + propsBuilder.absoluteExpiryTime(creationTime + Long.parseLong(expiration)); |
112 | 121 | } |
| 122 | + |
| 123 | + mProps.getHeaders().forEach((key, val) -> mapProp(key, val, appPropsBuilder)); |
113 | 124 | builder.addData(message.getBody()); |
114 | 125 | return builder.build(); |
115 | 126 | } |
116 | 127 |
|
117 | | - private void mapProp(String key, Object val, ApplicationPropertiesBuilder builder) { // NOSONAR - complexity |
| 128 | + private void mapProp(String key, @Nullable Object val, ApplicationPropertiesBuilder builder) { |
118 | 129 | if (val instanceof String string) { |
119 | 130 | builder.entry(key, string); |
120 | 131 | } |
@@ -154,19 +165,28 @@ private void toMessageProperties(com.rabbitmq.stream.Message streamMessage, |
154 | 165 | if (properties != null) { |
155 | 166 | JavaUtils.INSTANCE |
156 | 167 | .acceptIfNotNull(properties.getMessageIdAsString(), mProps::setMessageId) |
157 | | - .acceptIfNotNull(properties.getUserId(), usr -> mProps.setUserId(new String(usr, this.charset))) |
| 168 | + .acceptIfNotNull(properties.getUserId(), |
| 169 | + usr -> mProps.setUserId(new String(usr, this.charset))) |
158 | 170 | .acceptIfNotNull(properties.getTo(), mProps::setTo) |
159 | 171 | .acceptIfNotNull(properties.getSubject(), mProps::setSubject) |
160 | 172 | .acceptIfNotNull(properties.getReplyTo(), mProps::setReplyTo) |
161 | 173 | .acceptIfNotNull(properties.getCorrelationIdAsString(), mProps::setCorrelationId) |
162 | 174 | .acceptIfNotNull(properties.getContentType(), mProps::setContentType) |
163 | 175 | .acceptIfNotNull(properties.getContentEncoding(), mProps::setContentEncoding) |
164 | | - .acceptIfNotNull(properties.getAbsoluteExpiryTime(), |
165 | | - exp -> mProps.setExpiration(Long.toString(exp))) |
166 | | - .acceptIfNotNull(properties.getCreationTime(), mProps::setCreationTime) |
167 | 176 | .acceptIfNotNull(properties.getGroupId(), mProps::setGroupId) |
168 | 177 | .acceptIfNotNull(properties.getGroupSequence(), mProps::setGroupSequence) |
169 | 178 | .acceptIfNotNull(properties.getReplyToGroupId(), mProps::setReplyToGroupId); |
| 179 | + |
| 180 | + long creationTime = properties.getCreationTime(); |
| 181 | + if (creationTime <= 0) { |
| 182 | + creationTime = System.currentTimeMillis(); |
| 183 | + } |
| 184 | + mProps.setCreationTime(creationTime); |
| 185 | + |
| 186 | + long absoluteExpiryTime = properties.getAbsoluteExpiryTime(); |
| 187 | + if (absoluteExpiryTime > creationTime) { |
| 188 | + mProps.setExpiration(Long.toString(absoluteExpiryTime - creationTime)); |
| 189 | + } |
170 | 190 | } |
171 | 191 | Map<String, Object> applicationProperties = streamMessage.getApplicationProperties(); |
172 | 192 | if (applicationProperties != null) { |
|
0 commit comments