AMQP-774: Add autoStartup to @RabbitListener

JIRA: https://jira.spring.io/browse/AMQP-774

Resolves https://github.com/spring-projects/spring-amqp/issues/669
This commit is contained in:
Gary Russell
2017-09-28 12:11:23 -04:00
committed by Artem Bilan
parent 8efcfb20c2
commit ffff7d7c12
7 changed files with 46 additions and 4 deletions

View File

@@ -233,4 +233,11 @@ public @interface RabbitListener {
*/
String concurrency() default "";
/**
* Set to true or false, to override the default setting in the container factory.
* @return true to auto start, false to not auto start.
* @since 2.0
*/
String autoStartup() default "";
}

View File

@@ -410,6 +410,10 @@ public class RabbitListenerAnnotationBeanPostProcessor
endpoint.setGroup((String) resolvedGroup);
}
}
String autoStartup = rabbitListener.autoStartup();
if (StringUtils.hasText(autoStartup)) {
endpoint.setAutoStartup(resolveExpressionAsBoolean(autoStartup));
}
endpoint.setExclusive(rabbitListener.exclusive());
String priority = resolve(rabbitListener.priority());

View File

@@ -340,7 +340,10 @@ public abstract class AbstractRabbitListenerContainerFactory<C extends AbstractM
if (this.applicationEventPublisher != null) {
instance.setApplicationEventPublisher(this.applicationEventPublisher);
}
if (this.autoStartup != null) {
if (endpoint.getAutoStartup() != null) {
instance.setAutoStartup(endpoint.getAutoStartup());
}
else if (this.autoStartup != null) {
instance.setAutoStartup(this.autoStartup);
}
if (this.phase != null) {

View File

@@ -71,6 +71,8 @@ public abstract class AbstractRabbitListenerEndpoint implements RabbitListenerEn
private String group;
private Boolean autoStartup;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
@@ -228,6 +230,21 @@ public abstract class AbstractRabbitListenerEndpoint implements RabbitListenerEn
this.group = group;
}
/**
* Override the default autoStartup property.
* @param autoStartup the autoStartup.
* @since 2.0
*/
public void setAutoStartup(Boolean autoStartup) {
this.autoStartup = autoStartup;
}
@Override
public Boolean getAutoStartup() {
return this.autoStartup;
}
@Override
public void setupListenerContainer(MessageListenerContainer listenerContainer) {
AbstractMessageListenerContainer container = (AbstractMessageListenerContainer) listenerContainer;

View File

@@ -48,6 +48,13 @@ public interface RabbitListenerEndpoint {
*/
String getConcurrency();
/**
* Override of the default autoStartup property.
* @return the autoStartup.
* @since 2.0
*/
Boolean getAutoStartup();
/**
* Setup the specified message listener container with the model
* defined by this endpoint.

View File

@@ -276,11 +276,13 @@ public class EnableRabbitIntegrationTests {
@Test
public void simpleDirectEndpoint() {
MessageListenerContainer container = this.registry.getListenerContainer("direct");
assertFalse(container.isRunning());
container.start();
String reply = (String) rabbitTemplate.convertSendAndReceive("test.simple.direct", "foo");
assertThat(reply, startsWith("FOOfoo"));
assertThat(reply, containsString("rabbitClientThread-")); // container runs on client thread
assertThat(TestUtils.getPropertyValue(this.registry.getListenerContainer("direct"), "consumersPerQueue"),
equalTo(2));
assertThat(TestUtils.getPropertyValue(container, "consumersPerQueue"), equalTo(2));
}
@Test
@@ -768,7 +770,7 @@ public class EnableRabbitIntegrationTests {
return foo.toUpperCase();
}
@RabbitListener(id = "direct", queues = "test.simple.direct",
@RabbitListener(id = "direct", queues = "test.simple.direct", autoStartup = "${no.property.here:false}",
containerFactory = "directListenerContainerFactory")
public String capitalizeDirect1(String foo) {
return foo.toUpperCase() + foo + Thread.currentThread().getName();

View File

@@ -133,6 +133,8 @@ Also `@QueueBinding.exchange()` now supports custom exchange types and declares
You can now set the `concurrency` of the listener container at the annotation level rather than having to configure a different container factory for different concurrency settings.
You can now set the `autoStartup` property of the listener container at the annotation level, overriding the default setting in the container factory.
See <<async-annotation-driven>> for more information.
===== Container Conditional Rollback