From a648466b451dc146768b24a1088765463a0d5dd5 Mon Sep 17 00:00:00 2001 From: George Kalampokis Date: Tue, 18 Feb 2020 13:22:17 +0200 Subject: [PATCH] Improved Notifications async process (ref #244) --- .../logic/managers/NotificationManager.java | 13 ++------ .../notification/NotificationScheduleJob.java | 31 ++++++++++++------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/NotificationManager.java b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/NotificationManager.java index 1051d20b1..afe65dd97 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/NotificationManager.java +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/NotificationManager.java @@ -4,7 +4,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import eu.eudat.data.entities.Notification; import eu.eudat.data.entities.UserInfo; import eu.eudat.data.enumeration.notification.ActiveStatus; -import eu.eudat.data.enumeration.notification.NotificationType; import eu.eudat.data.enumeration.notification.NotifyState; import eu.eudat.logic.services.ApiContext; import eu.eudat.logic.services.utilities.MailService; @@ -18,11 +17,7 @@ import org.springframework.stereotype.Component; import javax.mail.MessagingException; import javax.transaction.Transactional; import java.io.IOException; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; -import java.util.concurrent.CompletableFuture; +import java.util.*; @Component public class NotificationManager { @@ -86,8 +81,6 @@ public class NotificationManager { switch (notification.getContactTypeHint()) { case EMAIL: this.sendEmailNotification(notification, userInfo, data, subjectTemplate, contentTemplate); - notification.setNotifyState(NotifyState.SUCCEEDED); - notification.setUpdatedAt(new Date()); break; } }catch (Exception e) { @@ -98,7 +91,6 @@ public class NotificationManager { } private void sendEmailNotification(Notification notification, UserInfo userInfo, Map data, String subjectTemplate, String contentTemplate) throws IOException { - CompletableFuture.runAsync(() -> { SimpleMail simpleMail = new SimpleMail(); simpleMail.setFrom(this.environment.getProperty("mail.from")); simpleMail.setSubject(makeSubject(data, subjectTemplate)); @@ -106,12 +98,13 @@ public class NotificationManager { simpleMail.setContent(makeContent(data, notification, userInfo, contentTemplate)); try { mailService.sendSimpleMail(simpleMail); + notification.setNotifyState(NotifyState.SUCCEEDED); + notification.setUpdatedAt(new Date()); } catch (MessagingException e) { notification.setNotifyState(NotifyState.ERROR); notification.setUpdatedAt(new Date()); logger.error(e.getMessage(), e); } - }); } private String makeSubject(Map data, String subjectTemplate) { diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/schedule/notification/NotificationScheduleJob.java b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/schedule/notification/NotificationScheduleJob.java index 3d9f04cdd..24c5b593a 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/schedule/notification/NotificationScheduleJob.java +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/schedule/notification/NotificationScheduleJob.java @@ -12,7 +12,9 @@ import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import javax.transaction.Transactional; +import java.util.LinkedList; import java.util.List; +import java.util.concurrent.CompletableFuture; @Component public class NotificationScheduleJob { @@ -30,19 +32,26 @@ public class NotificationScheduleJob { @Transactional @Scheduled(fixedRateString = "${notification.rateInterval}") public void sendNotifications() { - List notifications = this.apiContext.getOperationsContext().getDatabaseRepository().getNotificationDao().asQueryable().where(((builder, root) -> + List> futures = new LinkedList<>(); + this.apiContext.getOperationsContext().getDatabaseRepository().getNotificationDao().asQueryable().where(((builder, root) -> builder.and( builder.or( builder.equal(root.get("notifyState"), NotifyState.PENDING), builder.equal(root.get("notifyState"), NotifyState.ERROR)) - , builder.equal(root.get("isActive"), ActiveStatus.ACTIVE)))).toList(); - if (!notifications.isEmpty()) { - notifications.forEach(notification -> { - try { - this.notificationManager.sendNotification(notification); - } catch (Exception e) { - logger.error(e.getMessage(), e); - } - }); - } + , builder.equal(root.get("isActive"), ActiveStatus.ACTIVE)))).toListAsync().thenApplyAsync((notifications) -> { + if (!notifications.isEmpty()) { + notifications.forEach(notification -> { + try { + this.notificationManager.sendNotification(notification); + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + }); + } + return notifications; + }).thenApplyAsync((notifications) -> { + notifications.forEach((notification) -> futures.add(this.apiContext.getOperationsContext().getDatabaseRepository().getNotificationDao().createOrUpdateAsync(notification))); + return futures; + }).join(); + } }