Skip to content

Commit 6b2ad0d

Browse files
committed
Fix RabbitAmqpUtils.toAmqpMessage() util for to prop
The `messageProperties.getReplyTo()` might be null, so set it into `amqpMessage::to` only if it is not null * Add convenient `JavaUtils.acceptOrElseIfNotNull()` for two alternative values
1 parent b6b61c4 commit 6b2ad0d

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

spring-amqp/src/main/java/org/springframework/amqp/utils/JavaUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,24 @@ public <T> JavaUtils acceptIfHasText(T t1, @Nullable String value, BiConsumer<T,
136136
return this;
137137
}
138138

139+
140+
/**
141+
* Invoke {@link Consumer#accept(Object)} with the value or alternative if one of them is not null.
142+
* @param value the value.
143+
* @param alternative the other value if the {@code value} argument is null.
144+
* @param consumer the consumer.
145+
* @param <T> the value type.
146+
* @return this.
147+
* @since 4.0
148+
*/
149+
public <T> JavaUtils acceptOrElseIfNotNull(@Nullable T value, @Nullable T alternative, Consumer<T> consumer) {
150+
if (value != null) {
151+
consumer.accept(value);
152+
}
153+
else if (alternative != null) {
154+
consumer.accept(alternative);
155+
}
156+
return this;
157+
}
158+
139159
}

spring-rabbitmq-client/src/main/java/org/springframework/amqp/rabbitmq/client/RabbitAmqpUtils.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.nio.charset.StandardCharsets;
2020
import java.util.Date;
2121
import java.util.Map;
22-
import java.util.Objects;
2322
import java.util.UUID;
2423

2524
import com.rabbitmq.client.amqp.Consumer;
@@ -88,20 +87,19 @@ public static void toAmqpMessage(Message message, com.rabbitmq.client.amqp.Messa
8887
.contentEncoding(messageProperties.getContentEncoding())
8988
.contentType(messageProperties.getContentType())
9089
.messageId(messageProperties.getMessageId())
91-
.correlationId(
92-
Objects.requireNonNullElse(
93-
messageProperties.getCorrelationId(), messageProperties.getMessageId()))
94-
.priority(messageProperties.getPriority().byteValue())
95-
.to(messageProperties.getReplyTo());
90+
.priority(messageProperties.getPriority().byteValue());
9691

9792
Map<String, @Nullable Object> headers = messageProperties.getHeaders();
9893
if (!headers.isEmpty()) {
9994
headers.forEach((key, val) -> mapProp(key, val, amqpMessage));
10095
}
10196

10297
JavaUtils.INSTANCE
98+
.acceptOrElseIfNotNull(messageProperties.getCorrelationId(),
99+
messageProperties.getMessageId(), amqpMessage::correlationId)
103100
.acceptIfNotNull(messageProperties.getUserId(),
104101
(userId) -> amqpMessage.userId(userId.getBytes(StandardCharsets.UTF_8)))
102+
.acceptIfNotNull(messageProperties.getReplyTo(), amqpMessage::to)
105103
.acceptIfNotNull(messageProperties.getTimestamp(),
106104
(timestamp) -> amqpMessage.creationTime(timestamp.getTime()))
107105
.acceptIfNotNull(messageProperties.getExpiration(),

0 commit comments

Comments
 (0)