Browse Source

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
pull/29918/head
fml2 2 years ago committed by Sam Brannen
parent
commit
ad4e0d9ad7
  1. 14
      spring-jms/src/main/java/org/springframework/jms/listener/adapter/MessagingMessageListenerAdapter.java

14
spring-jms/src/main/java/org/springframework/jms/listener/adapter/MessagingMessageListenerAdapter.java

@ -20,6 +20,7 @@ import jakarta.jms.JMSException; @@ -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; @@ -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 @@ -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 {

Loading…
Cancel
Save