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
This commit is contained in:
fml2 2023-01-10 08:24:10 +01:00 committed by Sam Brannen
parent c13dfc5144
commit ad4e0d9ad7
1 changed files with 13 additions and 1 deletions

View File

@ -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 {