From 880720a72f58317777a1b8061057600db7be402d Mon Sep 17 00:00:00 2001 From: Ace Date: Fri, 9 Nov 2018 21:51:42 +0530 Subject: [PATCH] Fixing issue with task scheduler --- .../carbon/device/mgt/core/task/Utils.java | 19 ++++++++++++++----- .../task/impl/DeviceDetailsRetrieverTask.java | 7 +++++-- .../core/task/impl/DeviceTaskManagerImpl.java | 10 +++------- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/Utils.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/Utils.java index f0997ce4b26..36b43b12c68 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/Utils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/Utils.java @@ -26,14 +26,23 @@ import java.util.Map; public class Utils { - public static Map getTenantedTaskOperationMap(Map> map) { + public static Map getTenantedTaskOperationMap(Map>> map, + String deviceType) { + Map taskMap = new HashMap<>(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); if (map.containsKey(tenantId)) { - return map.get(tenantId); + if (map.get(tenantId).containsKey(deviceType)) { + return map.get(tenantId).get(deviceType); + } else { + Map> existingTenantMap = map.get(tenantId); + existingTenantMap.put(deviceType, taskMap); + return taskMap; + } } else { - Map mp = new HashMap<>(); - map.put(tenantId, mp); - return mp; + HashMap> typeMap = new HashMap<>(); + typeMap.put(deviceType, taskMap); + map.put(tenantId, typeMap); + return taskMap; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java index fb91f62a52c..567964d808a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java @@ -39,6 +39,7 @@ public class DeviceDetailsRetrieverTask implements Task { private String deviceType; private boolean executeForTenants = false; private final String IS_CLOUD = "is.cloud"; + private DeviceManagementProviderService deviceManagementProviderService; @Override public void setProperties(Map map) { @@ -51,7 +52,7 @@ public class DeviceDetailsRetrieverTask implements Task { @Override public void execute() { - DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder.getInstance() + deviceManagementProviderService = DeviceManagementDataHolder.getInstance() .getDeviceManagementProvider(); OperationMonitoringTaskConfig operationMonitoringTaskConfig = deviceManagementProviderService .getDeviceMonitoringConfig(deviceType); @@ -96,7 +97,9 @@ public class DeviceDetailsRetrieverTask implements Task { operationMonitoringTaskConfig); //pass the configurations also from here, monitoring tasks try { - deviceTaskManager.addOperations(); + if (deviceManagementProviderService.isDeviceMonitoringEnabled(deviceType)) { + deviceTaskManager.addOperations(); + } } catch (DeviceMgtTaskException e) { log.error("Error occurred while trying to add the operations to " + "device to retrieve device details.", e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java index 41d291e74e7..21328891892 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java @@ -36,17 +36,13 @@ import org.wso2.carbon.device.mgt.core.task.DeviceTaskManager; import org.wso2.carbon.device.mgt.core.task.Utils; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; public class DeviceTaskManagerImpl implements DeviceTaskManager { private static Log log = LogFactory.getLog(DeviceTaskManagerImpl.class); private String deviceType; - private static Map> map = new HashMap<>(); + static volatile Map>> map = new HashMap<>(); private OperationMonitoringTaskConfig operationMonitoringTaskConfig; public DeviceTaskManagerImpl(String deviceType, @@ -126,7 +122,7 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager { List opNames = new ArrayList<>(); Long milliseconds = System.currentTimeMillis(); int frequency = this.getTaskFrequency(); - Map mp = Utils.getTenantedTaskOperationMap(map); + Map mp = Utils.getTenantedTaskOperationMap(map, deviceType); for (MonitoringOperation top : monitoringOperations) { if (!mp.containsKey(top.getTaskName())) {