美文网首页
springboot 同时监听queue和topic

springboot 同时监听queue和topic

作者: 孙大圣_0507 | 来源:发表于2017-06-07 17:43 被阅读0次

    自定义两个ContainerFactory,setPubSubDomain(false)为监听queue,setPubSubDomain(true)为监听topic;

    @Bean

    public DefaultJmsListenerContainerFactory queueListenerFactory(){

        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();

        factory.setConnectionFactory(connectionFactory);

        factory.setPubSubDomain(false);

        return factory;

    }

    @Bean

    public DefaultJmsListenerContainerFactory topicListenerFactory(){

        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();

        factory.setConnectionFactory(connectionFactory);

        factory.setPubSubDomain(true);

        return factory;

    }

    监听队列时指定@JmsListener的containerFactory为上面定义的queueListenerFactory

    @JmsListener(destination="${newsedit.queue}",containerFactory="queueListenerFactory")

    public voidreceiveQueueMsg(String text){

        try{

            PublishServiceQueueMsg msg =mapper.readValue(text,PublishServiceQueueMsg.class);

            msgHandler.handler(msg);

        }catch(IOException e) {

            logger.error("receiveQueueMsg error!",e);

            e.printStackTrace();

        }

    }

    监听主题时指定@JmsListener的containerFactory为上面定义的topicListenerFactory

    @JmsListener(destination="${newsedit.topic}",containerFactory="topicListenerFactory")

    public voidreceiveTopicMsg(String text){

        try{

            PublishServiceTopicMsg msg =mapper.readValue(text,PublishServiceTopicMsg.class);

            msgHandler.handler(msg);

        }catch(IOException e) {

            logger.error("receiveTopicMsg error!",e);

            e.printStackTrace();

        }

    }

    相关文章

      网友评论

          本文标题:springboot 同时监听queue和topic

          本文链接:https://www.haomeiwen.com/subject/tpdafxtx.html