Shahbour
Shahbour

Reputation: 1323

Start and Stop @JmsListener

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

Upvotes: 1

Views: 5994

Answers (1)

Gary Russell
Gary Russell

Reputation: 174729

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();
        }

    }

}

Upvotes: 3

Related Questions