This is an automated email from the ASF dual-hosted git repository.

quantranhong1999 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 6a8b8bc8bdce3ab18598bf8a8be97fc2d28a60b4
Author: Quan Tran <[email protected]>
AuthorDate: Thu Oct 1 11:29:08 2026 +0700

    [FIX] RabbitMQ mail queue consumer health check should restart consumers
    
    When a mail queue work queue had no consumer, the health check called each
    ReconnectionHandler but never subscribed to the returned publishers, so no
    consumer was restarted. This regressed when the health check was made
    reactive in d4bf925441: JAMES-3955 used to block on each handler.
    
    The handlers are now subscribed in sequence, as SimpleConnectionPool does
    on reconnection.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../RabbitMQMailQueueConsumerHealthCheck.java      |  6 +-
 .../RabbitMQMailQueueConsumerHealthCheckTest.java  | 92 ++++++++++++++++++++++
 2 files changed, 96 insertions(+), 2 deletions(-)

diff --git 
a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueConsumerHealthCheck.java
 
b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueConsumerHealthCheck.java
index d63ef9235a..6adf86f5e9 100644
--- 
a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueConsumerHealthCheck.java
+++ 
b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueConsumerHealthCheck.java
@@ -32,6 +32,7 @@ import com.github.fge.lambdas.Throwing;
 import com.rabbitmq.client.Channel;
 import com.rabbitmq.client.Connection;
 
+import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
 import reactor.core.scheduler.Schedulers;
 
@@ -73,8 +74,9 @@ public class RabbitMQMailQueueConsumerHealthCheck implements 
HealthCheck {
             .anyMatch(Throwing.predicate(queue -> channel.consumerCount(queue) 
== 0));
 
         if (queueWithoutConsumers) {
-            return Mono.fromRunnable(() -> reconnectionHandlers.forEach(r -> 
r.handleReconnection(connection)))
-                .thenReturn(Result.degraded(COMPONENT, "No consumers"));
+            return Flux.fromIterable(reconnectionHandlers)
+                .concatMap(reconnectionHandler -> 
reconnectionHandler.handleReconnection(connection))
+                .then(Mono.just(Result.degraded(COMPONENT, "No consumers")));
         } else {
             return Mono.just(Result.healthy(COMPONENT));
         }
diff --git 
a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueConsumerHealthCheckTest.java
 
b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueConsumerHealthCheckTest.java
new file mode 100644
index 0000000000..9106ab08d8
--- /dev/null
+++ 
b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueConsumerHealthCheckTest.java
@@ -0,0 +1,92 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.queue.rabbitmq;
+
+import static org.apache.james.backends.rabbitmq.Constants.AUTO_ACK;
+import static org.apache.james.backends.rabbitmq.Constants.AUTO_DELETE;
+import static org.apache.james.backends.rabbitmq.Constants.DURABLE;
+import static org.apache.james.backends.rabbitmq.Constants.EXCLUSIVE;
+import static org.apache.james.queue.api.MailQueueFactory.SPOOL;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.james.backends.rabbitmq.RabbitMQExtension;
+import org.apache.james.backends.rabbitmq.SimpleConnectionPool;
+import org.apache.james.core.healthcheck.Result;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import com.rabbitmq.client.Channel;
+
+import reactor.core.publisher.Mono;
+
+class RabbitMQMailQueueConsumerHealthCheckTest {
+    private static final String SPOOL_WORK_QUEUE = 
MailQueueName.fromString(SPOOL.asString()).toWorkQueueName().asString();
+
+    @RegisterExtension
+    static RabbitMQExtension rabbitMQExtension = 
RabbitMQExtension.singletonRabbitMQ()
+        .isolationPolicy(RabbitMQExtension.IsolationPolicy.STRONG);
+
+    private final AtomicInteger reconnections = new AtomicInteger();
+    private Channel channel;
+    private RabbitMQMailQueueConsumerHealthCheck testee;
+
+    @BeforeEach
+    void setUp() throws Exception {
+        channel = 
rabbitMQExtension.getConnectionPool().getResilientConnection().block().createChannel();
+        channel.queueDeclare(SPOOL_WORK_QUEUE, DURABLE, !EXCLUSIVE, 
!AUTO_DELETE, ImmutableMap.of());
+
+        RabbitMQMailQueueFactory queueFactory = 
mock(RabbitMQMailQueueFactory.class);
+        
when(queueFactory.listCreatedMailQueues()).thenReturn(ImmutableSet.of(SPOOL));
+        SimpleConnectionPool.ReconnectionHandler countingHandler = connection 
-> Mono.fromRunnable(reconnections::incrementAndGet);
+
+        testee = new RabbitMQMailQueueConsumerHealthCheck(queueFactory, 
ImmutableSet.of(countingHandler), rabbitMQExtension.getConnectionPool());
+    }
+
+    @AfterEach
+    void tearDown() throws Exception {
+        channel.close();
+    }
+
+    @Test
+    void checkShouldRunReconnectionHandlersWhenAWorkQueueHasNoConsumer() {
+        Result result = testee.check().block();
+
+        assertThat(result.isDegraded()).isTrue();
+        assertThat(reconnections).hasValue(1);
+    }
+
+    @Test
+    void checkShouldNotRunReconnectionHandlersWhenWorkQueuesHaveConsumers() 
throws Exception {
+        channel.basicConsume(SPOOL_WORK_QUEUE, AUTO_ACK, (consumerTag, 
delivery) -> { }, consumerTag -> { });
+
+        Result result = testee.check().block();
+
+        assertThat(result.isHealthy()).isTrue();
+        assertThat(reconnections).hasValue(0);
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to