Search in sources :

Example 1 with TbQueueCallback

use of org.thingsboard.server.queue.TbQueueCallback in project thingsboard by thingsboard.

the class DefaultTbQueueRequestTemplate method sendToRequestTemplate.

void sendToRequestTemplate(Request request, UUID requestId, SettableFuture<Response> future, ResponseMetaData<Response> responseMetaData) {
    log.trace("[{}] Sending request, key [{}], expTime [{}], request {}", requestId, request.getKey(), responseMetaData.expTime, request);
    if (messagesStats != null) {
        messagesStats.incrementTotal();
    }
    requestTemplate.send(TopicPartitionInfo.builder().topic(requestTemplate.getDefaultTopic()).build(), request, new TbQueueCallback() {

        @Override
        public void onSuccess(TbQueueMsgMetadata metadata) {
            if (messagesStats != null) {
                messagesStats.incrementSuccessful();
            }
            log.trace("[{}] Request sent: {}, request {}", requestId, metadata, request);
        }

        @Override
        public void onFailure(Throwable t) {
            if (messagesStats != null) {
                messagesStats.incrementFailed();
            }
            pendingRequests.remove(requestId);
            future.setException(t);
        }
    });
}
Also used : TbQueueCallback(org.thingsboard.server.queue.TbQueueCallback) TbQueueMsgMetadata(org.thingsboard.server.queue.TbQueueMsgMetadata)

Example 2 with TbQueueCallback

use of org.thingsboard.server.queue.TbQueueCallback in project thingsboard by thingsboard.

the class TelemetryEdgeProcessor method processPostAttributes.

private ListenableFuture<Void> processPostAttributes(TenantId tenantId, CustomerId customerId, EntityId entityId, TransportProtos.PostAttributeMsg msg, TbMsgMetaData metaData) {
    SettableFuture<Void> futureToSet = SettableFuture.create();
    JsonObject json = JsonUtils.getJsonObject(msg.getKvList());
    Pair<String, RuleChainId> defaultQueueAndRuleChain = getDefaultQueueNameAndRuleChainId(tenantId, entityId);
    String queueName = defaultQueueAndRuleChain.getKey();
    RuleChainId ruleChainId = defaultQueueAndRuleChain.getValue();
    TbMsg tbMsg = TbMsg.newMsg(queueName, SessionMsgType.POST_ATTRIBUTES_REQUEST.name(), entityId, customerId, metaData, gson.toJson(json), ruleChainId, null);
    tbClusterService.pushMsgToRuleEngine(tenantId, tbMsg.getOriginator(), tbMsg, new TbQueueCallback() {

        @Override
        public void onSuccess(TbQueueMsgMetadata metadata) {
            futureToSet.set(null);
        }

        @Override
        public void onFailure(Throwable t) {
            log.error("Can't process post attributes [{}]", msg, t);
            futureToSet.setException(t);
        }
    });
    return futureToSet;
}
Also used : TbQueueCallback(org.thingsboard.server.queue.TbQueueCallback) JsonObject(com.google.gson.JsonObject) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) TbQueueMsgMetadata(org.thingsboard.server.queue.TbQueueMsgMetadata) TbMsg(org.thingsboard.server.common.msg.TbMsg)

Example 3 with TbQueueCallback

use of org.thingsboard.server.queue.TbQueueCallback in project thingsboard by thingsboard.

the class TelemetryEdgeProcessor method processAttributesUpdate.

private ListenableFuture<Void> processAttributesUpdate(TenantId tenantId, CustomerId customerId, EntityId entityId, TransportProtos.PostAttributeMsg msg, TbMsgMetaData metaData) {
    SettableFuture<Void> futureToSet = SettableFuture.create();
    JsonObject json = JsonUtils.getJsonObject(msg.getKvList());
    Set<AttributeKvEntry> attributes = JsonConverter.convertToAttributes(json);
    ListenableFuture<List<Void>> future = attributesService.save(tenantId, entityId, metaData.getValue("scope"), new ArrayList<>(attributes));
    Futures.addCallback(future, new FutureCallback<List<Void>>() {

        @Override
        public void onSuccess(@Nullable List<Void> voids) {
            Pair<String, RuleChainId> defaultQueueAndRuleChain = getDefaultQueueNameAndRuleChainId(tenantId, entityId);
            String queueName = defaultQueueAndRuleChain.getKey();
            RuleChainId ruleChainId = defaultQueueAndRuleChain.getValue();
            TbMsg tbMsg = TbMsg.newMsg(queueName, DataConstants.ATTRIBUTES_UPDATED, entityId, customerId, metaData, gson.toJson(json), ruleChainId, null);
            tbClusterService.pushMsgToRuleEngine(tenantId, tbMsg.getOriginator(), tbMsg, new TbQueueCallback() {

                @Override
                public void onSuccess(TbQueueMsgMetadata metadata) {
                    futureToSet.set(null);
                }

                @Override
                public void onFailure(Throwable t) {
                    log.error("Can't process attributes update [{}]", msg, t);
                    futureToSet.setException(t);
                }
            });
        }

        @Override
        public void onFailure(Throwable t) {
            log.error("Can't process attributes update [{}]", msg, t);
            futureToSet.setException(t);
        }
    }, dbCallbackExecutorService);
    return futureToSet;
}
Also used : AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) TbQueueCallback(org.thingsboard.server.queue.TbQueueCallback) JsonObject(com.google.gson.JsonObject) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) TbQueueMsgMetadata(org.thingsboard.server.queue.TbQueueMsgMetadata) List(java.util.List) ArrayList(java.util.ArrayList) TbMsg(org.thingsboard.server.common.msg.TbMsg) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair)

