diff --git a/spring-amqp/src/main/java/org/springframework/amqp/utils/JavaUtils.java b/spring-amqp/src/main/java/org/springframework/amqp/utils/JavaUtils.java index ee151353..1a1a31af 100644 --- a/spring-amqp/src/main/java/org/springframework/amqp/utils/JavaUtils.java +++ b/spring-amqp/src/main/java/org/springframework/amqp/utils/JavaUtils.java @@ -136,4 +136,24 @@ public final class JavaUtils { return this; } + + /** + * Invoke {@link Consumer#accept(Object)} with the value or alternative if one of them is not null. + * @param value the value. + * @param alternative the other value if the {@code value} argument is null. + * @param consumer the consumer. + * @param the value type. + * @return this. + * @since 4.0 + */ + public JavaUtils acceptOrElseIfNotNull(@Nullable T value, @Nullable T alternative, Consumer consumer) { + if (value != null) { + consumer.accept(value); + } + else if (alternative != null) { + consumer.accept(alternative); + } + return this; + } + } diff --git a/spring-rabbitmq-client/src/main/java/org/springframework/amqp/rabbitmq/client/RabbitAmqpUtils.java b/spring-rabbitmq-client/src/main/java/org/springframework/amqp/rabbitmq/client/RabbitAmqpUtils.java index c10ebebd..f19a8659 100644 --- a/spring-rabbitmq-client/src/main/java/org/springframework/amqp/rabbitmq/client/RabbitAmqpUtils.java +++ b/spring-rabbitmq-client/src/main/java/org/springframework/amqp/rabbitmq/client/RabbitAmqpUtils.java @@ -19,7 +19,6 @@ package org.springframework.amqp.rabbitmq.client; import java.nio.charset.StandardCharsets; import java.util.Date; import java.util.Map; -import java.util.Objects; import java.util.UUID; import com.rabbitmq.client.amqp.Consumer; @@ -88,11 +87,7 @@ public final class RabbitAmqpUtils { .contentEncoding(messageProperties.getContentEncoding()) .contentType(messageProperties.getContentType()) .messageId(messageProperties.getMessageId()) - .correlationId( - Objects.requireNonNullElse( - messageProperties.getCorrelationId(), messageProperties.getMessageId())) - .priority(messageProperties.getPriority().byteValue()) - .to(messageProperties.getReplyTo()); + .priority(messageProperties.getPriority().byteValue()); Map headers = messageProperties.getHeaders(); if (!headers.isEmpty()) { @@ -100,8 +95,11 @@ public final class RabbitAmqpUtils { } JavaUtils.INSTANCE + .acceptOrElseIfNotNull(messageProperties.getCorrelationId(), + messageProperties.getMessageId(), amqpMessage::correlationId) .acceptIfNotNull(messageProperties.getUserId(), (userId) -> amqpMessage.userId(userId.getBytes(StandardCharsets.UTF_8))) + .acceptIfNotNull(messageProperties.getReplyTo(), amqpMessage::to) .acceptIfNotNull(messageProperties.getTimestamp(), (timestamp) -> amqpMessage.creationTime(timestamp.getTime())) .acceptIfNotNull(messageProperties.getExpiration(),