Skip to content

Commit

Permalink
GH-1136: Support Meta Annotations with AliasFor
Browse files Browse the repository at this point in the history
Resolves #1136

Note that this is a minimal implementation that avoids major refactoring
of the BPP.

Repeatable user meta-annotatiions are not supported.

In a future release we should perform that refactoring as well as supporting
`@Repeatable` user annotations.

Also restore the `stop()` with finally to fix `EnableRabbitTests.testProperShutdownOnException()`
which took 30 seconds to run after the finally was removed by
c1e3179

* Increase search scope
  • Loading branch information
garyrussell authored and artembilan committed Jan 2, 2020
1 parent 1b173a4 commit 3793fc1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@
import org.springframework.context.expression.StandardBeanExpressionResolver;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.DefaultConversionService;
Expand Down Expand Up @@ -421,9 +424,30 @@ private Method checkProxy(Method methodArg, Object bean) {
return method;
}

protected void processListener(MethodRabbitListenerEndpoint endpoint, RabbitListener rabbitListener, Object bean,
protected void processListener(MethodRabbitListenerEndpoint endpoint, RabbitListener rabbitListenerArg, Object bean,
Object target, String beanName) {

RabbitListener rabbitListener = rabbitListenerArg;
MergedAnnotation<RabbitListener> mergedAnnotation = MergedAnnotation.missing();
/*
* Synthesize the actual annotation to handle meta-annotations and aliasing. Note
* that only single @RabbitListener annotations can be meta-annotated.
*/
if (endpoint instanceof MultiMethodRabbitListenerEndpoint) {
if (AnnotationUtils.findAnnotation((Class<?>) target, RabbitListeners.class) == null) {
mergedAnnotation = MergedAnnotations.from((Class<?>) target, SearchStrategy.TYPE_HIERARCHY)
.get(RabbitListener.class);
}
}
else {
if (AnnotationUtils.findAnnotation(endpoint.getMethod(), RabbitListeners.class) == null) {
mergedAnnotation = MergedAnnotations.from(endpoint.getMethod(), SearchStrategy.TYPE_HIERARCHY)
.get(RabbitListener.class);
}
}
if (!MergedAnnotation.missing().equals(mergedAnnotation)) {
rabbitListener = mergedAnnotation.synthesize();
}
endpoint.setBean(bean);
endpoint.setMessageHandlerMethodFactory(this.messageHandlerMethodFactory);
endpoint.setId(getEndpointId(rabbitListener));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,16 @@ public void stop() {
}
}

@Override
public void stop(Runnable callback) {
try {
stop();
}
finally {
callback.run();
}
}

/**
* This method is invoked when the container is stopping.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.annotation.AliasFor;
import org.springframework.stereotype.Component;

/**
Expand Down Expand Up @@ -124,7 +125,10 @@ public void metaAnnotationIsDiscovered() {
RabbitListenerContainerTestFactory factory = context.getBean(RabbitListenerContainerTestFactory.class);
assertThat(factory.getListenerContainers().size()).as("one container should have been registered").isEqualTo(1);
RabbitListenerEndpoint endpoint = factory.getListenerContainers().get(0).getEndpoint();
assertThat(((AbstractRabbitListenerEndpoint) endpoint).getQueueNames().iterator().next()).isEqualTo("metaTestQueue");
assertThat(((AbstractRabbitListenerEndpoint) endpoint).getQueueNames()
.iterator()
.next())
.isEqualTo("metaTestQueue");

context.close();
}
Expand Down Expand Up @@ -330,16 +334,20 @@ public void handleIt(String body) {
@Component
static class MetaAnnotationTestBean {

@FooListener
@FooListener("metaTestQueue")
public void handleIt(String body) {
}
}


@RabbitListener(queues = "metaTestQueue")
@RabbitListener
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
static @interface FooListener {

@AliasFor(annotation = RabbitListener.class, attribute = "queues")
String[] value() default {};

}

@Component
Expand Down

0 comments on commit 3793fc1

Please sign in to comment.