Control message listener container to stop for certain period and start again to listen

前端 未结 1 623
故里飘歌
故里飘歌 2021-01-27 20:20

Listener :

 
        

        
相关标签:
1条回答
  • 2021-01-27 20:54

    maybe there is better solution but i think this one can fit :

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.jms.listener.DefaultMessageListenerContainer;
    import org.springframework.scheduling.annotation.EnableAsync;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.scheduling.annotation.Scheduled;
    
    @Configuration
    @EnableAsync
    @EnableScheduling
    public class AppConfig {
    
        @Autowired
        private DefaultMessageListenerContainer dmlc;
    
        @Scheduled(fixedDelay = 5000)
        public void start() {
            if (!dmlc.isRunning()) {
                dmlc.start();
            }
        }
    
        @Scheduled(fixedDelay = 5000)
        public void stop() {
            if (dmlc.isRunning()) {
                dmlc.stop();
            }
        }
    
        // @Scheduled(fixedDelay = 5000)
        // public void startOrStop() {
        // if (dmlc.isRunning()) {
        // dmlc.stop();
        // } else {
        // dmlc.start();
        // }
        // }
    
        @Bean
        public DefaultMessageListenerContainer dmlc() {
            DefaultMessageListenerContainer dmlc = new DefaultMessageListenerContainer();
            // dmlc.set...
            return dmlc;
        }
    }
    

    https://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html

    0 讨论(0)
提交回复
热议问题