Search in sources :

Example 1 with GeneralEdgeEventFetcher

use of org.thingsboard.server.service.edge.rpc.fetch.GeneralEdgeEventFetcher in project thingsboard by thingsboard.

the class EdgeGrpcSession method processEdgeEvents.

ListenableFuture<Void> processEdgeEvents() throws Exception {
    SettableFuture<Void> result = SettableFuture.create();
    log.trace("[{}] starting processing edge events", this.sessionId);
    if (isConnected() && isSyncCompleted()) {
        Long queueStartTs = getQueueStartTs().get();
        GeneralEdgeEventFetcher fetcher = new GeneralEdgeEventFetcher(queueStartTs, ctx.getEdgeEventService());
        ListenableFuture<UUID> ifOffsetFuture = startProcessingEdgeEvents(fetcher);
        Futures.addCallback(ifOffsetFuture, new FutureCallback<>() {

            @Override
            public void onSuccess(@Nullable UUID ifOffset) {
                if (ifOffset != null) {
                    Long newStartTs = Uuids.unixTimestamp(ifOffset);
                    ListenableFuture<List<Void>> updateFuture = updateQueueStartTs(newStartTs);
                    Futures.addCallback(updateFuture, new FutureCallback<>() {

                        @Override
                        public void onSuccess(@Nullable List<Void> list) {
                            log.debug("[{}] queue offset was updated [{}][{}]", sessionId, ifOffset, newStartTs);
                            result.set(null);
                        }

                        @Override
                        public void onFailure(Throwable t) {
                            log.error("[{}] Failed to update queue offset [{}]", sessionId, ifOffset, t);
                            result.setException(t);
                        }
                    }, ctx.getGrpcCallbackExecutorService());
                } else {
                    log.trace("[{}] ifOffset is null. Skipping iteration without db update", sessionId);
                    result.set(null);
                }
            }

            @Override
            public void onFailure(Throwable t) {
                log.error("[{}] Failed to process events", sessionId, t);
                result.setException(t);
            }
        }, ctx.getGrpcCallbackExecutorService());
    } else {
        log.trace("[{}] edge is not connected or sync is not completed. Skipping iteration", sessionId);
        result.set(null);
    }
    return result;
}
Also used : ListenableFuture(com.google.common.util.concurrent.ListenableFuture) List(java.util.List) ArrayList(java.util.ArrayList) UUID(java.util.UUID) GeneralEdgeEventFetcher(org.thingsboard.server.service.edge.rpc.fetch.GeneralEdgeEventFetcher) FutureCallback(com.google.common.util.concurrent.FutureCallback) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Aggregations

FutureCallback (com.google.common.util.concurrent.FutureCallback)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 UUID (java.util.UUID)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1 GeneralEdgeEventFetcher (org.thingsboard.server.service.edge.rpc.fetch.GeneralEdgeEventFetcher)1