Example 4 with TbQueueCallback

use of org.thingsboard.server.queue.TbQueueCallback in project thingsboard by thingsboard.

the class TelemetryEdgeProcessor method processAttributeDeleteMsg.

private ListenableFuture<Void> processAttributeDeleteMsg(TenantId tenantId, EntityId entityId, AttributeDeleteMsg attributeDeleteMsg, String entityType) {
    SettableFuture<Void> futureToSet = SettableFuture.create();
    String scope = attributeDeleteMsg.getScope();
    List<String> attributeNames = attributeDeleteMsg.getAttributeNamesList();
    attributesService.removeAll(tenantId, entityId, scope, attributeNames);
    if (EntityType.DEVICE.name().equals(entityType)) {
        Set<AttributeKey> attributeKeys = new HashSet<>();
        for (String attributeName : attributeNames) {
            attributeKeys.add(new AttributeKey(scope, attributeName));
        }
        tbClusterService.pushMsgToCore(DeviceAttributesEventNotificationMsg.onDelete(tenantId, (DeviceId) entityId, attributeKeys), new TbQueueCallback() {

            @Override
            public void onSuccess(TbQueueMsgMetadata metadata) {
                futureToSet.set(null);
            }

            @Override
            public void onFailure(Throwable t) {
                log.error("Can't process attribute delete msg [{}]", attributeDeleteMsg, t);
                futureToSet.setException(t);
            }
        });
    }
    return futureToSet;
}
Also used : AttributeKey(org.thingsboard.server.common.data.kv.AttributeKey) TbQueueCallback(org.thingsboard.server.queue.TbQueueCallback) DeviceId(org.thingsboard.server.common.data.id.DeviceId) TbQueueMsgMetadata(org.thingsboard.server.queue.TbQueueMsgMetadata) HashSet(java.util.HashSet)

Example 5 with TbQueueCallback

use of org.thingsboard.server.queue.TbQueueCallback in project thingsboard by thingsboard.

the class DefaultTbClusterService method broadcast.

private void broadcast(ToTransportMsg transportMsg, TbQueueCallback callback) {
    TbQueueProducer<TbProtoQueueMsg<ToTransportMsg>> toTransportNfProducer = producerProvider.getTransportNotificationsMsgProducer();
    Set<String> tbTransportServices = partitionService.getAllServiceIds(ServiceType.TB_TRANSPORT);
    TbQueueCallback proxyCallback = callback != null ? new MultipleTbQueueCallbackWrapper(tbTransportServices.size(), callback) : null;
    for (String transportServiceId : tbTransportServices) {
        TopicPartitionInfo tpi = partitionService.getNotificationsTopic(ServiceType.TB_TRANSPORT, transportServiceId);
        toTransportNfProducer.send(tpi, new TbProtoQueueMsg<>(UUID.randomUUID(), transportMsg), proxyCallback);
        toTransportNfs.incrementAndGet();
    }
}
Also used : TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) TbQueueCallback(org.thingsboard.server.queue.TbQueueCallback) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) MultipleTbQueueCallbackWrapper(org.thingsboard.server.queue.common.MultipleTbQueueCallbackWrapper) ByteString(com.google.protobuf.ByteString)

Aggregations

TbQueueCallback (org.thingsboard.server.queue.TbQueueCallback)8 TbQueueMsgMetadata (org.thingsboard.server.queue.TbQueueMsgMetadata)7 TbMsg (org.thingsboard.server.common.msg.TbMsg)4 JsonObject (com.google.gson.JsonObject)3 DeviceId (org.thingsboard.server.common.data.id.DeviceId)3 RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ByteString (com.google.protobuf.ByteString)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 UUID (java.util.UUID)1 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)1 Pair (org.apache.commons.lang3.tuple.Pair)1 AttributeKey (org.thingsboard.server.common.data.kv.AttributeKey)1 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)1 TopicPartitionInfo (org.thingsboard.server.common.msg.queue.TopicPartitionInfo)1 FromDeviceRpcResponse (org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse)1 TransportProtos (org.thingsboard.server.gen.transport.TransportProtos)1