Search in sources :

Example 1 with NotificationContext

use of org.apache.nifi.bootstrap.notification.NotificationContext in project nifi by apache.

the class NotificationServiceManager method notify.

public void notify(final NotificationType type, final String subject, final String message) {
    final List<ConfiguredNotificationService> configs = servicesByNotificationType.get(type);
    if (configs == null || configs.isEmpty()) {
        return;
    }
    for (final ConfiguredNotificationService config : configs) {
        final NotificationService service = config.getService();
        final AtomicInteger attemptCount = new AtomicInteger(0);
        notificationExecutor.submit(new Runnable() {

            @Override
            public void run() {
                // Check if the service is valid; if not, warn now so that users know this before they fail to receive notifications
                final ValidationContext validationContext = new NotificationValidationContext(buildNotificationContext(config), variableRegistry);
                final Collection<ValidationResult> validationResults = service.validate(validationContext);
                final List<String> invalidReasons = new ArrayList<>();
                for (final ValidationResult result : validationResults) {
                    if (!result.isValid()) {
                        invalidReasons.add(result.toString());
                    }
                }
                // If the service is valid, attempt to send the notification
                boolean failure = false;
                if (invalidReasons.isEmpty()) {
                    final NotificationContext context = buildNotificationContext(config);
                    try {
                        service.notify(context, type, subject, message);
                        logger.info("Successfully sent notification of type {} to {}", type, service);
                    } catch (final Throwable t) {
                        // keep running even if a Throwable is caught because we need to ensure that we are able to restart NiFi
                        logger.error("Failed to send notification of type {} to {} with Subject {} due to {}. Will ", type, service == null ? "Unknown Notification Service" : service.toString(), subject, t.toString());
                        logger.error("", t);
                        failure = true;
                    }
                } else {
                    logger.warn("Notification Service {} is not valid for the following reasons: {}", service, invalidReasons);
                    failure = true;
                }
                final int attempts = attemptCount.incrementAndGet();
                if (failure) {
                    if (attempts < maxAttempts) {
                        logger.info("After failing to send notification to {} {} times, will attempt again in 1 minute", service, attempts);
                        notificationExecutor.schedule(this, 1, TimeUnit.MINUTES);
                    } else {
                        logger.info("After failing to send notification of type {} to {} {} times, will no longer attempt to send notification", type, service, attempts);
                    }
                }
            }
        });
        if (NotificationType.NIFI_STOPPED.equals(type)) {
            // we don't want to return before the notifier has had a chance to perform its task.
            while (attemptCount.get() == 0) {
                try {
                    Thread.sleep(1000L);
                } catch (final InterruptedException ie) {
                }
            }
        }
    }
}
Also used : NotificationValidationContext(org.apache.nifi.bootstrap.notification.NotificationValidationContext) NotificationService(org.apache.nifi.bootstrap.notification.NotificationService) ValidationResult(org.apache.nifi.components.ValidationResult) NotificationValidationContext(org.apache.nifi.bootstrap.notification.NotificationValidationContext) ValidationContext(org.apache.nifi.components.ValidationContext) NotificationContext(org.apache.nifi.bootstrap.notification.NotificationContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Collection(java.util.Collection) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List)

Example 2 with NotificationContext

use of org.apache.nifi.bootstrap.notification.NotificationContext in project nifi by apache.

the class NotificationServiceManager method buildNotificationContext.

private NotificationContext buildNotificationContext(final ConfiguredNotificationService config) {
    return new NotificationContext() {

        @Override
        public PropertyValue getProperty(final PropertyDescriptor descriptor) {
            final PropertyDescriptor fullPropDescriptor = config.getService().getPropertyDescriptor(descriptor.getName());
            if (fullPropDescriptor == null) {
                return null;
            }
            String configuredValue = config.getProperties().get(fullPropDescriptor.getName());
            if (configuredValue == null) {
                configuredValue = fullPropDescriptor.getDefaultValue();
            }
            return new StandardPropertyValue(configuredValue, null, variableRegistry);
        }

        @Override
        public Map<PropertyDescriptor, String> getProperties() {
            final Map<PropertyDescriptor, String> props = new HashMap<>();
            final Map<String, String> configuredProps = config.getProperties();
            final NotificationService service = config.getService();
            final List<PropertyDescriptor> configuredPropertyDescriptors = new ArrayList<>(service.getPropertyDescriptors());
            // This is needed to capture all dynamic properties
            configuredProps.forEach((key, value) -> {
                PropertyDescriptor propertyDescriptor = config.service.getPropertyDescriptor(key);
                props.put(config.service.getPropertyDescriptor(key), value);
                configuredPropertyDescriptors.remove(propertyDescriptor);
            });
            for (final PropertyDescriptor descriptor : configuredPropertyDescriptors) {
                props.put(descriptor, descriptor.getDefaultValue());
            }
            return props;
        }
    };
}
Also used : NotificationContext(org.apache.nifi.bootstrap.notification.NotificationContext) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) ArrayList(java.util.ArrayList) NotificationService(org.apache.nifi.bootstrap.notification.NotificationService)

Aggregations

ArrayList (java.util.ArrayList)2 NotificationContext (org.apache.nifi.bootstrap.notification.NotificationContext)2 NotificationService (org.apache.nifi.bootstrap.notification.NotificationService)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 StandardPropertyValue (org.apache.nifi.attribute.expression.language.StandardPropertyValue)1 NotificationValidationContext (org.apache.nifi.bootstrap.notification.NotificationValidationContext)1 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)1 ValidationContext (org.apache.nifi.components.ValidationContext)1 ValidationResult (org.apache.nifi.components.ValidationResult)1 NodeList (org.w3c.dom.NodeList)1