Improved Notifications async process (ref #244)
This commit is contained in:
parent
ca10323e78
commit
a648466b45
|
@ -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<String, String> 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<String, String> data, String subjectTemplate) {
|
||||
|
|
|
@ -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<Notification> notifications = this.apiContext.getOperationsContext().getDatabaseRepository().getNotificationDao().asQueryable().where(((builder, root) ->
|
||||
List<CompletableFuture<Notification>> 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();
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue