Search in sources :

Example 1 with TbCallback

use of org.thingsboard.server.common.msg.queue.TbCallback in project thingsboard by thingsboard.

the class DefaultTbCoreConsumerService method launchUsageStatsConsumer.

private void launchUsageStatsConsumer() {
    usageStatsExecutor.submit(() -> {
        while (!stopped) {
            try {
                List<TbProtoQueueMsg<ToUsageStatsServiceMsg>> msgs = usageStatsConsumer.poll(getNotificationPollDuration());
                if (msgs.isEmpty()) {
                    continue;
                }
                ConcurrentMap<UUID, TbProtoQueueMsg<ToUsageStatsServiceMsg>> pendingMap = msgs.stream().collect(Collectors.toConcurrentMap(s -> UUID.randomUUID(), Function.identity()));
                CountDownLatch processingTimeoutLatch = new CountDownLatch(1);
                TbPackProcessingContext<TbProtoQueueMsg<ToUsageStatsServiceMsg>> ctx = new TbPackProcessingContext<>(processingTimeoutLatch, pendingMap, new ConcurrentHashMap<>());
                pendingMap.forEach((id, msg) -> {
                    log.trace("[{}] Creating usage stats callback for message: {}", id, msg.getValue());
                    TbCallback callback = new TbPackCallback<>(id, ctx);
                    try {
                        handleUsageStats(msg, callback);
                    } catch (Throwable e) {
                        log.warn("[{}] Failed to process usage stats: {}", id, msg, e);
                        callback.onFailure(e);
                    }
                });
                if (!processingTimeoutLatch.await(getNotificationPackProcessingTimeout(), TimeUnit.MILLISECONDS)) {
                    ctx.getAckMap().forEach((id, msg) -> log.warn("[{}] Timeout to process usage stats: {}", id, msg.getValue()));
                    ctx.getFailedMap().forEach((id, msg) -> log.warn("[{}] Failed to process usage stats: {}", id, msg.getValue()));
                }
                usageStatsConsumer.commit();
            } catch (Exception e) {
                if (!stopped) {
                    log.warn("Failed to obtain usage stats from queue.", e);
                    try {
                        Thread.sleep(getNotificationPollDuration());
                    } catch (InterruptedException e2) {
                        log.trace("Failed to wait until the server has capacity to handle new usage stats", e2);
                    }
                }
            }
        }
        log.info("TB Usage Stats Consumer stopped.");
    });
}
Also used : TbCoreDeviceRpcService(org.thingsboard.server.service.rpc.TbCoreDeviceRpcService) DeviceStateServiceMsgProto(org.thingsboard.server.gen.transport.TransportProtos.DeviceStateServiceMsgProto) ApplicationReadyEvent(org.springframework.boot.context.event.ApplicationReadyEvent) ToDeviceRpcRequestActorMsg(org.thingsboard.server.service.rpc.ToDeviceRpcRequestActorMsg) ToCoreNotificationMsg(org.thingsboard.server.gen.transport.TransportProtos.ToCoreNotificationMsg) StatsFactory(org.thingsboard.server.common.stats.StatsFactory) TenantId(org.thingsboard.server.common.data.id.TenantId) PreDestroy(javax.annotation.PreDestroy) Future(java.util.concurrent.Future) TbAlarmDeleteProto(org.thingsboard.server.gen.transport.TransportProtos.TbAlarmDeleteProto) ActorSystemContext(org.thingsboard.server.actors.ActorSystemContext) EdgeNotificationService(org.thingsboard.server.service.edge.EdgeNotificationService) SubscriptionMgrMsgProto(org.thingsboard.server.gen.transport.TransportProtos.SubscriptionMgrMsgProto) AbstractConsumerService(org.thingsboard.server.service.queue.processing.AbstractConsumerService) LocalSubscriptionServiceMsgProto(org.thingsboard.server.gen.transport.TransportProtos.LocalSubscriptionServiceMsgProto) FromDeviceRPCResponseProto(org.thingsboard.server.gen.transport.TransportProtos.FromDeviceRPCResponseProto) TbSubscriptionCloseProto(org.thingsboard.server.gen.transport.TransportProtos.TbSubscriptionCloseProto) TransportToDeviceActorMsg(org.thingsboard.server.gen.transport.TransportProtos.TransportToDeviceActorMsg) TbAlarmUpdateProto(org.thingsboard.server.gen.transport.TransportProtos.TbAlarmUpdateProto) ToOtaPackageStateServiceMsg(org.thingsboard.server.gen.transport.TransportProtos.ToOtaPackageStateServiceMsg) DeviceStateService(org.thingsboard.server.service.state.DeviceStateService) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) EventListener(org.springframework.context.event.EventListener) ThingsBoardThreadFactory(org.thingsboard.common.util.ThingsBoardThreadFactory) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) TbTimeSeriesUpdateProto(org.thingsboard.server.gen.transport.TransportProtos.TbTimeSeriesUpdateProto) Executors(java.util.concurrent.Executors) Alarm(org.thingsboard.server.common.data.alarm.Alarm) CountDownLatch(java.util.concurrent.CountDownLatch) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) TbActorMsg(org.thingsboard.server.common.msg.TbActorMsg) TbCoreQueueFactory(org.thingsboard.server.queue.provider.TbCoreQueueFactory) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) TbTenantProfileCache(org.thingsboard.server.dao.tenant.TbTenantProfileCache) TbApiUsageStateService(org.thingsboard.server.service.apiusage.TbApiUsageStateService) TbQueueConsumer(org.thingsboard.server.queue.TbQueueConsumer) TransportToDeviceActorMsgWrapper(org.thingsboard.server.service.transport.msg.TransportToDeviceActorMsgWrapper) Setter(lombok.Setter) Getter(lombok.Getter) JacksonUtil(org.thingsboard.common.util.JacksonUtil) Scheduled(org.springframework.scheduling.annotation.Scheduled) Function(java.util.function.Function) TbAttributeDeleteProto(org.thingsboard.server.gen.transport.TransportProtos.TbAttributeDeleteProto) DataDecodingEncodingService(org.thingsboard.server.common.transport.util.DataDecodingEncodingService) ConcurrentMap(java.util.concurrent.ConcurrentMap) Value(org.springframework.beans.factory.annotation.Value) MsgType(org.thingsboard.server.common.msg.MsgType) ToUsageStatsServiceMsg(org.thingsboard.server.gen.transport.TransportProtos.ToUsageStatsServiceMsg) TbCoreComponent(org.thingsboard.server.queue.util.TbCoreComponent) ServiceType(org.thingsboard.server.common.msg.queue.ServiceType) Service(org.springframework.stereotype.Service) ToCoreMsg(org.thingsboard.server.gen.transport.TransportProtos.ToCoreMsg) TbAttributeUpdateProto(org.thingsboard.server.gen.transport.TransportProtos.TbAttributeUpdateProto) TbCallback(org.thingsboard.server.common.msg.queue.TbCallback) ExecutorService(java.util.concurrent.ExecutorService) IdMsgPair(org.thingsboard.server.service.queue.processing.IdMsgPair) TbSubscriptionUtils(org.thingsboard.server.service.subscription.TbSubscriptionUtils) Order(org.springframework.core.annotation.Order) FromDeviceRpcResponse(org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse) PartitionChangeEvent(org.thingsboard.server.queue.discovery.event.PartitionChangeEvent) EdgeNotificationMsgProto(org.thingsboard.server.gen.transport.TransportProtos.EdgeNotificationMsgProto) RpcError(org.thingsboard.server.common.data.rpc.RpcError) TbDeviceProfileCache(org.thingsboard.server.service.profile.TbDeviceProfileCache) TimeUnit(java.util.concurrent.TimeUnit) SubscriptionManagerService(org.thingsboard.server.service.subscription.SubscriptionManagerService) OtaPackageStateService(org.thingsboard.server.service.ota.OtaPackageStateService) TbLocalSubscriptionService(org.thingsboard.server.service.subscription.TbLocalSubscriptionService) TbTimeSeriesDeleteProto(org.thingsboard.server.gen.transport.TransportProtos.TbTimeSeriesDeleteProto) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) CountDownLatch(java.util.concurrent.CountDownLatch) TbCallback(org.thingsboard.server.common.msg.queue.TbCallback) UUID(java.util.UUID)

