Search in sources :

Example 1 with TbPackProcessingContext

use of org.thingsboard.server.service.queue.TbPackProcessingContext 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

ByteString (com.google.protobuf.ByteString)1 List (java.util.List)1 Optional (java.util.Optional)1 UUID (java.util.UUID)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 TimeUnit (java.util.concurrent.TimeUnit)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 PreDestroy (javax.annotation.PreDestroy)1 Slf4j (lombok.extern.slf4j.Slf4j)1 ApplicationReadyEvent (org.springframework.boot.context.event.ApplicationReadyEvent)1 EventListener (org.springframework.context.event.EventListener)1 Order (org.springframework.core.annotation.Order)1 ThingsBoardThreadFactory (org.thingsboard.common.util.ThingsBoardThreadFactory)1 ActorSystemContext (org.thingsboard.server.actors.ActorSystemContext)1 EntityType (org.thingsboard.server.common.data.EntityType)1