You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2020/10/25 15:45:01 UTC

[camel] branch camel-3.4.x updated: [CAMEL-15748] nullify mqtt client on producer and consumer stop (#4509)

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

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


The following commit(s) were added to refs/heads/camel-3.4.x by this push:
     new da07bf5  [CAMEL-15748] nullify mqtt client on producer and consumer stop (#4509)
da07bf5 is described below

commit da07bf50799f8bfea816b12cd5c9e4b880c7b3f2
Author: Marco Collovati <mc...@gmail.com>
AuthorDate: Sun Oct 25 16:44:43 2020 +0100

    [CAMEL-15748] nullify mqtt client on producer and consumer stop (#4509)
    
    * CAMEL-15748: nullify mqtt client on producer and consumer stop
    
    When restarting a route with a paho consumer or using a paho producer
    after a connection failure to the broker the mqtt client was not
    reconnected. This patch sets consumer and producer internl mqtt  client
    instance to null os stop, so that a restart will create a new instance
    and connect to the broker again.
    
    * Added ASF license header on PahoReconnectAfterFailureTest
---
 .../apache/camel/component/paho/PahoConsumer.java  |   2 +-
 .../apache/camel/component/paho/PahoProducer.java  |   2 +-
 .../paho/PahoReconnectAfterFailureTest.java        | 144 +++++++++++++++++++++
 3 files changed, 146 insertions(+), 2 deletions(-)

diff --git a/components/camel-paho/src/main/java/org/apache/camel/component/paho/PahoConsumer.java b/components/camel-paho/src/main/java/org/apache/camel/component/paho/PahoConsumer.java
index 0e28700..e51c66f 100644
--- a/components/camel-paho/src/main/java/org/apache/camel/component/paho/PahoConsumer.java
+++ b/components/camel-paho/src/main/java/org/apache/camel/component/paho/PahoConsumer.java
@@ -126,8 +126,8 @@ public class PahoConsumer extends DefaultConsumer {
             }
             LOG.debug("Disconnecting client: {} from broker: {}", clientId, getEndpoint().getConfiguration().getBrokerUrl());
             client.disconnect();
-            client = null;
         }
+        client = null;
     }
 
     @Override
diff --git a/components/camel-paho/src/main/java/org/apache/camel/component/paho/PahoProducer.java b/components/camel-paho/src/main/java/org/apache/camel/component/paho/PahoProducer.java
index 8684824..f7a0d7d 100644
--- a/components/camel-paho/src/main/java/org/apache/camel/component/paho/PahoProducer.java
+++ b/components/camel-paho/src/main/java/org/apache/camel/component/paho/PahoProducer.java
@@ -92,8 +92,8 @@ public class PahoProducer extends DefaultProducer {
         if (stopClient && client != null && client.isConnected()) {
             LOG.debug("Disconnecting client: {} from broker: {}", clientId, getEndpoint().getConfiguration().getBrokerUrl());
             client.disconnect();
-            client = null;
         }
+        client = null;
     }
 
 }
diff --git a/components/camel-paho/src/test/java/org/apache/camel/component/paho/PahoReconnectAfterFailureTest.java b/components/camel-paho/src/test/java/org/apache/camel/component/paho/PahoReconnectAfterFailureTest.java
new file mode 100644
index 0000000..0f0bc6f
--- /dev/null
+++ b/components/camel-paho/src/test/java/org/apache/camel/component/paho/PahoReconnectAfterFailureTest.java
@@ -0,0 +1,144 @@
+/*
+ * 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;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.activemq.broker.BrokerService;
+import org.apache.camel.CamelContext;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Route;
+import org.apache.camel.ServiceStatus;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.spi.RouteController;
+import org.apache.camel.spi.SupervisingRouteController;
+import org.apache.camel.support.RoutePolicySupport;
+import org.apache.camel.test.AvailablePortFinder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.After;
+import org.junit.Test;
+
+public class PahoReconnectAfterFailureTest extends CamelTestSupport {
+
+    public static final String TESTING_ROUTE_ID = "testingRoute";
+    BrokerService broker;
+
+    int mqttPort = AvailablePortFinder.getNextAvailable();
+    CountDownLatch routeStartedLatch = new CountDownLatch(1);
+
+    @EndpointInject("mock:test")
+    MockEndpoint mock;
+
+    @Override
+    protected boolean useJmx() {
+        return false;
+    }
+
+    @Override
+    public void doPreSetup() throws Exception {
+        super.doPreSetup();
+        broker = new BrokerService();
+        broker.setPersistent(false);
+        broker.addConnector("mqtt://localhost:" + mqttPort);
+        // Broker will be started later, after camel context is started,
+        // to ensure first consumer connection fails
+    }
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+        // Setup supervisor to restart routes because paho consumer 
+        // is not able to recover automatically on startup
+        SupervisingRouteController supervising = context.getRouteController().supervising();
+        supervising.setBackOffDelay(500);
+        supervising.setIncludeRoutes("paho:*");
+        return context;
+    }
+
+    @Override
+    @After
+    public void tearDown() throws Exception {
+        super.tearDown();
+        broker.stop();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+
+                from("direct:test").to("paho:queue?lazyStartProducer=true&brokerUrl=tcp://localhost:" + mqttPort);
+                from("paho:queue?brokerUrl=tcp://localhost:" + mqttPort)
+                    .id(TESTING_ROUTE_ID)
+                    .routePolicy(new RoutePolicySupport() {
+                        @Override
+                        public void onStart(Route route) {
+                            routeStartedLatch.countDown();
+                        }
+                    })
+                    .to("mock:test");
+            }
+        };
+    }
+
+
+    @Test
+    public void startConsumerShouldReconnectMqttClientAfterFailures() throws Exception {
+        RouteController routeController = context.getRouteController();
+
+        assertNotEquals("Broker down, expecting  route not to be started", ServiceStatus.Started, routeController.getRouteStatus(TESTING_ROUTE_ID));
+
+        // Start broker and wait for supervisor to restart route
+        // consumer should now connect
+        broker.start();
+        routeStartedLatch.await(5, TimeUnit.SECONDS);
+        assertEquals("Expecting consumer connected to broker and route started", ServiceStatus.Started, routeController.getRouteStatus(TESTING_ROUTE_ID));
+
+        // Given
+        String msg = "msg";
+        mock.expectedBodiesReceived(msg);
+
+        // When
+        template.sendBody("paho:queue?lazyStartProducer=true&brokerUrl=tcp://localhost:" + mqttPort, msg);
+
+        // Then
+        mock.assertIsSatisfied();
+
+    }
+
+    @Test
+    public void startProducerShouldReconnectMqttClientAfterFailures() throws Exception {
+        String msg = "msg";
+        mock.expectedBodiesReceived(msg);
+
+        try {
+            template.sendBody("direct:test", "notSentMessage");
+            fail("Broker is down, paho producer should fail");
+        }catch (Exception e) {
+            // ignore
+        }
+
+        broker.start();
+
+        template.sendBody("direct:test", msg);
+
+        mock.assertIsSatisfied();
+    }
+}