Example 2 with TbCallback

use of org.thingsboard.server.common.msg.queue.TbCallback in project thingsboard by thingsboard.

the class DefaultSubscriptionManagerService method addSubscription.

@Override
public void addSubscription(TbSubscription subscription, TbCallback callback) {
    log.trace("[{}][{}][{}] Registering subscription for entity [{}]", subscription.getServiceId(), subscription.getSessionId(), subscription.getSubscriptionId(), subscription.getEntityId());
    TopicPartitionInfo tpi = partitionService.resolve(ServiceType.TB_CORE, subscription.getTenantId(), subscription.getEntityId());
    if (currentPartitions.contains(tpi)) {
        partitionedSubscriptions.computeIfAbsent(tpi, k -> ConcurrentHashMap.newKeySet()).add(subscription);
        callback.onSuccess();
    } else {
        log.warn("[{}][{}] Entity belongs to external partition. Probably rebalancing is in progress. Topic: {}", subscription.getTenantId(), subscription.getEntityId(), tpi.getFullTopicName());
        callback.onFailure(new RuntimeException("Entity belongs to external partition " + tpi.getFullTopicName() + "!"));
    }
    boolean newSubscription = subscriptionsByEntityId.computeIfAbsent(subscription.getEntityId(), k -> ConcurrentHashMap.newKeySet()).add(subscription);
    subscriptionsByWsSessionId.computeIfAbsent(subscription.getSessionId(), k -> new ConcurrentHashMap<>()).put(subscription.getSubscriptionId(), subscription);
    if (newSubscription) {
        switch(subscription.getType()) {
            case TIMESERIES:
                handleNewTelemetrySubscription((TbTimeseriesSubscription) subscription);
                break;
            case ATTRIBUTES:
                handleNewAttributeSubscription((TbAttributeSubscription) subscription);
                break;
            case ALARMS:
                handleNewAlarmsSubscription((TbAlarmsSubscription) subscription);
                break;
        }
    }
}
Also used : DonAsynchron(org.thingsboard.common.util.DonAsynchron) Autowired(org.springframework.beans.factory.annotation.Autowired) ToCoreNotificationMsg(org.thingsboard.server.gen.transport.TransportProtos.ToCoreNotificationMsg) AlarmSubscriptionUpdate(org.thingsboard.server.service.telemetry.sub.AlarmSubscriptionUpdate) TenantId(org.thingsboard.server.common.data.id.TenantId) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) PreDestroy(javax.annotation.PreDestroy) Map(java.util.Map) EntityType(org.thingsboard.server.common.data.EntityType) DeviceAttributesEventNotificationMsg(org.thingsboard.rule.engine.api.msg.DeviceAttributesEventNotificationMsg) LocalSubscriptionServiceMsgProto(org.thingsboard.server.gen.transport.TransportProtos.LocalSubscriptionServiceMsgProto) TbServiceInfoProvider(org.thingsboard.server.queue.discovery.TbServiceInfoProvider) AttributesService(org.thingsboard.server.dao.attributes.AttributesService) TbQueueProducer(org.thingsboard.server.queue.TbQueueProducer) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Predicate(java.util.function.Predicate) DeviceStateService(org.thingsboard.server.service.state.DeviceStateService) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TimeseriesService(org.thingsboard.server.dao.timeseries.TimeseriesService) Set(java.util.Set) ThingsBoardThreadFactory(org.thingsboard.common.util.ThingsBoardThreadFactory) Executors(java.util.concurrent.Executors) Alarm(org.thingsboard.server.common.data.alarm.Alarm) TbQueueProducerProvider(org.thingsboard.server.queue.provider.TbQueueProducerProvider) Objects(java.util.Objects) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) PostConstruct(javax.annotation.PostConstruct) TbClusterService(org.thingsboard.server.cluster.TbClusterService) TbSubscriptionUpdateProto(org.thingsboard.server.gen.transport.TransportProtos.TbSubscriptionUpdateProto) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) PartitionService(org.thingsboard.server.queue.discovery.PartitionService) JacksonUtil(org.thingsboard.common.util.JacksonUtil) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) TbCoreComponent(org.thingsboard.server.queue.util.TbCoreComponent) ServiceType(org.thingsboard.server.common.msg.queue.ServiceType) Service(org.springframework.stereotype.Service) EntityId(org.thingsboard.server.common.data.id.EntityId) TbApplicationEventListener(org.thingsboard.server.queue.discovery.TbApplicationEventListener) TbCallback(org.thingsboard.server.common.msg.queue.TbCallback) ExecutorService(java.util.concurrent.ExecutorService) DataConstants(org.thingsboard.server.common.data.DataConstants) TbSubscriptionUpdateTsValue(org.thingsboard.server.gen.transport.TransportProtos.TbSubscriptionUpdateTsValue) DefaultDeviceStateService(org.thingsboard.server.service.state.DefaultDeviceStateService) PartitionChangeEvent(org.thingsboard.server.queue.discovery.event.PartitionChangeEvent) Aggregation(org.thingsboard.server.common.data.kv.Aggregation) TreeMap(java.util.TreeMap) TbSubscriptionUpdateValueListProto(org.thingsboard.server.gen.transport.TransportProtos.TbSubscriptionUpdateValueListProto) TbAlarmSubscriptionUpdateProto(org.thingsboard.server.gen.transport.TransportProtos.TbAlarmSubscriptionUpdateProto) TelemetrySubscriptionUpdate(org.thingsboard.server.service.telemetry.sub.TelemetrySubscriptionUpdate) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with TbCallback

