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

南楼画角 提交于 2019-12-02 16:33:09

问题


Listener :

 <bean id="msglistenerForAuditEvent" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="jmsFactory"/>
        <property name="sessionTransacted" value="true"/>
        <property name="destinationName" value="test.event"/>
        <property name="messageListener" ref="auditListener" />
    </bean>

I want to stop the container to listen the JMS messages and start it again after certain period?

Can it be acheived?


回答1:


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



来源:https://stackoverflow.com/questions/42925870/control-message-listener-container-to-stop-for-certain-period-and-start-again-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!