Fix Auto-Startup for @JmsListener
Ignore container's auto-startup once the context is refreshed. Issue: SPR-14015
This commit is contained in:
@@ -109,6 +109,31 @@ public class EnableJmsTests extends AbstractJmsAnnotationDrivenTests {
|
||||
testDefaultContainerFactoryConfiguration(context);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void containerAreStartedByDefault() {
|
||||
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
EnableJmsDefaultContainerFactoryConfig.class, DefaultBean.class);
|
||||
JmsListenerContainerTestFactory factory =
|
||||
context.getBean(JmsListenerContainerTestFactory.class);
|
||||
MessageListenerTestContainer container = factory.getListenerContainers().get(0);
|
||||
assertTrue(container.isAutoStartup());
|
||||
assertTrue(container.isStarted());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void containerCanBeStarterViaTheRegistry() {
|
||||
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
EnableJmsAutoStartupFalseConfig.class, DefaultBean.class);
|
||||
JmsListenerContainerTestFactory factory =
|
||||
context.getBean(JmsListenerContainerTestFactory.class);
|
||||
MessageListenerTestContainer container = factory.getListenerContainers().get(0);
|
||||
assertFalse(container.isAutoStartup());
|
||||
assertFalse(container.isStarted());
|
||||
JmsListenerEndpointRegistry registry = context.getBean(JmsListenerEndpointRegistry.class);
|
||||
registry.start();
|
||||
assertTrue(container.isStarted());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Test
|
||||
public void jmsHandlerMethodFactoryConfiguration() throws JMSException {
|
||||
@@ -314,6 +339,23 @@ public class EnableJmsTests extends AbstractJmsAnnotationDrivenTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableJms
|
||||
static class EnableJmsAutoStartupFalseConfig implements JmsListenerConfigurer {
|
||||
|
||||
@Override
|
||||
public void configureJmsListeners(JmsListenerEndpointRegistrar registrar) {
|
||||
registrar.setContainerFactory(simpleFactory());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JmsListenerContainerTestFactory simpleFactory() {
|
||||
JmsListenerContainerTestFactory factory = new JmsListenerContainerTestFactory();
|
||||
factory.setAutoStartup(false);
|
||||
return factory;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Component
|
||||
@Lazy
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -26,9 +26,15 @@ import java.util.Map;
|
||||
*/
|
||||
public class JmsListenerContainerTestFactory implements JmsListenerContainerFactory<MessageListenerTestContainer> {
|
||||
|
||||
private boolean autoStartup = true;
|
||||
|
||||
private final Map<String, MessageListenerTestContainer> listenerContainers =
|
||||
new LinkedHashMap<>();
|
||||
|
||||
public void setAutoStartup(boolean autoStartup) {
|
||||
this.autoStartup = autoStartup;
|
||||
}
|
||||
|
||||
public List<MessageListenerTestContainer> getListenerContainers() {
|
||||
return new ArrayList<>(this.listenerContainers.values());
|
||||
}
|
||||
@@ -40,6 +46,7 @@ public class JmsListenerContainerTestFactory implements JmsListenerContainerFact
|
||||
@Override
|
||||
public MessageListenerTestContainer createListenerContainer(JmsListenerEndpoint endpoint) {
|
||||
MessageListenerTestContainer container = new MessageListenerTestContainer(endpoint);
|
||||
container.setAutoStartup(this.autoStartup);
|
||||
this.listenerContainers.put(endpoint.getId(), container);
|
||||
return container;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -31,6 +31,8 @@ public class MessageListenerTestContainer
|
||||
|
||||
private final JmsListenerEndpoint endpoint;
|
||||
|
||||
private boolean autoStartup = true;
|
||||
|
||||
private boolean startInvoked;
|
||||
|
||||
private boolean initializationInvoked;
|
||||
@@ -43,6 +45,10 @@ public class MessageListenerTestContainer
|
||||
this.endpoint = endpoint;
|
||||
}
|
||||
|
||||
public void setAutoStartup(boolean autoStartup) {
|
||||
this.autoStartup = autoStartup;
|
||||
}
|
||||
|
||||
public JmsListenerEndpoint getEndpoint() {
|
||||
return endpoint;
|
||||
}
|
||||
@@ -86,7 +92,7 @@ public class MessageListenerTestContainer
|
||||
|
||||
@Override
|
||||
public boolean isAutoStartup() {
|
||||
return true;
|
||||
return this.autoStartup;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user