Search in sources :

Example 1 with TbRuleEngineProcessingDecision

use of org.thingsboard.server.service.queue.processing.TbRuleEngineProcessingDecision in project thingsboard by thingsboard.

the class DefaultTbRuleEngineConsumerService method consumerLoop.

void consumerLoop(TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>> consumer, TbRuleEngineQueueConfiguration configuration, TbRuleEngineConsumerStats stats, String threadSuffix) {
    updateCurrentThreadName(threadSuffix);
    while (!stopped && !consumer.isStopped()) {
        try {
            List<TbProtoQueueMsg<ToRuleEngineMsg>> msgs = consumer.poll(pollDuration);
            if (msgs.isEmpty()) {
                continue;
            }
            final TbRuleEngineSubmitStrategy submitStrategy = getSubmitStrategy(configuration);
            final TbRuleEngineProcessingStrategy ackStrategy = getAckStrategy(configuration);
            submitStrategy.init(msgs);
            while (!stopped) {
                TbMsgPackProcessingContext ctx = new TbMsgPackProcessingContext(configuration.getName(), submitStrategy, ackStrategy.isSkipTimeoutMsgs());
                submitStrategy.submitAttempt((id, msg) -> submitExecutor.submit(() -> submitMessage(configuration, stats, ctx, id, msg)));
                final boolean timeout = !ctx.await(configuration.getPackProcessingTimeout(), TimeUnit.MILLISECONDS);
                TbRuleEngineProcessingResult result = new TbRuleEngineProcessingResult(configuration.getName(), timeout, ctx);
                if (timeout) {
                    printFirstOrAll(configuration, ctx, ctx.getPendingMap(), "Timeout");
                }
                if (!ctx.getFailedMap().isEmpty()) {
                    printFirstOrAll(configuration, ctx, ctx.getFailedMap(), "Failed");
                }
                ctx.printProfilerStats();
                TbRuleEngineProcessingDecision decision = ackStrategy.analyze(result);
                if (statsEnabled) {
                    stats.log(result, decision.isCommit());
                }
                ctx.cleanup();
                if (decision.isCommit()) {
                    submitStrategy.stop();
                    break;
                } else {
                    submitStrategy.update(decision.getReprocessMap());
                }
            }
            consumer.commit();
        } catch (Exception e) {
            if (!stopped) {
                log.warn("Failed to process 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 Rule Engine Consumer stopped.");
}
Also used : TbRuleEngineProcessingStrategy(org.thingsboard.server.service.queue.processing.TbRuleEngineProcessingStrategy) TbRuleEngineProcessingDecision(org.thingsboard.server.service.queue.processing.TbRuleEngineProcessingDecision) TbRuleEngineSubmitStrategy(org.thingsboard.server.service.queue.processing.TbRuleEngineSubmitStrategy) TbRuleEngineProcessingResult(org.thingsboard.server.service.queue.processing.TbRuleEngineProcessingResult) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) RuleEngineException(org.thingsboard.server.common.msg.queue.RuleEngineException)

Aggregations

RuleEngineException (org.thingsboard.server.common.msg.queue.RuleEngineException)1 TbProtoQueueMsg (org.thingsboard.server.queue.common.TbProtoQueueMsg)1 TbRuleEngineProcessingDecision (org.thingsboard.server.service.queue.processing.TbRuleEngineProcessingDecision)1 TbRuleEngineProcessingResult (org.thingsboard.server.service.queue.processing.TbRuleEngineProcessingResult)1 TbRuleEngineProcessingStrategy (org.thingsboard.server.service.queue.processing.TbRuleEngineProcessingStrategy)1 TbRuleEngineSubmitStrategy (org.thingsboard.server.service.queue.processing.TbRuleEngineSubmitStrategy)1