use of org.thingsboard.server.common.msg.queue.TbCallback in project thingsboard by thingsboard.

the class DeviceActorMessageProcessor method process.

void process(TbActorCtx context, TransportToDeviceActorMsgWrapper wrapper) {
    TransportToDeviceActorMsg msg = wrapper.getMsg();
    TbCallback callback = wrapper.getCallback();
    var sessionInfo = msg.getSessionInfo();
    if (msg.hasSessionEvent()) {
        processSessionStateMsgs(sessionInfo, msg.getSessionEvent());
    }
    if (msg.hasSubscribeToAttributes()) {
        processSubscriptionCommands(context, sessionInfo, msg.getSubscribeToAttributes());
    }
    if (msg.hasSubscribeToRPC()) {
        processSubscriptionCommands(context, sessionInfo, msg.getSubscribeToRPC());
    }
    if (msg.hasSendPendingRPC()) {
        sendPendingRequests(context, getSessionId(sessionInfo), sessionInfo.getNodeId());
    }
    if (msg.hasGetAttributes()) {
        handleGetAttributesRequest(context, sessionInfo, msg.getGetAttributes());
    }
    if (msg.hasToDeviceRPCCallResponse()) {
        processRpcResponses(context, sessionInfo, msg.getToDeviceRPCCallResponse());
    }
    if (msg.hasSubscriptionInfo()) {
        handleSessionActivity(context, sessionInfo, msg.getSubscriptionInfo());
    }
    if (msg.hasClaimDevice()) {
        handleClaimDeviceMsg(context, sessionInfo, msg.getClaimDevice());
    }
    if (msg.hasRpcResponseStatusMsg()) {
        processRpcResponseStatus(context, sessionInfo, msg.getRpcResponseStatusMsg());
    }
    if (msg.hasUplinkNotificationMsg()) {
        processUplinkNotificationMsg(context, sessionInfo, msg.getUplinkNotificationMsg());
    }
    callback.onSuccess();
}
Also used : TbCallback(org.thingsboard.server.common.msg.queue.TbCallback) TransportToDeviceActorMsg(org.thingsboard.server.gen.transport.TransportProtos.TransportToDeviceActorMsg)

