Provide best-effort toString for Lazy resolved message

Previously, MessagingMessageListenerAdapter or any adapter relying on
the default MessagingMessageConverter would log an incoming message
with a toString of the Message that does not provide any extra
information. This is due to the default implementation providing a
lazy resolution message that only attempts to extract the payload
when necessary.

This commit implements a toString method that uses the raw JMS message
if the payload is not available. If it is, the payload is used instead.

Closes gh-21265

(cherry picked from commit a37abd5e54)
This commit is contained in:
Stéphane Nicoll
2023-09-26 12:26:39 +02:00
committed by Juergen Hoeller
parent 7a8ec2a761
commit 12159b98a2
2 changed files with 67 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -40,6 +40,7 @@ import org.springframework.jms.support.QosSettings;
import org.springframework.jms.support.converter.MappingJackson2MessageConverter;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jms.support.converter.MessageType;
import org.springframework.jms.support.converter.MessagingMessageConverter;
import org.springframework.messaging.Message;
import org.springframework.messaging.converter.MessageConversionException;
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
@@ -340,6 +341,45 @@ public class MessagingMessageListenerAdapterTests {
return responseMessage;
}
@Test
void lazyResolutionMessageToStringProvidesBestEffortWithUnresolvedPayload() throws JMSException {
MessagingMessageListenerAdapter adapter = getSimpleInstance("echo", Message.class);
MessagingMessageConverter messagingMessageConverter = adapter.getMessagingMessageConverter();
assertThat(messagingMessageConverter).isNotNull();
TextMessage message = new StubTextMessage();
assertThat(messagingMessageConverter.fromMessage(message)).isInstanceOfSatisfying(Message.class, msg ->
assertThat(msg.toString()).contains("rawMessage=").contains(message.toString())
.doesNotContain("payload=").doesNotContain("headers="));
}
@Test
void lazyResolutionMessageToStringWithResolvedPayload() throws JMSException {
MessagingMessageListenerAdapter adapter = getSimpleInstance("echo", Message.class);
MessagingMessageConverter messagingMessageConverter = adapter.getMessagingMessageConverter();
assertThat(messagingMessageConverter).isNotNull();
TextMessage message = new StubTextMessage("Hello");
assertThat(messagingMessageConverter.fromMessage(message)).isInstanceOfSatisfying(Message.class, msg -> {
msg.getPayload(); // force resolution
assertThat(msg.toString()).contains("payload=Hello")
.doesNotContain("rawMessage=").doesNotContain("headers=");
});
}
@Test
void lazyResolutionMessageToStringWithResolvedPayloadAndHeaders() throws JMSException {
MessagingMessageListenerAdapter adapter = getSimpleInstance("echo", Message.class);
MessagingMessageConverter messagingMessageConverter = adapter.getMessagingMessageConverter();
assertThat(messagingMessageConverter).isNotNull();
TextMessage message = new StubTextMessage("Hello");
message.setJMSPriority(7);
assertThat(messagingMessageConverter.fromMessage(message)).isInstanceOfSatisfying(Message.class, msg -> {
msg.getPayload();
msg.getHeaders(); // force resolution
assertThat(msg.toString()).contains("payload=Hello").contains("headers=").contains("jms_priority=7")
.doesNotContain("rawMessage=");
});
}
protected MessagingMessageListenerAdapter getSimpleInstance(String methodName, Class<?>... parameterTypes) {
Method m = ReflectionUtils.findMethod(SampleBean.class, methodName, parameterTypes);