Rewrite NotificationCenter with new broadcast system

This commit is contained in:
Andrii Landiak 2023-10-05 14:17:42 +03:00
parent 92b09b8514
commit d1ba6935f4
4 changed files with 19 additions and 8 deletions

View File

@ -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.NotificationTemplate;
import org.thingsboard.server.common.data.notification.template.WebDeliveryMethodNotificationTemplate; import org.thingsboard.server.common.data.notification.template.WebDeliveryMethodNotificationTemplate;
import org.thingsboard.server.common.data.page.PageDataIterable; 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.ServiceType;
import org.thingsboard.server.common.msg.queue.TbCallback; import org.thingsboard.server.common.msg.queue.TbCallback;
import org.thingsboard.server.common.msg.queue.TopicPartitionInfo; 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) { public void deleteNotificationRequest(TenantId tenantId, NotificationRequestId notificationRequestId) {
log.debug("Deleting notification request {}", notificationRequestId); log.debug("Deleting notification request {}", notificationRequestId);
NotificationRequest notificationRequest = notificationRequestService.findNotificationRequestById(tenantId, notificationRequestId); NotificationRequest notificationRequest = notificationRequestService.findNotificationRequestById(tenantId, notificationRequestId);
notificationRequestService.deleteNotificationRequest(tenantId, notificationRequestId); notificationRequestService.deleteNotificationRequest(tenantId, notificationRequest);
if (notificationRequest.isSent()) { if (notificationRequest.isSent()) {
// TODO: no need to send request update for other than PLATFORM_USERS target type // 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()); .build());
} else if (notificationRequest.isScheduled()) { } else if (notificationRequest.isScheduled()) {
// TODO: just forward to scheduler service // TODO: just forward to scheduler service
clusterService.broadcastEntityStateChangeEvent(tenantId, notificationRequestId, ComponentLifecycleEvent.DELETED); // handling in EntityStateSourcingListener.class
// clusterService.broadcastEntityStateChangeEvent(tenantId, notificationRequestId, ComponentLifecycleEvent.DELETED);
} }
} }

View File

@ -17,6 +17,7 @@ package org.thingsboard.server.dao.notification;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.id.EntityId; 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.PageData;
import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.dao.entity.EntityDaoService; import org.thingsboard.server.dao.entity.EntityDaoService;
import org.thingsboard.server.dao.eventsourcing.DeleteEntityEvent;
import org.thingsboard.server.dao.service.DataValidator; import org.thingsboard.server.dao.service.DataValidator;
import java.util.List; import java.util.List;
@ -43,6 +45,8 @@ public class DefaultNotificationRequestService implements NotificationRequestSer
private final NotificationRequestDao notificationRequestDao; private final NotificationRequestDao notificationRequestDao;
private final ApplicationEventPublisher eventPublisher;
private final NotificationRequestValidator notificationRequestValidator = new NotificationRequestValidator(); private final NotificationRequestValidator notificationRequestValidator = new NotificationRequestValidator();
@Override @Override
@ -83,8 +87,9 @@ public class DefaultNotificationRequestService implements NotificationRequestSer
// ON DELETE CASCADE is used: notifications for request are deleted as well // ON DELETE CASCADE is used: notifications for request are deleted as well
@Override @Override
public void deleteNotificationRequest(TenantId tenantId, NotificationRequestId requestId) { public void deleteNotificationRequest(TenantId tenantId, NotificationRequest request) {
notificationRequestDao.removeById(tenantId, requestId.getId()); notificationRequestDao.removeById(tenantId, request.getUuidId());
eventPublisher.publishEvent(DeleteEntityEvent.builder().tenantId(tenantId).entity(request).entityId(request.getId()).build());
} }
@Override @Override

View File

@ -29,6 +29,8 @@ import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.dao.entity.AbstractEntityService; import org.thingsboard.server.dao.entity.AbstractEntityService;
import org.thingsboard.server.dao.entity.EntityDaoService; 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.List;
import java.util.Map; import java.util.Map;
@ -49,7 +51,10 @@ public class DefaultNotificationRuleService extends AbstractEntityService implem
} }
} }
try { 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) { } catch (Exception e) {
checkConstraintViolation(e, Map.of( checkConstraintViolation(e, Map.of(
"uq_notification_rule_name", "Notification rule with such name already exists" "uq_notification_rule_name", "Notification rule with such name already exists"
@ -86,6 +91,7 @@ public class DefaultNotificationRuleService extends AbstractEntityService implem
@Override @Override
public void deleteNotificationRuleById(TenantId tenantId, NotificationRuleId id) { public void deleteNotificationRuleById(TenantId tenantId, NotificationRuleId id) {
notificationRuleDao.removeById(tenantId, id.getId()); notificationRuleDao.removeById(tenantId, id.getId());
eventPublisher.publishEvent(DeleteEntityEvent.builder().tenantId(tenantId).entityId(id).build());
} }
@Override @Override

View File

@ -197,8 +197,6 @@ public class TenantServiceImpl extends AbstractCachedEntityService<TenantId, Ten
boolean create = tenant.getId() == null; boolean create = tenant.getId() == null;
Tenant savedTenant = tenantDao.save(tenant.getId(), tenant); Tenant savedTenant = tenantDao.save(tenant.getId(), tenant);
publishEvictEvent(new TenantEvictEvent(savedTenant.getId(), create)); publishEvictEvent(new TenantEvictEvent(savedTenant.getId(), create));
eventPublisher.publishEvent(SaveEntityEvent.builder().tenantId(TenantId.SYS_TENANT_ID)
.entityId(savedTenant.getId()).entity(savedTenant).added(create).build());
if (tenant.getId() == null) { if (tenant.getId() == null) {
deviceProfileService.createDefaultDeviceProfile(savedTenant.getId()); deviceProfileService.createDefaultDeviceProfile(savedTenant.getId());
assetProfileService.createDefaultAssetProfile(savedTenant.getId()); assetProfileService.createDefaultAssetProfile(savedTenant.getId());
@ -209,6 +207,8 @@ public class TenantServiceImpl extends AbstractCachedEntityService<TenantId, Ten
log.error("Failed to create default notification configs for tenant {}", savedTenant.getId(), e); log.error("Failed to create default notification configs for tenant {}", savedTenant.getId(), e);
} }
} }
eventPublisher.publishEvent(SaveEntityEvent.builder().tenantId(TenantId.SYS_TENANT_ID)
.entityId(savedTenant.getId()).entity(savedTenant).added(create).build());
return savedTenant; return savedTenant;
} }