forked from community/device-mgt-core
Merge pull request #725 from warunalakshitha/master
Add Scheduler Task for Sending Push Notificationrevert-70aa11f8
commit
6798dea68a
@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.core.config.push.notification;
|
||||
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlElementWrapper;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This class is for Push notification related Configurations
|
||||
*/
|
||||
@XmlRootElement(name = "PushNotificationConfiguration")
|
||||
public class PushNotificationConfiguration {
|
||||
|
||||
private int schedulerBatchSize;
|
||||
private int schedulerBatchDelayMills;
|
||||
private int schedulerTaskInitialDelay;
|
||||
private boolean schedulerTaskEnabled;
|
||||
private List<String> pushNotificationProviders;
|
||||
|
||||
@XmlElement(name = "SchedulerBatchSize", required = true)
|
||||
public int getSchedulerBatchSize() {
|
||||
return schedulerBatchSize;
|
||||
}
|
||||
|
||||
public void setSchedulerBatchSize(int schedulerBatchSize) {
|
||||
this.schedulerBatchSize = schedulerBatchSize;
|
||||
}
|
||||
|
||||
@XmlElement(name = "SchedulerBatchDelayMills", required = true)
|
||||
public int getSchedulerBatchDelayMills() {
|
||||
return schedulerBatchDelayMills;
|
||||
}
|
||||
|
||||
public void setSchedulerBatchDelayMills(int schedulerBatchDelayMills) {
|
||||
this.schedulerBatchDelayMills = schedulerBatchDelayMills;
|
||||
}
|
||||
|
||||
@XmlElement(name = "SchedulerTaskInitialDelay", required = true)
|
||||
public int getSchedulerTaskInitialDelay() {
|
||||
return schedulerTaskInitialDelay;
|
||||
}
|
||||
|
||||
public void setSchedulerTaskInitialDelay(int schedulerTaskInitialDelay) {
|
||||
this.schedulerTaskInitialDelay = schedulerTaskInitialDelay;
|
||||
}
|
||||
|
||||
@XmlElement(name = "SchedulerTaskEnabled", required = true)
|
||||
public boolean isSchedulerTaskEnabled() {
|
||||
return schedulerTaskEnabled;
|
||||
}
|
||||
|
||||
public void setSchedulerTaskEnabled(boolean schedulerTaskEnabled) {
|
||||
this.schedulerTaskEnabled = schedulerTaskEnabled;
|
||||
}
|
||||
|
||||
@XmlElementWrapper(name = "PushNotificationProviders", required = true)
|
||||
@XmlElement(name = "Provider", required = true)
|
||||
public List<String> getPushNotificationProviders() {
|
||||
return pushNotificationProviders;
|
||||
}
|
||||
|
||||
public void setPushNotificationProviders(List<String> pushNotificationProviders) {
|
||||
this.pushNotificationProviders = pushNotificationProviders;
|
||||
}
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
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
|
||||
*/
|
||||
public class OperationMapping {
|
||||
|
||||
private DeviceIdentifier deviceIdentifier;
|
||||
private int operationId;
|
||||
private int tenantId;
|
||||
private Operation.Status status;
|
||||
private Operation.PushNotificationStatus pushNotificationStatus;
|
||||
|
||||
public int getOperationId() {
|
||||
return operationId;
|
||||
}
|
||||
|
||||
public void setOperationId(int operationId) {
|
||||
this.operationId = operationId;
|
||||
}
|
||||
|
||||
public int getTenantId() {
|
||||
return tenantId;
|
||||
}
|
||||
|
||||
public void setTenantId(int tenantId) {
|
||||
this.tenantId = tenantId;
|
||||
}
|
||||
|
||||
public DeviceIdentifier getDeviceIdentifier() {
|
||||
return deviceIdentifier;
|
||||
}
|
||||
|
||||
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.PushNotificationStatus getPushNotificationStatus() {
|
||||
return pushNotificationStatus;
|
||||
}
|
||||
|
||||
public void setPushNotificationStatus(Operation.PushNotificationStatus pushNotificationStatus) {
|
||||
this.pushNotificationStatus = pushNotificationStatus;
|
||||
}
|
||||
}
|
@ -0,0 +1,127 @@
|
||||
/*
|
||||
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.core.push.notification.mgt.task;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
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.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.config.DeviceConfigurationManager;
|
||||
import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation;
|
||||
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
|
||||
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMapping;
|
||||
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO;
|
||||
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.OperationMappingDAO;
|
||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* ${{@link PushNotificationSchedulerTask}} is for sending push notifications for given device batch.
|
||||
*/
|
||||
public class PushNotificationSchedulerTask implements Runnable {
|
||||
|
||||
private static Log log = LogFactory.getLog(PushNotificationSchedulerTask.class);
|
||||
private final OperationDAO operationDAO = OperationManagementDAOFactory.getOperationDAO();
|
||||
private final OperationMappingDAO operationMappingDAO = OperationManagementDAOFactory.getOperationMappingDAO();
|
||||
private final DeviceManagementProviderService provider = DeviceManagementDataHolder.getInstance()
|
||||
.getDeviceManagementProvider();
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Map<Integer, List<OperationMapping>> operationMappingsTenantMap = new HashMap<>();
|
||||
List<OperationMapping> operationsCompletedList = new LinkedList<>();
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Push notification job started");
|
||||
}
|
||||
try {
|
||||
//Get next available operation list per device batch
|
||||
OperationManagementDAOFactory.openConnection();
|
||||
operationMappingsTenantMap = operationDAO.getOperationMappingsByStatus(Operation.Status
|
||||
.PENDING, Operation.PushNotificationStatus.SCHEDULED, DeviceConfigurationManager.getInstance()
|
||||
.getDeviceManagementConfig().getPushNotificationConfiguration().getSchedulerBatchSize());
|
||||
} catch (OperationManagementDAOException e) {
|
||||
log.error("Unable to retrieve scheduled pending operations for task.", e);
|
||||
} finally {
|
||||
OperationManagementDAOFactory.closeConnection();
|
||||
}
|
||||
// Sending push notification to each device
|
||||
for (List<OperationMapping> operationMappings : operationMappingsTenantMap.values()) {
|
||||
for (OperationMapping operationMapping : operationMappings) {
|
||||
try {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Sending push notification for operationId :" + operationMapping.getOperationId() +
|
||||
"to deviceId : " + operationMapping.getDeviceIdentifier().getId());
|
||||
}
|
||||
// Set tenant id and domain
|
||||
PrivilegedCarbonContext.startTenantFlow();
|
||||
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(operationMapping.getTenantId(), true);
|
||||
// Get notification strategy for given device type
|
||||
NotificationStrategy notificationStrategy = provider.getNotificationStrategyByDeviceType
|
||||
(operationMapping.getDeviceIdentifier().getType());
|
||||
// Send the push notification on given strategy
|
||||
notificationStrategy.execute(new NotificationContext(operationMapping.getDeviceIdentifier(),
|
||||
provider.getOperation(operationMapping.getDeviceIdentifier().getType(), operationMapping
|
||||
.getOperationId())));
|
||||
operationMapping.setPushNotificationStatus(Operation.PushNotificationStatus.COMPLETED);
|
||||
operationsCompletedList.add(operationMapping);
|
||||
} catch (DeviceManagementException e) {
|
||||
log.error("Error occurred while getting notification strategy for operation mapping " +
|
||||
operationMapping.getDeviceIdentifier().getType(), e);
|
||||
} catch (OperationManagementException e) {
|
||||
log.error("Unable to get the operation for operation " + operationMapping.getOperationId(), e);
|
||||
} catch (PushNotificationExecutionFailedException e) {
|
||||
log.error("Error occurred while sending push notification to operation: " + operationMapping
|
||||
.getOperationId(), e);
|
||||
} finally {
|
||||
PrivilegedCarbonContext.endTenantFlow();
|
||||
}
|
||||
}
|
||||
}
|
||||
// Update push notification status to competed for operations which already sent
|
||||
if (operationsCompletedList.size() > 0) {
|
||||
try {
|
||||
OperationManagementDAOFactory.beginTransaction();
|
||||
operationMappingDAO.updateOperationMapping(operationsCompletedList);
|
||||
OperationManagementDAOFactory.commitTransaction();
|
||||
} catch (TransactionManagementException | OperationManagementDAOException e) {
|
||||
OperationManagementDAOFactory.rollbackTransaction();
|
||||
log.error("Error occurred while updating operation mappings for sent notifications ", e);
|
||||
} finally {
|
||||
OperationManagementDAOFactory.closeConnection();
|
||||
}
|
||||
}
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Push notification job running completed.");
|
||||
}
|
||||
} catch (Throwable cause) {
|
||||
log.error("PushNotificationSchedulerTask failed due to " + cause);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.wso2.carbon.policy.mgt.core.mgt.bean;
|
||||
|
||||
import org.wso2.carbon.device.mgt.common.policy.mgt.Policy;
|
||||
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This class stores list of updated policies and list of changed devices for Policy Manager
|
||||
*/
|
||||
public class UpdatedPolicyDeviceListBean {
|
||||
|
||||
private List<Policy> updatedPolicies;
|
||||
private List<Integer> updatedPolicyIds;
|
||||
private List<String> changedDeviceTypes;
|
||||
|
||||
public UpdatedPolicyDeviceListBean(List<Policy> updatedPolicies, List<Integer> updatedPolicyIds, List<String>
|
||||
deviceTypes) {
|
||||
this.updatedPolicies = updatedPolicies;
|
||||
this.updatedPolicyIds = updatedPolicyIds;
|
||||
this.changedDeviceTypes = deviceTypes;
|
||||
}
|
||||
|
||||
public List<Policy> getUpdatedPolicies() {
|
||||
return updatedPolicies;
|
||||
}
|
||||
|
||||
public void setUpdatedPolicies(List<Policy> updatedPolicies) {
|
||||
this.updatedPolicies = updatedPolicies;
|
||||
}
|
||||
|
||||
public List<Integer> getUpdatedPolicyIds() {
|
||||
return updatedPolicyIds;
|
||||
}
|
||||
|
||||
public void setUpdatedPolicyIds(List<Integer> updatedPolicyIds) {
|
||||
this.updatedPolicyIds = updatedPolicyIds;
|
||||
}
|
||||
|
||||
public List<String> getChangedDeviceTypes() {
|
||||
return changedDeviceTypes;
|
||||
}
|
||||
|
||||
public void setChangedDeviceTypes(List<String> changedDeviceTypes) {
|
||||
this.changedDeviceTypes = changedDeviceTypes;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue