From 76f8d963f7594cc1f5be0a976b311bf433d3df0f Mon Sep 17 00:00:00 2001 From: warunalakshitha Date: Thu, 27 Apr 2017 12:23:01 +0530 Subject: [PATCH] Improve push notifications for request with device list less than batch size --- .../operation/mgt/OperationManagerImpl.java | 17 +++++++++++++-- .../core}/operation/mgt/OperationMapping.java | 21 ++++++++++++++++++- .../core/operation/mgt/dao/OperationDAO.java | 2 +- .../mgt/dao/OperationMappingDAO.java | 4 ++-- .../mgt/dao/impl/GenericOperationDAOImpl.java | 2 +- .../mgt/dao/impl/OperationMappingDAOImpl.java | 8 +++---- .../operation/OracleOperationDAOImpl.java | 2 +- .../operation/SQLServerOperationDAOImpl.java | 2 +- .../mgt/task/PushNotificationJob.java | 5 +++-- 9 files changed, 48 insertions(+), 15 deletions(-) rename components/device-mgt/{org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common => org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core}/operation/mgt/OperationMapping.java (72%) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index aeb44e559f..aaf77bf2d8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -40,6 +40,7 @@ import org.wso2.carbon.device.mgt.common.push.notification.NotificationContext; import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationExecutionFailedException; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; +import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; @@ -146,7 +147,12 @@ public class OperationManagerImpl implements OperationManager { boolean isScheduledOperation = this.isTaskScheduledOperation(operation, deviceIds); boolean isNotRepeated = false; boolean isScheduled = false; - if (notificationStrategy != null) { + + // check whether device list is greater than batch size notification strategy has enable to send push + // notification using scheduler task + if (DeviceConfigurationManager.getInstance().getDeviceManagementConfig(). + getPushNotificationConfiguration().getSchedulerBatchSize() < authorizedDeviceList.size() && + notificationStrategy != null) { isScheduled = notificationStrategy.getConfig().isScheduled(); } @@ -178,8 +184,15 @@ public class OperationManagerImpl implements OperationManager { } else { operationMappingDAO.addOperationMapping(operationId, enrolmentId, isScheduled); } - if (notificationStrategy != null && !notificationStrategy.getConfig().isScheduled()) { + /* + If notification strategy has not enable to send push notification using scheduler task + we will send notification immediately + */ + if (notificationStrategy != null && !isScheduled) { try { + if (log.isDebugEnabled()) { + log.debug("Sending push notification to " + deviceId + " add operation thread."); + } notificationStrategy.execute(new NotificationContext(deviceId, operation)); operationMappingDAO.updateOperationMapping(operationId, enrolmentId, org.wso2.carbon .device.mgt.core.dto.operation.mgt.Operation.PushStatus.COMPLETED); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationMapping.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationMapping.java similarity index 72% rename from components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationMapping.java rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationMapping.java index f9feed6d8d..6775ebd92a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationMapping.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationMapping.java @@ -15,9 +15,10 @@ * specific language governing permissions and limitations * under the License. */ -package org.wso2.carbon.device.mgt.common.operation.mgt; +package org.wso2.carbon.device.mgt.core.operation.mgt; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; /** * Class for represent operation mapping @@ -27,6 +28,8 @@ public class OperationMapping { private DeviceIdentifier deviceIdentifier; private int operationId; private int tenantId; + private Operation.Status status; + private Operation.PushStatus pushStatus; public int getOperationId() { return operationId; @@ -51,4 +54,20 @@ public class OperationMapping { public void setDeviceIdentifier(DeviceIdentifier deviceIdentifier) { this.deviceIdentifier = deviceIdentifier; } + + public Operation.Status getStatus() { + return status; + } + + public void setStatus(Operation.Status status) { + this.status = status; + } + + public Operation.PushStatus getPushStatus() { + return pushStatus; + } + + public void setPushStatus(Operation.PushStatus pushStatus) { + this.pushStatus = pushStatus; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java index 63100f702c..d22684f6a3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java @@ -20,7 +20,7 @@ package org.wso2.carbon.device.mgt.core.operation.mgt.dao; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; -import org.wso2.carbon.device.mgt.common.operation.mgt.OperationMapping; +import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMapping; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationMappingDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationMappingDAO.java index 0a2c45841d..003aad11f5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationMappingDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationMappingDAO.java @@ -18,7 +18,7 @@ */ package org.wso2.carbon.device.mgt.core.operation.mgt.dao; -import org.wso2.carbon.device.mgt.common.operation.mgt.OperationMapping; +import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMapping; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import java.util.List; @@ -31,7 +31,7 @@ public interface OperationMappingDAO { void updateOperationMapping(int operationId, Integer deviceId, Operation.PushStatus pushStatus) throws OperationManagementDAOException; - void updateOperationMapping(List operationMappingList, Operation.PushStatus pushStatus) throws + void updateOperationMapping(List operationMappingList) throws OperationManagementDAOException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java index a16aa42d5f..1e145c14f4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java @@ -24,7 +24,7 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; -import org.wso2.carbon.device.mgt.common.operation.mgt.OperationMapping; +import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMapping; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java index 473f1d76b3..f9b8b5e8a8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java @@ -18,8 +18,8 @@ */ package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl; -import org.wso2.carbon.device.mgt.common.operation.mgt.OperationMapping; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMapping; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOUtil; @@ -98,7 +98,7 @@ public class OperationMappingDAOImpl implements OperationMappingDAO { } @Override - public void updateOperationMapping(List operationMappingList, Operation.PushStatus pushStatus) throws + public void updateOperationMapping(List operationMappingList) throws OperationManagementDAOException { PreparedStatement stmt = null; try { @@ -108,7 +108,7 @@ public class OperationMappingDAOImpl implements OperationMappingDAO { stmt = conn.prepareStatement(sql); if (conn.getMetaData().supportsBatchUpdates()) { for (OperationMapping operationMapping : operationMappingList) { - stmt.setString(1, pushStatus.toString()); + stmt.setString(1, operationMapping.getPushStatus().toString()); stmt.setInt(2, Integer.parseInt(operationMapping.getDeviceIdentifier().getId())); stmt.setInt(3, operationMapping.getOperationId()); stmt.addBatch(); @@ -116,7 +116,7 @@ public class OperationMappingDAOImpl implements OperationMappingDAO { stmt.executeBatch(); } else { for (OperationMapping operationMapping : operationMappingList) { - stmt.setString(1, pushStatus.toString()); + stmt.setString(1, operationMapping.getPushStatus().toString()); stmt.setInt(2, Integer.parseInt(operationMapping.getDeviceIdentifier().getId())); stmt.setInt(3, operationMapping.getOperationId()); stmt.executeUpdate(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/OracleOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/OracleOperationDAOImpl.java index 7a4f3a6af8..c78afd44a8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/OracleOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/OracleOperationDAOImpl.java @@ -23,7 +23,7 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; -import org.wso2.carbon.device.mgt.common.operation.mgt.OperationMapping; +import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMapping; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/SQLServerOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/SQLServerOperationDAOImpl.java index b7a5f3cd6b..c553fee8d7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/SQLServerOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/SQLServerOperationDAOImpl.java @@ -23,7 +23,7 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; -import org.wso2.carbon.device.mgt.common.operation.mgt.OperationMapping; +import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMapping; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/task/PushNotificationJob.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/task/PushNotificationJob.java index f9dc91d9dd..c34ce46663 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/task/PushNotificationJob.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/task/PushNotificationJob.java @@ -23,7 +23,7 @@ import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.TransactionManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; -import org.wso2.carbon.device.mgt.common.operation.mgt.OperationMapping; +import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMapping; import org.wso2.carbon.device.mgt.common.push.notification.NotificationContext; import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationExecutionFailedException; @@ -84,6 +84,7 @@ public class PushNotificationJob implements Runnable { notificationStrategy.execute(new NotificationContext(operationMapping.getDeviceIdentifier(), provider.getOperation(operationMapping.getDeviceIdentifier().getType(), operationMapping .getOperationId()))); + operationMapping.setPushStatus(Operation.PushStatus.COMPLETED); operationsCompletedList.add(operationMapping); } catch (DeviceManagementException e) { log.error("Error occurred while getting notification strategy for operation mapping " + @@ -109,7 +110,7 @@ public class PushNotificationJob implements Runnable { // Update push notification status to competed for operations which already sent try { OperationManagementDAOFactory.beginTransaction(); - operationMappingDAO.updateOperationMapping(operationsCompletedList, Operation.PushStatus.COMPLETED); + operationMappingDAO.updateOperationMapping(operationsCompletedList); OperationManagementDAOFactory.commitTransaction(); } catch (TransactionManagementException | OperationManagementDAOException e) { OperationManagementDAOFactory.rollbackTransaction();