Skip to content

Commit

Permalink
reduce state in application
Browse files Browse the repository at this point in the history
* by fetching and using mail-related data on-demand
  • Loading branch information
goekay committed Dec 17, 2024
1 parent ee07944 commit 0bf41ae
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 30 deletions.
31 changes: 3 additions & 28 deletions src/main/java/de/rwth/idsg/steve/service/MailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import jakarta.annotation.PostConstruct;
import jakarta.mail.Authenticator;
import jakarta.mail.Message;
import jakarta.mail.MessagingException;
Expand All @@ -35,11 +34,9 @@
import jakarta.mail.Transport;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;

import java.util.Properties;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
* @author Sevket Goekay <[email protected]>
Expand All @@ -52,31 +49,8 @@ public class MailService {
@Autowired private SettingsRepository settingsRepository;
@Autowired private ScheduledExecutorService executorService;

private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private final Lock readLock = readWriteLock.readLock();
private final Lock writeLock = readWriteLock.writeLock();

private MailSettings settings;
private Session session;

@PostConstruct
public void loadSettingsFromDB() {
writeLock.lock();
try {
settings = settingsRepository.getMailSettings();
} finally {
writeLock.unlock();
}
session = createSession(getSettings());
}

public MailSettings getSettings() {
readLock.lock();
try {
return this.settings;
} finally {
readLock.unlock();
}
return settingsRepository.getMailSettings();
}

public void sendTestMail() {
Expand All @@ -99,6 +73,7 @@ public void sendAsync(String subject, String body) {

public void send(String subject, String body) throws MessagingException {
MailSettings settings = getSettings();
Session session = createSession(getSettings());

Message mail = new MimeMessage(session);
mail.setSubject("[SteVe] " + subject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public String postSettings(@Valid @ModelAttribute("settingsForm") SettingsForm s
}

settingsRepository.update(settingsForm);
mailService.loadSettingsFromDB();
return "redirect:/manager/settings";
}

Expand All @@ -107,7 +106,6 @@ public String testMail(@Valid @ModelAttribute("settingsForm") SettingsForm setti
}

settingsRepository.update(settingsForm);
mailService.loadSettingsFromDB();
mailService.sendTestMail();

return "redirect:/manager/settings";
Expand Down

0 comments on commit 0bf41ae

Please sign in to comment.