Search in sources :

Example 1 with RuleNode

use of org.thingsboard.rule.engine.api.RuleNode in project thingsboard by thingsboard.

the class AnnotationComponentDiscoveryService method scanAndPersistComponent.

private ComponentDescriptor scanAndPersistComponent(BeanDefinition def, ComponentType type) {
    ComponentDescriptor scannedComponent = new ComponentDescriptor();
    String clazzName = def.getBeanClassName();
    try {
        scannedComponent.setType(type);
        Class<?> clazz = Class.forName(clazzName);
        RuleNode ruleNodeAnnotation = clazz.getAnnotation(RuleNode.class);
        scannedComponent.setName(ruleNodeAnnotation.name());
        scannedComponent.setScope(ruleNodeAnnotation.scope());
        NodeDefinition nodeDefinition = prepareNodeDefinition(ruleNodeAnnotation);
        ObjectNode configurationDescriptor = mapper.createObjectNode();
        JsonNode node = mapper.valueToTree(nodeDefinition);
        configurationDescriptor.set("nodeDefinition", node);
        scannedComponent.setConfigurationDescriptor(configurationDescriptor);
        scannedComponent.setClazz(clazzName);
        log.info("Processing scanned component: {}", scannedComponent);
    } catch (Exception e) {
        log.error("Can't initialize component {}, due to {}", def.getBeanClassName(), e.getMessage(), e);
        throw new RuntimeException(e);
    }
    ComponentDescriptor persistedComponent = componentDescriptorService.findByClazz(TenantId.SYS_TENANT_ID, clazzName);
    if (persistedComponent == null) {
        log.info("Persisting new component: {}", scannedComponent);
        scannedComponent = componentDescriptorService.saveComponent(TenantId.SYS_TENANT_ID, scannedComponent);
    } else if (scannedComponent.equals(persistedComponent)) {
        log.info("Component is already persisted: {}", persistedComponent);
        scannedComponent = persistedComponent;
    } else {
        log.info("Component {} will be updated to {}", persistedComponent, scannedComponent);
        componentDescriptorService.deleteByClazz(TenantId.SYS_TENANT_ID, persistedComponent.getClazz());
        scannedComponent.setId(persistedComponent.getId());
        scannedComponent = componentDescriptorService.saveComponent(TenantId.SYS_TENANT_ID, scannedComponent);
    }
    return scannedComponent;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ComponentDescriptor(org.thingsboard.server.common.data.plugin.ComponentDescriptor) RuleNode(org.thingsboard.rule.engine.api.RuleNode) NodeDefinition(org.thingsboard.rule.engine.api.NodeDefinition) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 2 with RuleNode

use of org.thingsboard.rule.engine.api.RuleNode in project thingsboard by thingsboard.

the class AnnotationComponentDiscoveryService method registerRuleNodeComponents.

private void registerRuleNodeComponents() {
    Set<BeanDefinition> ruleNodeBeanDefinitions = getBeanDefinitions(RuleNode.class);
    for (BeanDefinition def : ruleNodeBeanDefinitions) {
        int retryCount = 0;
        Exception cause = null;
        while (retryCount < MAX_OPTIMISITC_RETRIES) {
            try {
                String clazzName = def.getBeanClassName();
                Class<?> clazz = Class.forName(clazzName);
                RuleNode ruleNodeAnnotation = clazz.getAnnotation(RuleNode.class);
                ComponentType type = ruleNodeAnnotation.type();
                ComponentDescriptor component = scanAndPersistComponent(def, type);
                components.put(component.getClazz(), component);
                putComponentIntoMaps(type, ruleNodeAnnotation, component);
                break;
            } catch (Exception e) {
                log.trace("Can't initialize component {}, due to {}", def.getBeanClassName(), e.getMessage(), e);
                cause = e;
                retryCount++;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e1) {
                    throw new RuntimeException(e1);
                }
            }
        }
        if (cause != null && retryCount == MAX_OPTIMISITC_RETRIES) {
            log.error("Can't initialize component {}, due to {}", def.getBeanClassName(), cause.getMessage(), cause);
            throw new RuntimeException(cause);
        }
    }
}
Also used : ComponentType(org.thingsboard.server.common.data.plugin.ComponentType) RuleNode(org.thingsboard.rule.engine.api.RuleNode) ComponentDescriptor(org.thingsboard.server.common.data.plugin.ComponentDescriptor) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Aggregations

RuleNode (org.thingsboard.rule.engine.api.RuleNode)2 ComponentDescriptor (org.thingsboard.server.common.data.plugin.ComponentDescriptor)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)1 NodeDefinition (org.thingsboard.rule.engine.api.NodeDefinition)1 ComponentType (org.thingsboard.server.common.data.plugin.ComponentType)1