有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java多个持久的侦听器。他们同时工作吗?

我的配置:

@Bean
    public ActiveMQConnectionFactory connectionFactory(){
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL(DEFAULT_BROKER_URL);
        return connectionFactory;
    }
 @Bean
    public DefaultMessageListenerContainer listenerContainers() {
        DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
        container.setConnectionFactory(connectionFactory());
        //container.setConnectionFactory(connectionFactory1());
        container.setClientId("consumer1");
        container.setDestinationName(COMMENT_QUEUE);
        container.setPubSubDomain(true);
        container.setSessionTransacted(true);
        container.setSubscriptionDurable(true);
        container.setMessageListener(datafileSubscriber);
        container.start();
        return container;
    }

    @Bean
    public DefaultMessageListenerContainer listenerContainers1() {
        DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
        container.setConnectionFactory(connectionFactory());
        container.setClientId("consumer2");
        container.setDestinationName(COMMENT_QUEUE);
        container.setPubSubDomain(true);
        container.setSessionTransacted(true);
        container.setSubscriptionDurable(true);
        container.setMessageListener(datafileSubscriber);
        container.start();
        return container;
    } 

我需要将消息发布到多个侦听器。所有侦听器都执行相同的代码。我希望它们经久耐用。我把这件事说成是真的。这是一种酒吧/酒吧模式

我的想法是,如果一个侦听器将执行代码。其他侦听器只需发送确认即可发送。这样他们可以得到另一个信息

我的假设是: 代理向两个侦听器发送消息。其中一个会立即确认,而另一个会处理
现在经纪人收到了另一条消息。因为第一个侦听器没有发送确认,所以它会将消息发送给第二个侦听器 它将消息放入第一个侦听器的队列中,以便在第一个侦听器确认前一条消息时发送消息

我这里的重要疑问: activemq代理是否在所有侦听器都未确认的情况下发送另一条消息

我认为这个概念是,每个侦听器都将在代理中维护一个队列。当代理获取消息时,它会将消息推送到 每个侦听器的队列。如果侦听器空闲,它将接收消息。如果正在忙于处理,则在发送确认之前,消息 我会排队的。确认后,下一条消息将传递给侦听器

我只是在我拥有的属性、持久订阅者、setsession Transaction true的上下文中这样说

我尝试过但失败了 我尝试将concurrent consumer属性设置为2,并将其设置为持久订户。看起来如果它是一个持久订户,它需要一个 唯一的客户端ID。因此,我切换到使用多个容器和并发消费者属性1

编辑: 我在这里所说的一切都是在我使用持久订阅者、SetSessionTransact-true和相同消息侦听器的配置上下文中进行的


共 (1) 个答案

  1. # 1 楼答案

    My assumption here: The broker sends a message to both the listeners. One of them acknowledges immediately, while the other processes it. Now the broker got another message. Since the 1st listener didnt send an acknowledgement, it will send the message to the 2nd listener and it puts the message in a queue for 1st listener so that it can send whenever the 1st listener acknowledges the previous message.

    它根本不是这样工作的,消费者/订阅是相互独立的。每个用户没有“队列”;只是话题;对于持久订阅,代理跟踪消费者发送的最后一条消息;当所有持久订阅都收到该消息时,该消息将被删除

    向使用者发送消息的实际过程取决于其他因素,例如,ActiveMQ支持预回迁(默认值1000),这意味着它将在不等待确认的情况下发送到该数字

    您必须将SessionTransact与DMLC一起使用,以便在侦听器完成之前不会提交ack

    concurrent consumer property to 2

    正如我在回答你的另一个问题时所说的,当从一个主题消费时,增加并发性是没有意义的