Example 4 with TbCallback

use of org.thingsboard.server.common.msg.queue.TbCallback in project thingsboard by thingsboard.

the class DefaultTbCoreConsumerService method launchMainConsumers.

@Override
protected void launchMainConsumers() {
    consumersExecutor.submit(() -> {
        while (!stopped) {
            try {
                List<TbProtoQueueMsg<ToCoreMsg>> msgs = mainConsumer.poll(pollDuration);
                if (msgs.isEmpty()) {
                    continue;
                }
                List<IdMsgPair<ToCoreMsg>> orderedMsgList = msgs.stream().map(msg -> new IdMsgPair<>(UUID.randomUUID(), msg)).collect(Collectors.toList());
                ConcurrentMap<UUID, TbProtoQueueMsg<ToCoreMsg>> pendingMap = orderedMsgList.stream().collect(Collectors.toConcurrentMap(IdMsgPair::getUuid, IdMsgPair::getMsg));
                CountDownLatch processingTimeoutLatch = new CountDownLatch(1);
                TbPackProcessingContext<TbProtoQueueMsg<ToCoreMsg>> ctx = new TbPackProcessingContext<>(processingTimeoutLatch, pendingMap, new ConcurrentHashMap<>());
                PendingMsgHolder pendingMsgHolder = new PendingMsgHolder();
                Future<?> packSubmitFuture = consumersExecutor.submit(() -> {
                    orderedMsgList.forEach((element) -> {
                        UUID id = element.getUuid();
                        TbProtoQueueMsg<ToCoreMsg> msg = element.getMsg();
                        log.trace("[{}] Creating main callback for message: {}", id, msg.getValue());
                        TbCallback callback = new TbPackCallback<>(id, ctx);
                        try {
                            ToCoreMsg toCoreMsg = msg.getValue();
                            pendingMsgHolder.setToCoreMsg(toCoreMsg);
                            if (toCoreMsg.hasToSubscriptionMgrMsg()) {
                                log.trace("[{}] Forwarding message to subscription manager service {}", id, toCoreMsg.getToSubscriptionMgrMsg());
                                forwardToSubMgrService(toCoreMsg.getToSubscriptionMgrMsg(), callback);
                            } else if (toCoreMsg.hasToDeviceActorMsg()) {
                                log.trace("[{}] Forwarding message to device actor {}", id, toCoreMsg.getToDeviceActorMsg());
                                forwardToDeviceActor(toCoreMsg.getToDeviceActorMsg(), callback);
                            } else if (toCoreMsg.hasDeviceStateServiceMsg()) {
                                log.trace("[{}] Forwarding message to state service {}", id, toCoreMsg.getDeviceStateServiceMsg());
                                forwardToStateService(toCoreMsg.getDeviceStateServiceMsg(), callback);
                            } else if (toCoreMsg.hasEdgeNotificationMsg()) {
                                log.trace("[{}] Forwarding message to edge service {}", id, toCoreMsg.getEdgeNotificationMsg());
                                forwardToEdgeNotificationService(toCoreMsg.getEdgeNotificationMsg(), callback);
                            } else if (!toCoreMsg.getToDeviceActorNotificationMsg().isEmpty()) {
                                Optional<TbActorMsg> actorMsg = encodingService.decode(toCoreMsg.getToDeviceActorNotificationMsg().toByteArray());
                                if (actorMsg.isPresent()) {
                                    TbActorMsg tbActorMsg = actorMsg.get();
                                    if (tbActorMsg.getMsgType().equals(MsgType.DEVICE_RPC_REQUEST_TO_DEVICE_ACTOR_MSG)) {
                                        tbCoreDeviceRpcService.forwardRpcRequestToDeviceActor((ToDeviceRpcRequestActorMsg) tbActorMsg);
                                    } else {
                                        log.trace("[{}] Forwarding message to App Actor {}", id, actorMsg.get());
                                        actorContext.tell(actorMsg.get());
                                    }
                                }
                                callback.onSuccess();
                            }
                        } catch (Throwable e) {
                            log.warn("[{}] Failed to process message: {}", id, msg, e);
                            callback.onFailure(e);
                        }
                    });
                });
                if (!processingTimeoutLatch.await(packProcessingTimeout, TimeUnit.MILLISECONDS)) {
                    if (!packSubmitFuture.isDone()) {
                        packSubmitFuture.cancel(true);
                        ToCoreMsg lastSubmitMsg = pendingMsgHolder.getToCoreMsg();
                        log.info("Timeout to process message: {}", lastSubmitMsg);
                    }
                    ctx.getAckMap().forEach((id, msg) -> log.debug("[{}] Timeout to process message: {}", id, msg.getValue()));
                    ctx.getFailedMap().forEach((id, msg) -> log.warn("[{}] Failed to process message: {}", id, msg.getValue()));
                }
                mainConsumer.commit();
            } catch (Exception e) {
                if (!stopped) {
                    log.warn("Failed to obtain messages from queue.", e);
                    try {
                        Thread.sleep(pollDuration);
                    } catch (InterruptedException e2) {
                        log.trace("Failed to wait until the server has capacity to handle new requests", e2);
                    }
                }
            }
        }
        log.info("TB Core Consumer stopped.");
    });
}
Also used : TbCoreDeviceRpcService(org.thingsboard.server.service.rpc.TbCoreDeviceRpcService) DeviceStateServiceMsgProto(org.thingsboard.server.gen.transport.TransportProtos.DeviceStateServiceMsgProto) ApplicationReadyEvent(org.springframework.boot.context.event.ApplicationReadyEvent) ToDeviceRpcRequestActorMsg(org.thingsboard.server.service.rpc.ToDeviceRpcRequestActorMsg) ToCoreNotificationMsg(org.thingsboard.server.gen.transport.TransportProtos.ToCoreNotificationMsg) StatsFactory(org.thingsboard.server.common.stats.StatsFactory) TenantId(org.thingsboard.server.common.data.id.TenantId) PreDestroy(javax.annotation.PreDestroy) Future(java.util.concurrent.Future) TbAlarmDeleteProto(org.thingsboard.server.gen.transport.TransportProtos.TbAlarmDeleteProto) ActorSystemContext(org.thingsboard.server.actors.ActorSystemContext) EdgeNotificationService(org.thingsboard.server.service.edge.EdgeNotificationService) SubscriptionMgrMsgProto(org.thingsboard.server.gen.transport.TransportProtos.SubscriptionMgrMsgProto) AbstractConsumerService(org.thingsboard.server.service.queue.processing.AbstractConsumerService) LocalSubscriptionServiceMsgProto(org.thingsboard.server.gen.transport.TransportProtos.LocalSubscriptionServiceMsgProto) FromDeviceRPCResponseProto(org.thingsboard.server.gen.transport.TransportProtos.FromDeviceRPCResponseProto) TbSubscriptionCloseProto(org.thingsboard.server.gen.transport.TransportProtos.TbSubscriptionCloseProto) TransportToDeviceActorMsg(org.thingsboard.server.gen.transport.TransportProtos.TransportToDeviceActorMsg) TbAlarmUpdateProto(org.thingsboard.server.gen.transport.TransportProtos.TbAlarmUpdateProto) ToOtaPackageStateServiceMsg(org.thingsboard.server.gen.transport.TransportProtos.ToOtaPackageStateServiceMsg) DeviceStateService(org.thingsboard.server.service.state.DeviceStateService) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) EventListener(org.springframework.context.event.EventListener) ThingsBoardThreadFactory(org.thingsboard.common.util.ThingsBoardThreadFactory) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) TbTimeSeriesUpdateProto(org.thingsboard.server.gen.transport.TransportProtos.TbTimeSeriesUpdateProto) Executors(java.util.concurrent.Executors) Alarm(org.thingsboard.server.common.data.alarm.Alarm) CountDownLatch(java.util.concurrent.CountDownLatch) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) TbActorMsg(org.thingsboard.server.common.msg.TbActorMsg) TbCoreQueueFactory(org.thingsboard.server.queue.provider.TbCoreQueueFactory) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) TbTenantProfileCache(org.thingsboard.server.dao.tenant.TbTenantProfileCache) TbApiUsageStateService(org.thingsboard.server.service.apiusage.TbApiUsageStateService) TbQueueConsumer(org.thingsboard.server.queue.TbQueueConsumer) TransportToDeviceActorMsgWrapper(org.thingsboard.server.service.transport.msg.TransportToDeviceActorMsgWrapper) Setter(lombok.Setter) Getter(lombok.Getter) JacksonUtil(org.thingsboard.common.util.JacksonUtil) Scheduled(org.springframework.scheduling.annotation.Scheduled) Function(java.util.function.Function) TbAttributeDeleteProto(org.thingsboard.server.gen.transport.TransportProtos.TbAttributeDeleteProto) DataDecodingEncodingService(org.thingsboard.server.common.transport.util.DataDecodingEncodingService) ConcurrentMap(java.util.concurrent.ConcurrentMap) Value(org.springframework.beans.factory.annotation.Value) MsgType(org.thingsboard.server.common.msg.MsgType) ToUsageStatsServiceMsg(org.thingsboard.server.gen.transport.TransportProtos.ToUsageStatsServiceMsg) TbCoreComponent(org.thingsboard.server.queue.util.TbCoreComponent) ServiceType(org.thingsboard.server.common.msg.queue.ServiceType) Service(org.springframework.stereotype.Service) ToCoreMsg(org.thingsboard.server.gen.transport.TransportProtos.ToCoreMsg) TbAttributeUpdateProto(org.thingsboard.server.gen.transport.TransportProtos.TbAttributeUpdateProto) TbCallback(org.thingsboard.server.common.msg.queue.TbCallback) ExecutorService(java.util.concurrent.ExecutorService) IdMsgPair(org.thingsboard.server.service.queue.processing.IdMsgPair) TbSubscriptionUtils(org.thingsboard.server.service.subscription.TbSubscriptionUtils) Order(org.springframework.core.annotation.Order) FromDeviceRpcResponse(org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse) PartitionChangeEvent(org.thingsboard.server.queue.discovery.event.PartitionChangeEvent) EdgeNotificationMsgProto(org.thingsboard.server.gen.transport.TransportProtos.EdgeNotificationMsgProto) RpcError(org.thingsboard.server.common.data.rpc.RpcError) TbDeviceProfileCache(org.thingsboard.server.service.profile.TbDeviceProfileCache) TimeUnit(java.util.concurrent.TimeUnit) SubscriptionManagerService(org.thingsboard.server.service.subscription.SubscriptionManagerService) OtaPackageStateService(org.thingsboard.server.service.ota.OtaPackageStateService) TbLocalSubscriptionService(org.thingsboard.server.service.subscription.TbLocalSubscriptionService) TbTimeSeriesDeleteProto(org.thingsboard.server.gen.transport.TransportProtos.TbTimeSeriesDeleteProto) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) IdMsgPair(org.thingsboard.server.service.queue.processing.IdMsgPair) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) CountDownLatch(java.util.concurrent.CountDownLatch) ToCoreMsg(org.thingsboard.server.gen.transport.TransportProtos.ToCoreMsg) TbCallback(org.thingsboard.server.common.msg.queue.TbCallback) UUID(java.util.UUID) TbActorMsg(org.thingsboard.server.common.msg.TbActorMsg)

