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.Notification;
|
||||||
import eu.eudat.data.entities.UserInfo;
|
import eu.eudat.data.entities.UserInfo;
|
||||||
import eu.eudat.data.enumeration.notification.ActiveStatus;
|
import eu.eudat.data.enumeration.notification.ActiveStatus;
|
||||||
import eu.eudat.data.enumeration.notification.NotificationType;
|
|
||||||
import eu.eudat.data.enumeration.notification.NotifyState;
|
import eu.eudat.data.enumeration.notification.NotifyState;
|
||||||
import eu.eudat.logic.services.ApiContext;
|
import eu.eudat.logic.services.ApiContext;
|
||||||
import eu.eudat.logic.services.utilities.MailService;
|
import eu.eudat.logic.services.utilities.MailService;
|
||||||
|
@ -18,11 +17,7 @@ import org.springframework.stereotype.Component;
|
||||||
import javax.mail.MessagingException;
|
import javax.mail.MessagingException;
|
||||||
import javax.transaction.Transactional;
|
import javax.transaction.Transactional;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Date;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class NotificationManager {
|
public class NotificationManager {
|
||||||
|
@ -86,8 +81,6 @@ public class NotificationManager {
|
||||||
switch (notification.getContactTypeHint()) {
|
switch (notification.getContactTypeHint()) {
|
||||||
case EMAIL:
|
case EMAIL:
|
||||||
this.sendEmailNotification(notification, userInfo, data, subjectTemplate, contentTemplate);
|
this.sendEmailNotification(notification, userInfo, data, subjectTemplate, contentTemplate);
|
||||||
notification.setNotifyState(NotifyState.SUCCEEDED);
|
|
||||||
notification.setUpdatedAt(new Date());
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}catch (Exception e) {
|
}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 {
|
private void sendEmailNotification(Notification notification, UserInfo userInfo, Map<String, String> data, String subjectTemplate, String contentTemplate) throws IOException {
|
||||||
CompletableFuture.runAsync(() -> {
|
|
||||||
SimpleMail simpleMail = new SimpleMail();
|
SimpleMail simpleMail = new SimpleMail();
|
||||||
simpleMail.setFrom(this.environment.getProperty("mail.from"));
|
simpleMail.setFrom(this.environment.getProperty("mail.from"));
|
||||||
simpleMail.setSubject(makeSubject(data, subjectTemplate));
|
simpleMail.setSubject(makeSubject(data, subjectTemplate));
|
||||||
|
@ -106,12 +98,13 @@ public class NotificationManager {
|
||||||
simpleMail.setContent(makeContent(data, notification, userInfo, contentTemplate));
|
simpleMail.setContent(makeContent(data, notification, userInfo, contentTemplate));
|
||||||
try {
|
try {
|
||||||
mailService.sendSimpleMail(simpleMail);
|
mailService.sendSimpleMail(simpleMail);
|
||||||
|
notification.setNotifyState(NotifyState.SUCCEEDED);
|
||||||
|
notification.setUpdatedAt(new Date());
|
||||||
} catch (MessagingException e) {
|
} catch (MessagingException e) {
|
||||||
notification.setNotifyState(NotifyState.ERROR);
|
notification.setNotifyState(NotifyState.ERROR);
|
||||||
notification.setUpdatedAt(new Date());
|
notification.setUpdatedAt(new Date());
|
||||||
logger.error(e.getMessage(), e);
|
logger.error(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String makeSubject(Map<String, String> data, String subjectTemplate) {
|
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 org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import javax.transaction.Transactional;
|
import javax.transaction.Transactional;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class NotificationScheduleJob {
|
public class NotificationScheduleJob {
|
||||||
|
@ -30,11 +32,12 @@ public class NotificationScheduleJob {
|
||||||
@Transactional
|
@Transactional
|
||||||
@Scheduled(fixedRateString = "${notification.rateInterval}")
|
@Scheduled(fixedRateString = "${notification.rateInterval}")
|
||||||
public void sendNotifications() {
|
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.and(
|
||||||
builder.or(
|
builder.or(
|
||||||
builder.equal(root.get("notifyState"), NotifyState.PENDING), builder.equal(root.get("notifyState"), NotifyState.ERROR))
|
builder.equal(root.get("notifyState"), NotifyState.PENDING), builder.equal(root.get("notifyState"), NotifyState.ERROR))
|
||||||
, builder.equal(root.get("isActive"), ActiveStatus.ACTIVE)))).toList();
|
, builder.equal(root.get("isActive"), ActiveStatus.ACTIVE)))).toListAsync().thenApplyAsync((notifications) -> {
|
||||||
if (!notifications.isEmpty()) {
|
if (!notifications.isEmpty()) {
|
||||||
notifications.forEach(notification -> {
|
notifications.forEach(notification -> {
|
||||||
try {
|
try {
|
||||||
|
@ -44,5 +47,11 @@ public class NotificationScheduleJob {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
return notifications;
|
||||||
|
}).thenApplyAsync((notifications) -> {
|
||||||
|
notifications.forEach((notification) -> futures.add(this.apiContext.getOperationsContext().getDatabaseRepository().getNotificationDao().createOrUpdateAsync(notification)));
|
||||||
|
return futures;
|
||||||
|
}).join();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue