From d1ba6935f40fcfa70d47722f8488043cc033c08a Mon Sep 17 00:00:00 2001 From: Andrii Landiak Date: Thu, 5 Oct 2023 14:17:42 +0300 Subject: [PATCH] Rewrite NotificationCenter with new broadcast system --- .../service/notification/DefaultNotificationCenter.java | 6 +++--- .../notification/DefaultNotificationRequestService.java | 9 +++++++-- .../dao/notification/DefaultNotificationRuleService.java | 8 +++++++- .../thingsboard/server/dao/tenant/TenantServiceImpl.java | 4 ++-- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationCenter.java b/application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationCenter.java index f6c77691da..204ff89527 100644 --- a/application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationCenter.java +++ b/application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationCenter.java @@ -51,7 +51,6 @@ import org.thingsboard.server.common.data.notification.template.DeliveryMethodNo import org.thingsboard.server.common.data.notification.template.NotificationTemplate; import org.thingsboard.server.common.data.notification.template.WebDeliveryMethodNotificationTemplate; import org.thingsboard.server.common.data.page.PageDataIterable; -import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent; import org.thingsboard.server.common.msg.queue.ServiceType; import org.thingsboard.server.common.msg.queue.TbCallback; import org.thingsboard.server.common.msg.queue.TopicPartitionInfo; @@ -391,7 +390,7 @@ public class DefaultNotificationCenter extends AbstractSubscriptionService imple public void deleteNotificationRequest(TenantId tenantId, NotificationRequestId notificationRequestId) { log.debug("Deleting notification request {}", notificationRequestId); NotificationRequest notificationRequest = notificationRequestService.findNotificationRequestById(tenantId, notificationRequestId); - notificationRequestService.deleteNotificationRequest(tenantId, notificationRequestId); + notificationRequestService.deleteNotificationRequest(tenantId, notificationRequest); if (notificationRequest.isSent()) { // TODO: no need to send request update for other than PLATFORM_USERS target type @@ -401,7 +400,8 @@ public class DefaultNotificationCenter extends AbstractSubscriptionService imple .build()); } else if (notificationRequest.isScheduled()) { // TODO: just forward to scheduler service - clusterService.broadcastEntityStateChangeEvent(tenantId, notificationRequestId, ComponentLifecycleEvent.DELETED); + // handling in EntityStateSourcingListener.class + // clusterService.broadcastEntityStateChangeEvent(tenantId, notificationRequestId, ComponentLifecycleEvent.DELETED); } } diff --git a/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationRequestService.java b/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationRequestService.java index e951a75c7c..7f7e7e4f4c 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationRequestService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationRequestService.java @@ -17,6 +17,7 @@ package org.thingsboard.server.dao.notification; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Service; import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.id.EntityId; @@ -31,6 +32,7 @@ import org.thingsboard.server.common.data.notification.NotificationRequestStatus import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.dao.entity.EntityDaoService; +import org.thingsboard.server.dao.eventsourcing.DeleteEntityEvent; import org.thingsboard.server.dao.service.DataValidator; import java.util.List; @@ -43,6 +45,8 @@ public class DefaultNotificationRequestService implements NotificationRequestSer private final NotificationRequestDao notificationRequestDao; + private final ApplicationEventPublisher eventPublisher; + private final NotificationRequestValidator notificationRequestValidator = new NotificationRequestValidator(); @Override @@ -83,8 +87,9 @@ public class DefaultNotificationRequestService implements NotificationRequestSer // ON DELETE CASCADE is used: notifications for request are deleted as well @Override - public void deleteNotificationRequest(TenantId tenantId, NotificationRequestId requestId) { - notificationRequestDao.removeById(tenantId, requestId.getId()); + public void deleteNotificationRequest(TenantId tenantId, NotificationRequest request) { + notificationRequestDao.removeById(tenantId, request.getUuidId()); + eventPublisher.publishEvent(DeleteEntityEvent.builder().tenantId(tenantId).entity(request).entityId(request.getId()).build()); } @Override diff --git a/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationRuleService.java b/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationRuleService.java index 41cea42875..90fcb3ccc4 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationRuleService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationRuleService.java @@ -29,6 +29,8 @@ import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.dao.entity.AbstractEntityService; import org.thingsboard.server.dao.entity.EntityDaoService; +import org.thingsboard.server.dao.eventsourcing.DeleteEntityEvent; +import org.thingsboard.server.dao.eventsourcing.SaveEntityEvent; import java.util.List; import java.util.Map; @@ -49,7 +51,10 @@ public class DefaultNotificationRuleService extends AbstractEntityService implem } } try { - return notificationRuleDao.saveAndFlush(tenantId, notificationRule); + NotificationRule savedRule = notificationRuleDao.saveAndFlush(tenantId, notificationRule); + eventPublisher.publishEvent(SaveEntityEvent.builder().tenantId(tenantId).entityId(savedRule.getId()) + .added(notificationRule.getId() == null).build()); + return savedRule; } catch (Exception e) { checkConstraintViolation(e, Map.of( "uq_notification_rule_name", "Notification rule with such name already exists" @@ -86,6 +91,7 @@ public class DefaultNotificationRuleService extends AbstractEntityService implem @Override public void deleteNotificationRuleById(TenantId tenantId, NotificationRuleId id) { notificationRuleDao.removeById(tenantId, id.getId()); + eventPublisher.publishEvent(DeleteEntityEvent.builder().tenantId(tenantId).entityId(id).build()); } @Override diff --git a/dao/src/main/java/org/thingsboard/server/dao/tenant/TenantServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/tenant/TenantServiceImpl.java index ac85bfaa72..1aa90f7022 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/tenant/TenantServiceImpl.java +++ b/dao/src/main/java/org/thingsboard/server/dao/tenant/TenantServiceImpl.java @@ -197,8 +197,6 @@ public class TenantServiceImpl extends AbstractCachedEntityService