1

I am using spring boot and have @EnableJms

If i am using only annotation @JmsListener(destination = "xxxxxx") and depend on spring boot autoconfiguration , how can i start and stop jmslistener knowing that i set it to auto-start false

 jms:
    listener:
      max-concurrency: 10
      concurrency: 1
      auto-startup: false

The question is how can i access the SimpleMessageListenerContainer or DefaultMessageListenerContainer

Shahbour
  • 1,323
  • 1
  • 16
  • 42

1 Answers1

3

See this answer - individual containers are accessible via the registry, or you can start/stop the registry to start/stop them all.

EDIT:

It's a bug, I opened a JIRA Issue; a work around is to reset the auto-startup before starting...

@SpringBootApplication
public class So36332914Application {

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext context = SpringApplication.run(So36332914Application.class, args);
        JmsTemplate template = context.getBean(JmsTemplate.class);
        JmsListenerEndpointRegistry registry = context.getBean(JmsListenerEndpointRegistry.class);
        System.out.println(registry.getListenerContainerIds().size() + " containers");
        System.out.println("Running: " + registry.isRunning());

        // https://jira.spring.io/browse/SPR-14105
        for (MessageListenerContainer container : registry.getListenerContainers()) {
            ((AbstractJmsListeningContainer) container).setAutoStartup(true);
        }
        registry.start();
        System.out.println("Running: " + registry.isRunning());
        template.convertAndSend("foo", "bar");
        registry.stop();
        System.out.println("Running: " + registry.isRunning());
        context.getBean(Foo.class).latch.await(10, TimeUnit.SECONDS);
        context.close();
    }

    @Bean
    public Foo foo() {
        return new Foo();
    }

    public static class Foo {

        private final CountDownLatch latch = new CountDownLatch(1);

        @JmsListener(destination="foo")
        public void foo(String foo) {
            System.out.println(foo);
            latch.countDown();
        }

    }

}
Community
  • 1
  • 1
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Based on the above answer if i do stop / start it is working perfectly . But if i put auto-start to false in the properties file and then tried to start after my initialization it don't start , any idea – Shahbour Mar 31 '16 at 22:33
  • Looks like a bug; I opened a JIRA issue - see my edit for a work-around. – Gary Russell Apr 01 '16 at 13:15
  • thanks for the update , i did loop over them and start on the container directly but fixing the auto-start is more clean – Shahbour Apr 01 '16 at 13:36
  • Have a look around here, not exaclly what you're trying to achieve but ... http://stackoverflow.com/questions/32588352/how-can-i-stop-start-pause-a-jmslistener-the-clean-way/33214651#33214651 – Seb May 26 '16 at 14:46