From ad4e0d9ad75b011aae5a7ca3eb34240148e7802c Mon Sep 17 00:00:00 2001 From: fml2 Date: Tue, 10 Jan 2023 08:24:10 +0100 Subject: [PATCH] Improve generated default name for a @JmsListener subscription Prior to this commit, when using durable subscribers with @JmsListener methods that do not specify a custom subscription name the generated default subscription name was always org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter. Consequently, multiple such @JmsListener methods were assigned the same subscription name which violates the uniqueness requirement. To address this, MessagingMessageListenerAdapter now implements SubscriptionNameProvider and generates the subscription name based on the following rules. - if the InvocableHandlerMethod is present, the subscription name will take the form of handlerMethod.getBeanType().getName() + "#" + handlerMethod.getMethod().getName(). - otherwise, getClass().getName() is used, which is analogous to the previous behavior. Closes gh-29790 --- .../adapter/MessagingMessageListenerAdapter.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/spring-jms/src/main/java/org/springframework/jms/listener/adapter/MessagingMessageListenerAdapter.java b/spring-jms/src/main/java/org/springframework/jms/listener/adapter/MessagingMessageListenerAdapter.java index f82a274fdd..16e3177bba 100644 --- a/spring-jms/src/main/java/org/springframework/jms/listener/adapter/MessagingMessageListenerAdapter.java +++ b/spring-jms/src/main/java/org/springframework/jms/listener/adapter/MessagingMessageListenerAdapter.java @@ -20,6 +20,7 @@ import jakarta.jms.JMSException; import jakarta.jms.Session; import org.springframework.core.MethodParameter; +import org.springframework.jms.listener.SubscriptionNameProvider; import org.springframework.jms.support.JmsHeaderMapper; import org.springframework.jms.support.converter.MessageConversionException; import org.springframework.lang.Nullable; @@ -48,7 +49,8 @@ import org.springframework.util.Assert; * @see JmsHeaderMapper * @see InvocableHandlerMethod */ -public class MessagingMessageListenerAdapter extends AbstractAdaptableMessageListener { +public class MessagingMessageListenerAdapter extends AbstractAdaptableMessageListener + implements SubscriptionNameProvider { @Nullable private InvocableHandlerMethod handlerMethod; @@ -67,6 +69,16 @@ public class MessagingMessageListenerAdapter extends AbstractAdaptableMessageLis return this.handlerMethod; } + @Override + public String getSubscriptionName() { + if (this.handlerMethod != null) { + return this.handlerMethod.getBeanType().getName() + "#" + this.handlerMethod.getMethod().getName(); + } + else { + return this.getClass().getName(); + } + } + @Override public void onMessage(jakarta.jms.Message jmsMessage, @Nullable Session session) throws JMSException {