springboot 同时监听queue和topic

自定义两个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)