Skip to content

Commit

Permalink
Added: Notification async execution pool
Browse files Browse the repository at this point in the history
  • Loading branch information
djuarezgf committed Nov 14, 2024
1 parent 7189998 commit eb83414
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- DataSHIELD Project Status Error
- Catch token manager exceptions and return ERROR status
- Send emails in async execution pool
- Notification async execution pool
32 changes: 26 additions & 6 deletions src/main/java/de/samply/app/ProjectManagerAsyncConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,44 @@ public class ProjectManagerAsyncConfiguration {
private final int emailSenderCorePoolSize;
private final int emailSenderMaxPoolSize;
private final int emailSenderQueueCapacity;
private final int notificationCorePoolSize;
private final int notificationMaxPoolSize;
private final int notificationQueueCapacity;


public ProjectManagerAsyncConfiguration(
@Value(ProjectManagerConst.EMAIL_SENDER_CORE_POOL_SIZE_SV) int emailSenderCorePoolSize,
@Value(ProjectManagerConst.EMAIL_SENDER_MAX_POOL_SIZE_SV) int emailSenderMaxPoolSize,
@Value(ProjectManagerConst.EMAIL_SENDER_QUEUE_CAPACITY_SV) int emailSenderQueueCapacity) {
@Value(ProjectManagerConst.EMAIL_SENDER_QUEUE_CAPACITY_SV) int emailSenderQueueCapacity,
@Value(ProjectManagerConst.NOTIFICATION_CORE_POOL_SIZE_SV) int notificationCorePoolSize,
@Value(ProjectManagerConst.NOTIFICATION_MAX_POOL_SIZE_SV) int notificationMaxPoolSize,
@Value(ProjectManagerConst.NOTIFICATION_QUEUE_CAPACITY_SV) int notificationQueueCapacity) {
this.emailSenderCorePoolSize = emailSenderCorePoolSize;
this.emailSenderMaxPoolSize = emailSenderMaxPoolSize;
this.emailSenderQueueCapacity = emailSenderQueueCapacity;
this.notificationCorePoolSize = notificationCorePoolSize;
this.notificationMaxPoolSize = notificationMaxPoolSize;
this.notificationQueueCapacity = notificationQueueCapacity;
}

@Bean(name = ProjectManagerConst.ASYNC_EMAIL_SENDER_EXECUTOR)
public Executor taskExecutor() {
public Executor emailSenderExecutor() {
return createEmailSenderExecutor(emailSenderCorePoolSize, emailSenderMaxPoolSize,
emailSenderQueueCapacity, ProjectManagerConst.ASYNC_EMAIL_SENDER_EXECUTOR);
}

@Bean(name = ProjectManagerConst.ASYNC_NOTIFICATION_EXECUTOR)
public Executor notificationExecutor() {
return createEmailSenderExecutor(notificationCorePoolSize, notificationMaxPoolSize,
notificationQueueCapacity, ProjectManagerConst.ASYNC_NOTIFICATION_EXECUTOR);
}

private Executor createEmailSenderExecutor(int corePoolSize, int maxPoolSize, int queueCapacity, String prefix) {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(emailSenderCorePoolSize);
executor.setMaxPoolSize(emailSenderMaxPoolSize);
executor.setQueueCapacity(emailSenderQueueCapacity);
executor.setThreadNamePrefix(ProjectManagerConst.ASYNC_EMAIL_SENDER_EXECUTOR + "-");
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.setThreadNamePrefix(prefix + "-");
executor.initialize();
return executor;
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/de/samply/app/ProjectManagerConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,10 @@ public class ProjectManagerConst {
public final static String EMAIL_SENDER_MAX_POOL_SIZE = "EMAIL_SENDER_MAX_POOL_SIZE";
public final static String EMAIL_SENDER_QUEUE_CAPACITY = "EMAIL_SENDER_QUEUE_CAPACITY";

public final static String NOTIFICATION_CORE_POOL_SIZE = "NOTIFICATION_CORE_POOL_SIZE";
public final static String NOTIFICATION_MAX_POOL_SIZE = "NOTIFICATION_MAX_POOL_SIZE";
public final static String NOTIFICATION_QUEUE_CAPACITY = "NOTIFICATION_QUEUE_CAPACITY";

// Spring Values (SV)
public final static String HEAD_SV = "${";
public final static String BOTTOM_SV = "}";
Expand Down Expand Up @@ -459,8 +463,13 @@ public class ProjectManagerConst {
public final static String EMAIL_SENDER_MAX_POOL_SIZE_SV = HEAD_SV + EMAIL_SENDER_MAX_POOL_SIZE + ":8" + BOTTOM_SV;
public final static String EMAIL_SENDER_QUEUE_CAPACITY_SV = HEAD_SV + EMAIL_SENDER_QUEUE_CAPACITY + ":500" + BOTTOM_SV;

public final static String NOTIFICATION_CORE_POOL_SIZE_SV = HEAD_SV + NOTIFICATION_CORE_POOL_SIZE + ":4" + BOTTOM_SV;
public final static String NOTIFICATION_MAX_POOL_SIZE_SV = HEAD_SV + NOTIFICATION_MAX_POOL_SIZE + ":8" + BOTTOM_SV;
public final static String NOTIFICATION_QUEUE_CAPACITY_SV = HEAD_SV + NOTIFICATION_QUEUE_CAPACITY + ":500" + BOTTOM_SV;

// Async Configuration
public final static String ASYNC_EMAIL_SENDER_EXECUTOR = "email-sender";
public final static String ASYNC_NOTIFICATION_EXECUTOR = "notification";

// Others
public final static String TEST_EMAIL = "test@project-manager.com";
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/de/samply/email/EmailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public EmailService(
this.frontendService = frontendService;
}

@Async(ProjectManagerConst.ASYNC_EMAIL_SENDER_EXECUTOR)
public void sendEmail(@NotNull String email, Optional<String> project, Optional<String> bridgehead, @NotNull ProjectRole role, @NotNull EmailTemplateType type) throws EmailServiceException {
if (enableEmails) {
sendEmail(email, project, bridgehead, role, type, new HashMap<>());
Expand All @@ -67,6 +68,7 @@ public void sendEmail(@NotNull String email, Optional<String> project, Optional<
}
}

@Async(ProjectManagerConst.ASYNC_EMAIL_SENDER_EXECUTOR)
public void sendEmail(@NotNull String email, Optional<String> project, Optional<String> bridgehead, @NotNull ProjectRole role, @NotNull EmailTemplateType type, Map<String, String> keyValues) throws EmailServiceException {
if (enableEmails) {
Map<String, String> context = new HashMap<>();
Expand Down Expand Up @@ -96,9 +98,7 @@ public void sendEmail(@NotNull String email, Optional<String> project, Optional<
}
}


@Async(ProjectManagerConst.ASYNC_EMAIL_SENDER_EXECUTOR)
public void sendEmail(String email, MessageSubject messageSubject) {
private void sendEmail(String email, MessageSubject messageSubject) {
try {
mailSender.send(createMimeMessage(email, emailFrom, messageSubject));
} catch (MailException | EmailServiceException e) {
Expand Down Expand Up @@ -134,10 +134,6 @@ private Optional<MessageSubject> createEmailMessageAndSubject(ProjectRole role,
return Optional.empty();
}

private String createEmailMessageAndSubject(String template, Map<String, String> keyValues) {
return templateEngine.process(template, createContext(keyValues));
}

private Context createContext(Map<String, String> keyValues) {
Context context = new Context();
keyValues.forEach((key, value) -> context.setVariable(key, value));
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/de/samply/notification/NotificationService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.samply.notification;

import de.samply.app.ProjectManagerConst;
import de.samply.db.model.Notification;
import de.samply.db.model.NotificationUserAction;
import de.samply.db.model.Project;
Expand All @@ -11,6 +12,7 @@
import de.samply.user.roles.OrganisationRole;
import jakarta.validation.constraints.NotNull;
import org.springframework.http.HttpStatus;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.Instant;
Expand Down Expand Up @@ -40,6 +42,7 @@ public NotificationService(NotificationRepository notificationRepository,
this.dtoFactory = dtoFactory;
}

@Async(ProjectManagerConst.ASYNC_NOTIFICATION_EXECUTOR)
public void createNotification(@NotNull String projectCode, String bridgehead, String email,
@NotNull OperationType operationType,
@NotNull String details, String error, HttpStatus httpStatus
Expand Down Expand Up @@ -99,6 +102,7 @@ private List<String> fetchUserVisibleBridgeheads(Optional<String> requestedBridg
}
}

@Async(ProjectManagerConst.ASYNC_NOTIFICATION_EXECUTOR)
public void setNotificationAsRead(@NotNull Long notificationId) {
NotificationUserAction notificationUserAction = fetchNotificationUserAction(notificationId);
notificationUserAction.setRead(true);
Expand Down

0 comments on commit eb83414

Please sign in to comment.