Example 5 with TbCallback

use of org.thingsboard.server.common.msg.queue.TbCallback in project thingsboard by thingsboard.

the class AbstractConsumerService method launchNotificationsConsumer.

protected void launchNotificationsConsumer() {
    notificationsConsumerExecutor.submit(() -> {
        while (!stopped) {
            try {
                List<TbProtoQueueMsg<N>> msgs = nfConsumer.poll(getNotificationPollDuration());
                if (msgs.isEmpty()) {
                    continue;
                }
                ConcurrentMap<UUID, TbProtoQueueMsg<N>> pendingMap = msgs.stream().collect(Collectors.toConcurrentMap(s -> UUID.randomUUID(), Function.identity()));
                CountDownLatch processingTimeoutLatch = new CountDownLatch(1);
                TbPackProcessingContext<TbProtoQueueMsg<N>> ctx = new TbPackProcessingContext<>(processingTimeoutLatch, pendingMap, new ConcurrentHashMap<>());
                pendingMap.forEach((id, msg) -> {
                    log.trace("[{}] Creating notification callback for message: {}", id, msg.getValue());
                    TbCallback callback = new TbPackCallback<>(id, ctx);
                    try {
                        handleNotification(id, msg, callback);
                    } catch (Throwable e) {
                        log.warn("[{}] Failed to process notification: {}", id, msg, e);
                        callback.onFailure(e);
                    }
                });
                if (!processingTimeoutLatch.await(getNotificationPackProcessingTimeout(), TimeUnit.MILLISECONDS)) {
                    ctx.getAckMap().forEach((id, msg) -> log.warn("[{}] Timeout to process notification: {}", id, msg.getValue()));
                    ctx.getFailedMap().forEach((id, msg) -> log.warn("[{}] Failed to process notification: {}", id, msg.getValue()));
                }
                nfConsumer.commit();
            } catch (Exception e) {
                if (!stopped) {
                    log.warn("Failed to obtain notifications from queue.", e);
                    try {
                        Thread.sleep(getNotificationPollDuration());
                    } catch (InterruptedException e2) {
                        log.trace("Failed to wait until the server has capacity to handle new notifications", e2);
                    }
                }
            }
        }
        log.info("TB Notifications Consumer stopped.");
    });
}
Also used : ApplicationReadyEvent(org.springframework.boot.context.event.ApplicationReadyEvent) DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) Function(java.util.function.Function) TenantId(org.thingsboard.server.common.data.id.TenantId) DataDecodingEncodingService(org.thingsboard.server.common.transport.util.DataDecodingEncodingService) ConcurrentMap(java.util.concurrent.ConcurrentMap) PreDestroy(javax.annotation.PreDestroy) ActorSystemContext(org.thingsboard.server.actors.ActorSystemContext) ServiceType(org.thingsboard.server.common.msg.queue.ServiceType) EntityType(org.thingsboard.server.common.data.EntityType) TbApplicationEventListener(org.thingsboard.server.queue.discovery.TbApplicationEventListener) TbCallback(org.thingsboard.server.common.msg.queue.TbCallback) ExecutorService(java.util.concurrent.ExecutorService) Order(org.springframework.core.annotation.Order) DeviceId(org.thingsboard.server.common.data.id.DeviceId) ComponentLifecycleMsg(org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg) PartitionChangeEvent(org.thingsboard.server.queue.discovery.event.PartitionChangeEvent) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) EventListener(org.springframework.context.event.EventListener) ThingsBoardThreadFactory(org.thingsboard.common.util.ThingsBoardThreadFactory) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) ComponentLifecycleEvent(org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent) Executors(java.util.concurrent.Executors) TbDeviceProfileCache(org.thingsboard.server.service.profile.TbDeviceProfileCache) ByteString(com.google.protobuf.ByteString) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) TbActorMsg(org.thingsboard.server.common.msg.TbActorMsg) TbPackProcessingContext(org.thingsboard.server.service.queue.TbPackProcessingContext) TenantProfileId(org.thingsboard.server.common.data.id.TenantProfileId) TbPackCallback(org.thingsboard.server.service.queue.TbPackCallback) Optional(java.util.Optional) TbApiUsageStateService(org.thingsboard.server.service.apiusage.TbApiUsageStateService) TbTenantProfileCache(org.thingsboard.server.dao.tenant.TbTenantProfileCache) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) TbQueueConsumer(org.thingsboard.server.queue.TbQueueConsumer) CustomerId(org.thingsboard.server.common.data.id.CustomerId) TbPackProcessingContext(org.thingsboard.server.service.queue.TbPackProcessingContext) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) CountDownLatch(java.util.concurrent.CountDownLatch) TbCallback(org.thingsboard.server.common.msg.queue.TbCallback) UUID(java.util.UUID) TbPackCallback(org.thingsboard.server.service.queue.TbPackCallback)

Aggregations

TbCallback (org.thingsboard.server.common.msg.queue.TbCallback)5 List (java.util.List)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 ConcurrentMap (java.util.concurrent.ConcurrentMap)4 ExecutorService (java.util.concurrent.ExecutorService)4 Executors (java.util.concurrent.Executors)4 Function (java.util.function.Function)4 PreDestroy (javax.annotation.PreDestroy)4 Slf4j (lombok.extern.slf4j.Slf4j)4 ThingsBoardThreadFactory (org.thingsboard.common.util.ThingsBoardThreadFactory)4 TenantId (org.thingsboard.server.common.data.id.TenantId)4 ServiceType (org.thingsboard.server.common.msg.queue.ServiceType)4 TbProtoQueueMsg (org.thingsboard.server.queue.common.TbProtoQueueMsg)4 PartitionChangeEvent (org.thingsboard.server.queue.discovery.event.PartitionChangeEvent)4 Optional (java.util.Optional)3 UUID (java.util.UUID)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 TimeUnit (java.util.concurrent.TimeUnit)3 Collectors (java.util.stream.Collectors)3 PostConstruct (javax.annotation.PostConstruct)3