Search in sources :

Example 1 with NotificationManager

use of com.epam.pipeline.notifier.service.task.NotificationManager in project cloud-pipeline by epam.

the class NotificationService method sendNotification.

/**
 * Scheduled task to load batch of {@link NotificationMessage} from database
 * and delegate it to all realizations of {@link NotificationManager}
 */
@Scheduled(fixedDelayString = "${notification.scheduler.delay}")
@Transactional(propagation = Propagation.REQUIRED)
public void sendNotification() {
    LOGGER.debug("Start scheduled notification loop...");
    Pageable limit = new PageRequest(0, notificationAtTime);
    List<NotificationMessage> result = notificationRepository.loadNotification(limit);
    result.forEach(message -> {
        notificationRepository.deleteById(message.getId());
        for (NotificationManager notificationManager : notificationManagers) {
            CompletableFuture.runAsync(() -> notificationManager.notifySubscribers(message), notificationThreadPool).exceptionally(throwable -> {
                LOGGER.warn("Exception while trying to send email", throwable);
                return null;
            });
        }
    });
    LOGGER.debug("End scheduled notification loop...");
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Pageable(org.springframework.data.domain.Pageable) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) NotificationManager(com.epam.pipeline.notifier.service.task.NotificationManager) Scheduled(org.springframework.scheduling.annotation.Scheduled) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

NotificationMessage (com.epam.pipeline.entity.notification.NotificationMessage)1 NotificationManager (com.epam.pipeline.notifier.service.task.NotificationManager)1 PageRequest (org.springframework.data.domain.PageRequest)1 Pageable (org.springframework.data.domain.Pageable)1 Scheduled (org.springframework.scheduling.annotation.Scheduled)1 Transactional (org.springframework.transaction.annotation.Transactional)1