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-29902
This commit is contained in:
fml2
2023-01-10 08:24:10 +01:00
committed by Sam Brannen
parent 40d2466334
commit 07fd7606e7
@@ -20,6 +20,7 @@ import javax.jms.JMSException;
import javax.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(javax.jms.Message jmsMessage, @Nullable Session session) throws JMSException {