From 279c4a1c7ac604a12551286ca72b4d6ccaacaa5f Mon Sep 17 00:00:00 2001 From: Ace Date: Wed, 27 Jan 2016 14:25:50 +0530 Subject: [PATCH] Adding fix for startup error when MB features are not yet up --- .../ConnectedCupControllerService.java | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/modules/samples/connectedcup/component/controller/src/main/java/org/coffeeking/controller/service/ConnectedCupControllerService.java b/modules/samples/connectedcup/component/controller/src/main/java/org/coffeeking/controller/service/ConnectedCupControllerService.java index 8a83c9d6..b05fdd2d 100644 --- a/modules/samples/connectedcup/component/controller/src/main/java/org/coffeeking/controller/service/ConnectedCupControllerService.java +++ b/modules/samples/connectedcup/component/controller/src/main/java/org/coffeeking/controller/service/ConnectedCupControllerService.java @@ -36,6 +36,7 @@ import org.wso2.carbon.device.mgt.iot.exception.DeviceControllerException; import org.wso2.carbon.device.mgt.iot.sensormgt.SensorDataManager; import org.wso2.carbon.device.mgt.iot.sensormgt.SensorRecord; import org.wso2.carbon.device.mgt.iot.transport.TransportHandlerException; +import org.wso2.carbon.device.mgt.iot.DeviceManagement; import javax.servlet.http.HttpServletResponse; import javax.ws.rs.Consumes; @@ -64,14 +65,37 @@ public class ConnectedCupControllerService { } public void setconnectedCupMQTTConnector( - ConnectedCupMQTTConnector connectedCupMQTTConnector) { - ConnectedCupControllerService.connectedCupMQTTConnector = connectedCupMQTTConnector; - if (MqttConfig.getInstance().isEnabled()) { - connectedCupMQTTConnector.connect(); - } else { - log.warn("MQTT disabled in 'devicemgt-config.xml'. " + - "Hence, DigitalDisplayMqttCommunicationHandler not started."); + final ConnectedCupMQTTConnector connectedCupMQTTConnector) { + + Runnable connector = new Runnable() { + public void run() { + if (waitForServerStartup()) { + return; + } + ConnectedCupControllerService.connectedCupMQTTConnector = connectedCupMQTTConnector; + if (MqttConfig.getInstance().isEnabled()) { + connectedCupMQTTConnector.connect(); + } else { + log.warn("MQTT disabled in 'devicemgt-config.xml'. " + + "Hence, ConnectedCupMQTTConnector not started."); + } + } + }; + + Thread connectorThread = new Thread(connector); + connectorThread.setDaemon(true); + connectorThread.start(); + } + + private boolean waitForServerStartup() { + while (!DeviceManagement.isServerReady()) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + return true; + } } + return false; } /**