简体   繁体   English

spring jms如何在持久主题侦听器之间分发消息?

[英]How does spring jms distribute messages among durable topic listeners?

raw jms code: 原始的jms代码:

TopicSubscriber durSubscriber1 = receiverSession.createDurableSubscriber(topic,"subscription_1");
durSubscriber1.setMessageListener(new MessageListener() {
    @Override
    public void onMessage(Message message) {

        RMQTextMessage rmqTextMessage = ((RMQTextMessage) message);
        try {
            System.out.println("sub_1:" + rmqTextMessage.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }

    }
});


TopicSubscriber durSubscriber2 = receiverSession.createDurableSubscriber(topic,"subscription_2");
    durSubscriber2.setMessageListener(new MessageListener() {
        @Override
        public void onMessage(Message message) {

            RMQTextMessage rmqTextMessage = ((RMQTextMessage) message);
            try {
                System.out.println("sub_2:" + rmqTextMessage.getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }

        }
});

When I use following code each listener gets all messages. 当我使用以下代码时,每个侦听器都会获取所有消息。 If I use same subscription name - application doesn't start. 如果我使用相同的订阅名称-应用程序无法启动。

From another hand I wrote code which uses spring jms: 我从另一手编写了使用spring jms的代码:

config: 配置:

@Bean
public JmsListenerContainerFactory<?> myFactory(DefaultJmsListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    // This provides all boot's default to this factory, including the message converter
    configurer.configure(factory, connectionFactory);
    // You could still override some of Boot's default if necessary.
    factory.setPubSubDomain(true);
    factory.setSubscriptionDurable(true);
    return factory;
}

listeners: 听众:

@JmsListener(destination = "my_topic_new", containerFactory = "myFactory")
public void receiveTopic(Email email) {
    System.out.println("list_1:" + email);
}

@JmsListener(destination = "my_topic_new", containerFactory = "myFactory")
public void receiveTopicDup(Email email) {
    System.out.println("list_2:" + email);
}

At this case both listeners divide messages. 在这种情况下,两个侦听器都将消息分开。 I mean that if producer sends 10 messages then listener_1 will get N messages and listener_2 will get M messages. 我的意思是,如果生产者发送10条消息,则listener_1将获得N条消息,listener_2将获得M条消息。
M+N=10 M + N = 10

Please explain difference at 2 code snippets. 请在2个代码段中说明差异。 Can you provide corresponding jms code for spring-jms version ? 您可以为spring-jms版本提供相应的jms代码吗?

It's due to the way the rabbit JMS client maps JMS to native AMQP. 这是由于Rabbit JMS客户端将JMS映射到本地AMQP的方式所致。 You have to give each listener a subscription name; 您必须为每个侦听器指定一个订阅名称; otherwise they will compete for messages in the same queue. 否则,他们将竞争同一队列中的消息。

@JmsListener(destination = "my_topic_new", containerFactory = "myFactory", subscription = "foo")
public void receiveTopic(String email) {
    System.out.println("list_1:" + email);
}

@JmsListener(destination = "my_topic_new", containerFactory = "myFactory", subscription = "bar")
public void receiveTopicDup(String email) {
    System.out.println("list_2:" + email);
}

This is only needed for durable subscriptions. 仅对于持久订阅才需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring JMS-独立的持久主题订阅者 - Spring JMS - standalone durable topic subscriber 使用ActiveMQ创建持久的主题和订阅者spring boot jms - Create durable topic and subscriber spring boot jms with ActiveMQ JMS:如何将DLQ中的消息重新传递给主题订户? - JMS: how to redeliver messages in the DLQ to a Topic Subscriber? 一个activemq主题可以与多个持久订户一起容纳多少条消息? - How many messages can an activemq topic hold with multiple durable subscribers? JMS多个持久订阅一个主题 - JMS multiple durable subscription to one topic 经纪人网络中的ActiveMQ JMS持久主题 - ActiveMQ JMS Durable Topic in a Network of Brokers 如何在Spring Jms Tibjms中在多个VM之间共享主题上发布的消息 - How to share messages, published on Topic, between multiple VMs, in Spring Jms Tibjms 如何使用java和spring 3.0从JMS主题(而不是队列)同时处理多个消息? - How can I handle multiple messages concurrently from a JMS topic (not queue) with java and spring 3.0? 如何在Spring中过滤JMS消息 - How to filter jms messages in spring 传入消息在来自 Azure 服务总线主题订阅的侦听器之间划分 - Incoming messages are dividing among listeners from Azure service bus topic subscription
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM