Add auto-startup configuration property

Allow to disable the auto-startup flag of the default JMS and RabbitMQ
containers. This effectively permit to disable automatic listening via
configuration.

Closes gh-3587
pull/3598/head
Stephane Nicoll 9 years ago
parent aa3393f18c
commit d6e914be87

@ -44,6 +44,7 @@ class RabbitAnnotationDrivenConfiguration {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
Listener listenerConfig = config.getListener();
factory.setAutoStartup(listenerConfig.isAutoStartup());
if (listenerConfig.getAcknowledgeMode() != null) {
factory.setAcknowledgeMode(listenerConfig.getAcknowledgeMode());
}

@ -285,6 +285,11 @@ public class RabbitProperties {
public static class Listener {
/**
* Start the container automatically on startup.
*/
private boolean autoStartup = true;
/**
* Acknowledge mode of container.
*/
@ -312,6 +317,14 @@ public class RabbitProperties {
*/
private Integer transactionSize;
public boolean isAutoStartup() {
return autoStartup;
}
public void setAutoStartup(boolean autoStartup) {
this.autoStartup = autoStartup;
}
public AcknowledgeMode getAcknowledgeMode() {
return this.acknowledgeMode;
}

@ -68,6 +68,7 @@ class JmsAnnotationDrivenConfiguration {
factory.setDestinationResolver(this.destinationResolver);
}
JmsProperties.Listener listener = this.properties.getListener();
factory.setAutoStartup(listener.isAutoStartup());
if (listener.getAcknowledgeMode() != null) {
factory.setSessionAcknowledgeMode(listener.getAcknowledgeMode().getMode());
}

@ -23,6 +23,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
*
* @author Greg Turnquist
* @author Phillip Webb
* @author Stephane Nicoll
*/
@ConfigurationProperties(prefix = "spring.jms")
public class JmsProperties {
@ -62,6 +63,11 @@ public class JmsProperties {
public static class Listener {
/**
* Start the container automatically on startup.
*/
private boolean autoStartup = true;
/**
* Acknowledge mode of the container. By default, the listener is
* transacted with automatic acknowledgment.
@ -78,6 +84,14 @@ public class JmsProperties {
*/
private Integer maxConcurrency;
public boolean isAutoStartup() {
return autoStartup;
}
public void setAutoStartup(boolean autoStartup) {
this.autoStartup = autoStartup;
}
public AcknowledgeMode getAcknowledgeMode() {
return acknowledgeMode;
}

@ -190,6 +190,7 @@ public class RabbitAutoConfigurationTests {
@Test
public void testRabbitListenerContainerFactoryWithCustomSettings() {
load(TestConfiguration.class,
"spring.rabbitmq.listener.autoStartup:false",
"spring.rabbitmq.listener.acknowledgeMode:manual",
"spring.rabbitmq.listener.concurrency:5",
"spring.rabbitmq.listener.maxConcurrency:10",
@ -199,6 +200,7 @@ public class RabbitAutoConfigurationTests {
.getBean("rabbitListenerContainerFactory",
SimpleRabbitListenerContainerFactory.class);
DirectFieldAccessor dfa = new DirectFieldAccessor(rabbitListenerContainerFactory);
assertEquals(false, dfa.getPropertyValue("autoStartup"));
assertEquals(AcknowledgeMode.MANUAL,
dfa.getPropertyValue("acknowledgeMode"));
assertEquals(5, dfa.getPropertyValue("concurrentConsumers"));

@ -147,6 +147,7 @@ public class JmsAutoConfigurationTests {
@Test
public void testJmsListenerContainerFactoryWithCustomSettings() {
load(EnableJmsConfiguration.class,
"spring.jms.listener.autoStartup=false",
"spring.jms.listener.acknowledgeMode=client",
"spring.jms.listener.concurrency=2",
"spring.jms.listener.maxConcurrency=10");
@ -156,9 +157,10 @@ public class JmsAutoConfigurationTests {
jmsListenerContainerFactory.getClass());
DefaultMessageListenerContainer listenerContainer = ((DefaultJmsListenerContainerFactory)
jmsListenerContainerFactory).createListenerContainer(mock(JmsListenerEndpoint.class));
assertEquals(false, listenerContainer.isAutoStartup());
assertEquals(Session.CLIENT_ACKNOWLEDGE, listenerContainer.getSessionAcknowledgeMode());
assertEquals(2, listenerContainer.getConcurrentConsumers());
assertEquals(10, listenerContainer.getMaxConcurrentConsumers());
assertEquals(Session.CLIENT_ACKNOWLEDGE, listenerContainer.getSessionAcknowledgeMode());
}
@Test

@ -475,6 +475,7 @@ content into your application; rather pick only the properties that you need.
spring.rabbitmq.password= # login password
spring.rabbitmq.requested-heartbeat= # requested heartbeat timeout, in seconds; zero for none
spring.rabbitmq.listener.acknowledge-mode= # acknowledge mode of container
spring.rabbitmq.listener.auto-startup=true # start the container automatically on startup
spring.rabbitmq.listener.concurrency= # minimum number of consumers
spring.rabbitmq.listener.max-concurrency= # maximum number of consumers
spring.rabbitmq.listener.prefetch= # number of messages to be handled in a single request
@ -534,6 +535,7 @@ content into your application; rather pick only the properties that you need.
# JMS ({sc-spring-boot-autoconfigure}/jms/JmsProperties.{sc-ext}[JmsProperties])
spring.jms.jndi-name= # JNDI location of a JMS ConnectionFactory
spring.jms.listener.acknowledge-mode= # session acknowledgment mode
spring.jms.listener.auto-startup=true # start the container automatically on startup
spring.jms.listener.concurrency= # minimum number of concurrent consumers
spring.jms.listener.max-concurrency= # maximum number of concurrent consumers
spring.jms.pub-sub-domain= # false for queue (default), true for topic

Loading…
Cancel
Save