Use default destination name with JmsClient.sendAndReceive() and headers

Prior to this commit, if the JmsClient was configured with a default
destination name (instead of a default Destination), invoking the
sendAndReceive() variant which accepts a map of headers resulted in an
exception stating that a default destination is required.

To address that, this commit overrides the
convertSendAndReceive(Object, Map<String, Object>, Class<T>, MessagePostProcessor)
method in JmsMessagingTemplate to add support for both a default
Destination and a default destination name.

Closes gh-36118
This commit is contained in:
Sam Brannen
2026-01-09 15:04:47 +01:00
parent 3e5d8d184a
commit 00606d2171
2 changed files with 48 additions and 4 deletions
@@ -422,6 +422,18 @@ public class JmsMessagingTemplate extends AbstractMessagingTemplate<Destination>
return (replyMessage != null ? (T) getMessageConverter().fromMessage(replyMessage, targetClass) : null);
}
@Override
public <T> @Nullable T convertSendAndReceive(Object request, @Nullable Map<String, Object> headers,
Class<T> targetClass, @Nullable MessagePostProcessor postProcessor) throws MessagingException {
Destination defaultDestination = getDefaultDestination();
if (defaultDestination != null) {
return convertSendAndReceive(defaultDestination, request, headers, targetClass, postProcessor);
}
else {
return convertSendAndReceive(getRequiredDefaultDestinationName(), request, headers, targetClass, postProcessor);
}
}
@Override
protected void doSend(Destination destination, Message<?> message) {
@@ -65,6 +65,7 @@ import static org.mockito.Mockito.verify;
*
* @author Stephane Nicoll
* @author Juergen Hoeller
* @author Sam Brannen
*/
@ExtendWith(MockitoExtension.class)
class JmsMessagingTemplateTests {
@@ -565,7 +566,7 @@ class JmsMessagingTemplateTests {
}
@Test
void convertSendAndReceivePayload() {
void convertSendAndReceivePayloadWithDestination() {
Destination destination = new Destination() {};
jakarta.jms.Message replyJmsMessage = createJmsTextMessage("My reply");
given(this.jmsTemplate.sendAndReceive(eq(destination), any())).willReturn(replyJmsMessage);
@@ -576,7 +577,7 @@ class JmsMessagingTemplateTests {
}
@Test
void convertSendAndReceivePayloadName() {
void convertSendAndReceivePayloadWithDestinationName() {
jakarta.jms.Message replyJmsMessage = createJmsTextMessage("My reply");
given(this.jmsTemplate.sendAndReceive(eq("myQueue"), any())).willReturn(replyJmsMessage);
@@ -586,7 +587,7 @@ class JmsMessagingTemplateTests {
}
@Test
void convertSendAndReceiveDefaultDestination() {
void convertSendAndReceiveWithDefaultDestination() {
Destination destination = new Destination() {};
this.messagingTemplate.setDefaultDestination(destination);
jakarta.jms.Message replyJmsMessage = createJmsTextMessage("My reply");
@@ -597,8 +598,24 @@ class JmsMessagingTemplateTests {
assertThat(reply).isEqualTo("My reply");
}
@Test // gh-36118
void convertSendAndReceiveWithDefaultDestinationAndHeaders() {
Map<String, Object> headers = Map.of("foo", "bar");
Object payload = "Hello";
Destination destination = new Destination() {};
this.messagingTemplate.setDefaultDestination(destination);
jakarta.jms.Message replyJmsMessage = createJmsTextMessage("My reply");
given(this.jmsTemplate.sendAndReceive(eq(destination), any())).willReturn(replyJmsMessage);
String reply = this.messagingTemplate.convertSendAndReceive(payload, headers, String.class);
verify(this.jmsTemplate, times(1)).sendAndReceive(eq(destination), this.messageCreator.capture());
assertTextMessage(this.messageCreator.getValue());
assertThat(reply).isEqualTo("My reply");
}
@Test
void convertSendAndReceiveDefaultDestinationName() {
void convertSendAndReceiveWithDefaultDestinationName() {
this.messagingTemplate.setDefaultDestinationName("myQueue");
jakarta.jms.Message replyJmsMessage = createJmsTextMessage("My reply");
given(this.jmsTemplate.sendAndReceive(eq("myQueue"), any())).willReturn(replyJmsMessage);
@@ -608,6 +625,21 @@ class JmsMessagingTemplateTests {
assertThat(reply).isEqualTo("My reply");
}
@Test // gh-36118
void convertSendAndReceiveWithDefaultDestinationNameAndHeaders() {
Map<String, Object> headers = Map.of("foo", "bar");
Object payload = "Hello";
this.messagingTemplate.setDefaultDestinationName("myQueue");
jakarta.jms.Message replyJmsMessage = createJmsTextMessage("My reply");
given(this.jmsTemplate.sendAndReceive(eq("myQueue"), any())).willReturn(replyJmsMessage);
String reply = this.messagingTemplate.convertSendAndReceive(payload, headers, String.class);
verify(this.jmsTemplate, times(1)).sendAndReceive(eq("myQueue"), this.messageCreator.capture());
assertTextMessage(this.messageCreator.getValue());
assertThat(reply).isEqualTo("My reply");
}
@Test
void convertSendAndReceiveNoDefaultSet() {
assertThatIllegalStateException().isThrownBy(() ->