Add support for repeatable JmsListener

Previously, a method could only declare one Jms endpoint so if several
destinations share the exact same business logic, you'd still need one
separate method declaration per destination.

We now make sure that JmsListener is a repeatable annotation, introducing
JmsListeners for pre Java8 use cases.

Issue: SPR-13147
This commit is contained in:
Stephane Nicoll
2015-06-22 11:39:52 +02:00
parent 9ada55dc6b
commit 4631add6cf
10 changed files with 180 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ package org.springframework.jms.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@@ -70,11 +71,13 @@ import org.springframework.messaging.handler.annotation.MessageMapping;
* @since 4.1
* @see EnableJms
* @see JmsListenerAnnotationBeanPostProcessor
* @see JmsListeners
*/
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@MessageMapping
@Documented
@Repeatable(JmsListeners.class)
public @interface JmsListener {
/**

View File

@@ -199,8 +199,8 @@ public class JmsListenerAnnotationBeanPostProcessor
ReflectionUtils.doWithMethods(targetClass, new ReflectionUtils.MethodCallback() {
@Override
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
JmsListener jmsListener = AnnotationUtils.getAnnotation(method, JmsListener.class);
if (jmsListener != null) {
for (JmsListener jmsListener :
AnnotationUtils.getRepeatableAnnotations(method, JmsListener.class, JmsListeners.class)) {
processJmsListener(jmsListener, method, bean);
annotatedMethods.add(method);
}

View File

@@ -0,0 +1,44 @@
/*
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jms.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Container annotation that aggregates several {@link JmsListener} annotations.
*
* <p>Can be used natively, declaring several nested {@link JmsListener} annotations.
* Can also be used in conjunction with Java 8's support for repeatable annotations,
* where {@link JmsListener} can simply be declared several times on the same method,
* implicitly generating this container annotation.
*
* @author Stephane Nicoll
* @since 4.2
* @see JmsListener
*/
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface JmsListeners {
JmsListener[] value();
}