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

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 2bfaff46fff6 CAMEL-27040: camel-paho-mqtt5 - manualAcksEnabled: an 
exchange that completes after the consumer stopped fails with a 
NullPointerException, or acknowledges on the client of the next start (#30246)
2bfaff46fff6 is described below

commit 2bfaff46fff65c6ab4ebe97533e1d98186699532
Author: allthingssecurity <[email protected]>
AuthorDate: Tue Nov 17 14:29:59 2026 +0530

    CAMEL-27040: camel-paho-mqtt5 - manualAcksEnabled: an exchange that 
completes after the consumer stopped fails with a NullPointerException, or 
acknowledges on the client of the next start (#30246)
    
    …ompletes after the consumer stopped fails with a NullPointerException, or 
acknowledges on the client of the next start
    
    A route stop does not wait for the exchanges in flight before it stops the
    consumer (DefaultConsumer is neither Suspendable nor ShutdownAware), and
    doStop closes the own client of the consumer and sets client to null. An
    exchange that completed after that threw a NullPointerException in the
    onComplete of its manual acknowledgement, which the unit of work logged
    as a WARN. After the consumer started again with a new client, onComplete
    acknowledged the packet id of the closed connection on the new client,
    and onFailure (rollback) acknowledged it there or made it reconnect.
    
    The on-completion now keeps the client the message arrived on, and
    onComplete and rollback do nothing (DEBUG log) when the current client is
    null or another one. On a session the broker keeps, the broker sends the
    unacknowledged message again to a client with the same client id;
    otherwise the session ended with the connection. A client given with the
    client option is the same object across a restart, so its messages are
    acknowledged as before. Such an exchange no longer resets the backoff of
    the current client either.
    
    New PahoMqtt5ConsumerAckAfterStopTest.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../component/paho/mqtt5/PahoMqtt5Consumer.java    |  25 +++-
 .../mqtt5/PahoMqtt5ConsumerAckAfterStopTest.java   | 162 +++++++++++++++++++++
 2 files changed, 183 insertions(+), 4 deletions(-)

diff --git 
a/components/camel-paho-mqtt5/src/main/java/org/apache/camel/component/paho/mqtt5/PahoMqtt5Consumer.java
 
b/components/camel-paho-mqtt5/src/main/java/org/apache/camel/component/paho/mqtt5/PahoMqtt5Consumer.java
index 5c0ba1eec792..b0cd71b1d419 100644
--- 
a/components/camel-paho-mqtt5/src/main/java/org/apache/camel/component/paho/mqtt5/PahoMqtt5Consumer.java
+++ 
b/components/camel-paho-mqtt5/src/main/java/org/apache/camel/component/paho/mqtt5/PahoMqtt5Consumer.java
@@ -299,9 +299,10 @@ public class PahoMqtt5Consumer extends DefaultConsumer {
      * always fails does not reconnect in a tight loop. In every other case 
the broker never sends the message again, so
      * it is acknowledged, which frees its place in the receive window.
      */
-    private void rollback(Exchange exchange, MqttMessage mqttMessage) {
+    private void rollback(Exchange exchange, MqttMessage mqttMessage, 
MqttClient receivingClient) {
         MqttClient current = client;
-        if (current == null) {
+        if (current == null || current != receivingClient) {
+            logNotAcknowledgedAfterStop(exchange, mqttMessage);
             return;
         }
         if (mqttMessage.getQos() != 1 || !isSessionKeptAfterDisconnect()) {
@@ -422,12 +423,21 @@ public class PahoMqtt5Consumer extends DefaultConsumer {
 
         exchange.setIn(paho);
         if (getEndpoint().getConfiguration().isManualAcksEnabled()) {
+            // the message is acknowledged on the client it arrived on: once 
the consumer stopped, that client is closed
+            // (or replaced by the client of the next start), and its packet 
id means nothing to another client
+            MqttClient receivingClient = client;
             exchange.getExchangeExtension().addOnCompletion(new 
SynchronizationAdapter() {
                 @Override
                 public void onComplete(Exchange exchange) {
+                    MqttClient current = client;
+                    if (current == null || current != receivingClient) {
+                        // not a message of the current client: its backoff is 
left as is
+                        logNotAcknowledgedAfterStop(exchange, mqttMessage);
+                        return;
+                    }
                     failedMessageReconnects.set(0);
                     try {
-                        
PahoMqtt5Consumer.this.client.messageArrivedComplete(mqttMessage.getId(), 
mqttMessage.getQos());
+                        current.messageArrivedComplete(mqttMessage.getId(), 
mqttMessage.getQos());
                     } catch (MqttException e) {
                         getExceptionHandler().handleException(
                                 "Error acknowledging MQTT message with ID: " + 
mqttMessage.getId(),
@@ -437,11 +447,18 @@ public class PahoMqtt5Consumer extends DefaultConsumer {
 
                 @Override
                 public void onFailure(Exchange exchange) {
-                    rollback(exchange, mqttMessage);
+                    rollback(exchange, mqttMessage, receivingClient);
                 }
             });
         }
         return exchange;
     }
 
+    private void logNotAcknowledgedAfterStop(Exchange exchange, MqttMessage 
mqttMessage) {
+        LOG.debug("MQTT message with ID: {} of Exchange ID: {} is not 
acknowledged: the consumer stopped and closed the client"
+                  + " it arrived on (on a session that the broker keeps, the 
broker sends it again to a client with the"
+                  + " same client id)",
+                mqttMessage.getId(), exchange.getExchangeId());
+    }
+
 }
diff --git 
a/components/camel-paho-mqtt5/src/test/java/org/apache/camel/component/paho/mqtt5/PahoMqtt5ConsumerAckAfterStopTest.java
 
b/components/camel-paho-mqtt5/src/test/java/org/apache/camel/component/paho/mqtt5/PahoMqtt5ConsumerAckAfterStopTest.java
new file mode 100644
index 000000000000..00a267be5e9e
--- /dev/null
+++ 
b/components/camel-paho-mqtt5/src/test/java/org/apache/camel/component/paho/mqtt5/PahoMqtt5ConsumerAckAfterStopTest.java
@@ -0,0 +1,162 @@
+/*
+ * 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.camel.component.paho.mqtt5;
+
+import java.util.ArrayDeque;
+import java.util.Deque;
+import java.util.List;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.spi.Synchronization;
+import org.eclipse.paho.mqttv5.client.MqttClient;
+import org.eclipse.paho.mqttv5.common.MqttMessage;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThatNoException;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+
+/**
+ * With manualAcksEnabled=true a message is acknowledged when its exchange 
completes. A route stop does not wait for the
+ * exchanges in flight before it stops the consumer, which closes its own 
client, so an exchange can complete after the
+ * consumer stopped, or after it started again with a new client.
+ */
+class PahoMqtt5ConsumerAckAfterStopTest {
+
+    private final Deque<MqttClient> clients = new ArrayDeque<>();
+    private CamelContext context;
+    private PahoMqtt5Consumer consumer;
+
+    @BeforeEach
+    void setUp() throws Exception {
+        context = new DefaultCamelContext();
+        context.start();
+        PahoMqtt5Configuration configuration = new PahoMqtt5Configuration();
+        configuration.setManualAcksEnabled(true);
+        PahoMqtt5Endpoint endpoint = new PahoMqtt5Endpoint(
+                "paho-mqtt5:test", "test", context.getComponent("paho-mqtt5", 
PahoMqtt5Component.class), configuration);
+        consumer = new PahoMqtt5Consumer(endpoint, exchange -> {
+        }) {
+            @Override
+            MqttClient createClient() {
+                return clients.removeFirst();
+            }
+        };
+    }
+
+    @AfterEach
+    void tearDown() {
+        context.stop();
+    }
+
+    @Test
+    void exchangeCompletingWhileRunningIsAcknowledged() throws Exception {
+        MqttClient client = addClient();
+        consumer.start();
+        Exchange exchange = consumer.createExchange(message(7), "test");
+
+        complete(exchange);
+
+        verify(client).messageArrivedComplete(7, 1);
+        consumer.stop();
+    }
+
+    @Test
+    void exchangeCompletingAfterStopIsNotAcknowledged() throws Exception {
+        MqttClient client = addClient();
+        consumer.start();
+        Exchange exchange = consumer.createExchange(message(7), "test");
+        consumer.stop();
+
+        assertThatNoException().isThrownBy(() -> complete(exchange));
+
+        verify(client, never()).messageArrivedComplete(anyInt(), anyInt());
+    }
+
+    @Test
+    void exchangeCompletingAfterRestartIsNotAcknowledgedOnTheNewClient() 
throws Exception {
+        addClient();
+        MqttClient newClient = addClient();
+        consumer.start();
+        Exchange exchange = consumer.createExchange(message(7), "test");
+        consumer.stop();
+        consumer.start();
+
+        complete(exchange);
+
+        // packet id 7 of the closed client is not a message of the new client
+        verify(newClient, never()).messageArrivedComplete(anyInt(), anyInt());
+        consumer.stop();
+    }
+
+    @Test
+    void exchangeFailingAfterRestartIsNotAcknowledgedOnTheNewClient() throws 
Exception {
+        addClient();
+        MqttClient newClient = addClient();
+        consumer.start();
+        Exchange exchange = consumer.createExchange(message(7), "test");
+        consumer.stop();
+        consumer.start();
+
+        exchange.setException(new IllegalStateException("failed"));
+        List<Synchronization> completions = 
exchange.getExchangeExtension().handoverCompletions();
+        completions.forEach(completion -> completion.onFailure(exchange));
+
+        verify(newClient, never()).messageArrivedComplete(anyInt(), anyInt());
+        consumer.stop();
+    }
+
+    @Test
+    void exchangeOfClientOptionCompletingAfterRestartIsAcknowledged() throws 
Exception {
+        // a client given with the client option is kept across a stop and 
used again by the next start
+        MqttClient client = mock(MqttClient.class);
+        consumer.setClient(client);
+        consumer.start();
+        Exchange exchange = consumer.createExchange(message(7), "test");
+        consumer.stop();
+        consumer.start();
+
+        complete(exchange);
+
+        verify(client).messageArrivedComplete(7, 1);
+        consumer.stop();
+    }
+
+    private MqttClient addClient() {
+        MqttClient client = mock(MqttClient.class);
+        clients.add(client);
+        return client;
+    }
+
+    private static MqttMessage message(int id) {
+        MqttMessage message = new MqttMessage("body".getBytes());
+        message.setId(id);
+        message.setQos(1);
+        return message;
+    }
+
+    private static void complete(Exchange exchange) {
+        List<Synchronization> completions = 
exchange.getExchangeExtension().handoverCompletions();
+        completions.forEach(completion -> completion.onComplete(exchange));
+    }
+}

Reply via email to