From d7b423d694c59d1ef9883a39caf1343ea51948c1 Mon Sep 17 00:00:00 2001 From: ayyoob Date: Thu, 19 May 2016 00:28:08 +0530 Subject: [PATCH] fixed exponential backup logic --- .../mqtt/util/MQTTAdapterListener.java | 41 ++++++++++--------- .../mqtt/util/MQTTEventAdapterConstants.java | 4 +- .../xmpp/util/XMPPAdapterListener.java | 4 +- .../xmpp/util/XMPPEventAdapterConstants.java | 4 +- 4 files changed, 28 insertions(+), 25 deletions(-) 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 2562a00ef..ecd1b9345 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 @@ -69,7 +69,6 @@ public class MQTTAdapterListener implements MqttCallback, Runnable { private InputEventAdapterListener eventAdapterListener = null; - public MQTTAdapterListener(MQTTBrokerConnectionConfiguration mqttBrokerConnectionConfiguration, String topic, String mqttClientId, InputEventAdapterListener inputEventAdapterListener, int tenantId) { @@ -173,22 +172,26 @@ public class MQTTAdapterListener implements MqttCallback, Runnable { StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON); postMethod.setEntity(requestEntity); HttpResponse httpResponse = httpClient.execute(postMethod); - String response = MQTTUtil.getResponseString(httpResponse); - try { - JSONParser jsonParser = new JSONParser(); - JSONObject jsonPayload = (JSONObject) jsonParser.parse(response); - String clientId = (String) jsonPayload.get(MQTTEventAdapterConstants.CLIENT_ID); - String clientSecret = (String) jsonPayload.get(MQTTEventAdapterConstants.CLIENT_SECRET); - JWTClientManagerService jwtClientManagerService = MQTTUtil.getJWTClientManagerService(); - AccessTokenInfo accessTokenInfo = jwtClientManagerService.getJWTClient().getAccessToken( - clientId, clientSecret, username, scopes); - connectionOptions.setUserName(accessTokenInfo.getAccessToken()); - } catch (ParseException e) { - String msg = "error occurred while parsing client credential payload"; - log.error(msg, e); - } catch (JWTClientException e) { - String msg = "error occurred while parsing the response from JWT Client"; - log.error(msg, e); + if (httpResponse != null) { + String response = MQTTUtil.getResponseString(httpResponse); + try { + if (response != null) { + JSONParser jsonParser = new JSONParser(); + JSONObject jsonPayload = (JSONObject) jsonParser.parse(response); + String clientId = (String) jsonPayload.get(MQTTEventAdapterConstants.CLIENT_ID); + String clientSecret = (String) jsonPayload.get(MQTTEventAdapterConstants.CLIENT_SECRET); + JWTClientManagerService jwtClientManagerService = MQTTUtil.getJWTClientManagerService(); + AccessTokenInfo accessTokenInfo = jwtClientManagerService.getJWTClient().getAccessToken( + clientId, clientSecret, username, scopes); + connectionOptions.setUserName(accessTokenInfo.getAccessToken()); + } + } catch (ParseException e) { + String msg = "error occurred while parsing client credential payload"; + log.error(msg, e); + } catch (JWTClientException e) { + String msg = "error occurred while parsing the response from JWT Client"; + log.error(msg, e); + } } } catch (MalformedURLException e) { log.error("Invalid dcrUrl : " + dcrUrlString); @@ -263,10 +266,10 @@ public class MQTTAdapterListener implements MqttCallback, Runnable { @Override public void run() { - int connectionDuration = MQTTEventAdapterConstants.initialReconnectDuration; + int connectionDuration = MQTTEventAdapterConstants.INITIAL_RECONNECTION_DURATION; while (!connectionSucceeded) { try { - connectionDuration = connectionDuration * MQTTEventAdapterConstants.reconnectionProgressionFactor; + connectionDuration = connectionDuration * MQTTEventAdapterConstants.RECONNECTION_PROGRESS_FACTOR; Thread.sleep(connectionDuration); startListener(); connectionSucceeded = true; 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/MQTTEventAdapterConstants.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/MQTTEventAdapterConstants.java index 88ac43171..1ac0ee949 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/MQTTEventAdapterConstants.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/MQTTEventAdapterConstants.java @@ -47,8 +47,8 @@ public class MQTTEventAdapterConstants { public static final String ADAPTER_CONF_KEEP_ALIVE = "keepAlive"; public static final int ADAPTER_CONF_DEFAULT_KEEP_ALIVE = 60000; - public static int initialReconnectDuration = 2000; - public static final int reconnectionProgressionFactor = 2; + public static final int INITIAL_RECONNECTION_DURATION = 4000; + public static final int RECONNECTION_PROGRESS_FACTOR = 2; public static final String EMPTY_STRING = ""; public static final String GRANT_TYPE = "urn:ietf:params:oauth:grant-type:jwt-bearer refresh_token"; 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 b31ac945c..168b9e6a8 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 @@ -203,9 +203,9 @@ public class XMPPAdapterListener implements Runnable { @Override public void run() { while (!connectionSucceeded) { - int connectionDuration = XMPPEventAdapterConstants.initialReconnectDuration; + int connectionDuration = XMPPEventAdapterConstants.INITIAL_RECONNECTION_DURATION; try { - connectionDuration = connectionDuration * XMPPEventAdapterConstants.reconnectionProgressionFactor; + connectionDuration = connectionDuration * XMPPEventAdapterConstants.RECONNECTION_PROGRESS_FACTOR; Thread.sleep(connectionDuration); startListener(); connectionSucceeded = true; 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/XMPPEventAdapterConstants.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/XMPPEventAdapterConstants.java index d62f78ab2..d823d8ba1 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/XMPPEventAdapterConstants.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/XMPPEventAdapterConstants.java @@ -49,8 +49,8 @@ public class XMPPEventAdapterConstants { public static final int DEFAULT_XMPP_PORT = 5222; public static final int DEFAULT_TIMEOUT_INTERVAL = 5000; - public static int initialReconnectDuration = 2000; - public static final int reconnectionProgressionFactor = 2; + public static int INITIAL_RECONNECTION_DURATION = 4000; + public static final int RECONNECTION_PROGRESS_FACTOR = 2; public static final String DEFAULT = "default";