diff --git a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/mqtt/MQTTEventAdapter.java b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/mqtt/MQTTEventAdapter.java index 0c42d056f4..de5c073e2f 100644 --- a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/mqtt/MQTTEventAdapter.java +++ b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/mqtt/MQTTEventAdapter.java @@ -96,7 +96,10 @@ public class MQTTEventAdapter implements InputEventAdapter { @Override public void connect() { - mqttAdapterListener.createConnection(); + if (!mqttAdapterListener.isConnectionInitialized()) { + mqttAdapterListener.createConnection(); + } + } @Override @@ -147,7 +150,7 @@ public class MQTTEventAdapter implements InputEventAdapter { @Override public boolean isPolling() { - return true; + return mqttAdapterListener.isConnectionInitialized(); } } diff --git a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/mqtt/util/MQTTAdapterListener.java b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/mqtt/util/MQTTAdapterListener.java index 87c77d1774..ec630d0ce5 100644 --- a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/mqtt/util/MQTTAdapterListener.java +++ b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/mqtt/util/MQTTAdapterListener.java @@ -58,6 +58,7 @@ public class MQTTAdapterListener implements MqttCallback, Runnable { private MqttClient mqttClient; private MqttConnectOptions connectionOptions; private boolean cleanSession; + private boolean connectionInitialized; private MQTTBrokerConnectionConfiguration mqttBrokerConnectionConfiguration; private String topic; @@ -279,6 +280,11 @@ public class MQTTAdapterListener implements MqttCallback, Runnable { } public void createConnection() { + connectionInitialized = true; new Thread(this).start(); } + + public boolean isConnectionInitialized() { + return connectionInitialized; + } } diff --git a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.xmpp/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/xmpp/XMPPEventAdapter.java b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.xmpp/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/xmpp/XMPPEventAdapter.java index 936e85282d..7bfe7989a8 100644 --- a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.xmpp/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/xmpp/XMPPEventAdapter.java +++ b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.xmpp/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/xmpp/XMPPEventAdapter.java @@ -103,7 +103,9 @@ public class XMPPEventAdapter implements InputEventAdapter { @Override public void connect() { - xmppAdapterListener.createConnection(); + if (xmppAdapterListener.isConnectionInitialized()) { + xmppAdapterListener.createConnection(); + } } @Override @@ -142,7 +144,7 @@ public class XMPPEventAdapter implements InputEventAdapter { @Override public boolean isPolling() { - return true; + return xmppAdapterListener.isConnectionInitialized(); } } diff --git a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.xmpp/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/xmpp/util/XMPPAdapterListener.java b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.xmpp/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/xmpp/util/XMPPAdapterListener.java index 47f1e1fb66..9821b3f42e 100644 --- a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.xmpp/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/xmpp/util/XMPPAdapterListener.java +++ b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.xmpp/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/xmpp/util/XMPPAdapterListener.java @@ -21,6 +21,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.jivesoftware.smack.ConnectionConfiguration; import org.jivesoftware.smack.PacketListener; +import org.jivesoftware.smack.ReconnectionManager; import org.jivesoftware.smack.SmackConfiguration; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPException; @@ -53,6 +54,7 @@ public class XMPPAdapterListener implements Runnable { private ContentValidator contentValidator; private ContentTransformer contentTransformer; private PacketListener packetListener; + private boolean connectionInitialized; private InputEventAdapterListener eventAdapterListener = null; @@ -217,6 +219,11 @@ public class XMPPAdapterListener implements Runnable { } public void createConnection() { + connectionInitialized = true; new Thread(this).start(); } + + public boolean isConnectionInitialized() { + return connectionInitialized; + } } diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/impl/util/VirtualFirealarmStartupListener.java b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/impl/util/VirtualFirealarmStartupListener.java index 394a148832..b9075fcf09 100644 --- a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/impl/util/VirtualFirealarmStartupListener.java +++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/impl/util/VirtualFirealarmStartupListener.java @@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.impl.util; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.core.ServerStartupObserver; +import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.internal.VirtualFirealarmManagementDataHolder; import java.io.IOException; @@ -36,7 +37,7 @@ public class VirtualFirealarmStartupListener implements ServerStartupObserver { try { VirtualFireAlarmUtils.setupMqttInputAdapter(); VirtualFireAlarmUtils.setupXmppInputAdapter(); - + VirtualFirealarmManagementDataHolder.getInstance().getInputEventAdapterService().start(); } catch (IOException e) { log.error("Failed to intilaize the virtual firealarm input adapter", e); }