|
|
|
@ -17,6 +17,8 @@ package org.thingsboard.monitoring.notification; |
|
|
|
|
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
import org.thingsboard.monitoring.data.notification.Notification; |
|
|
|
import org.thingsboard.monitoring.notification.channels.NotificationChannel; |
|
|
|
@ -24,7 +26,6 @@ import org.thingsboard.monitoring.notification.channels.NotificationChannel; |
|
|
|
import java.util.List; |
|
|
|
import java.util.concurrent.ExecutorService; |
|
|
|
import java.util.concurrent.Executors; |
|
|
|
import java.util.function.Consumer; |
|
|
|
|
|
|
|
@Component |
|
|
|
@RequiredArgsConstructor |
|
|
|
@ -34,15 +35,20 @@ public class NotificationService { |
|
|
|
private final List<NotificationChannel> notificationChannels; |
|
|
|
private final ExecutorService notificationExecutor = Executors.newSingleThreadExecutor(); |
|
|
|
|
|
|
|
public void sendNotification(Notification notification) { |
|
|
|
forEachNotificationChannel(notificationChannel -> notificationChannel.sendNotification(notification)); |
|
|
|
} |
|
|
|
@Value("${monitoring.notifications.message_prefix}") |
|
|
|
private String messagePrefix; |
|
|
|
|
|
|
|
private void forEachNotificationChannel(Consumer<NotificationChannel> function) { |
|
|
|
public void sendNotification(Notification notification) { |
|
|
|
String message; |
|
|
|
if (StringUtils.isEmpty(messagePrefix)) { |
|
|
|
message = notification.getText(); |
|
|
|
} else { |
|
|
|
message = messagePrefix + System.lineSeparator() + notification.getText(); |
|
|
|
} |
|
|
|
notificationChannels.forEach(notificationChannel -> { |
|
|
|
notificationExecutor.submit(() -> { |
|
|
|
try { |
|
|
|
function.accept(notificationChannel); |
|
|
|
notificationChannel.sendNotification(message); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("Failed to send notification to {}", notificationChannel.getClass().getSimpleName(), e); |
|
|
|
} |
|
|
|
|