-
Notifications
You must be signed in to change notification settings - Fork 0
Fixed async error #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Fixed async error #136
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
services/spring-letter/src/main/java/tum/devoops/letterservice/config/AsyncConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package tum.devoops.letterservice.config; | ||
|
|
||
| import java.util.concurrent.Executor; | ||
|
|
||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.scheduling.annotation.AsyncConfigurer; | ||
| import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
|
||
| @Configuration | ||
| public class AsyncConfig implements AsyncConfigurer { | ||
|
|
||
| @Override | ||
| @Bean(name = "taskExecutor") | ||
| public Executor getAsyncExecutor() { | ||
| ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
| executor.setCorePoolSize(2); | ||
| executor.setMaxPoolSize(10); | ||
| executor.setQueueCapacity(500); | ||
| executor.setThreadNamePrefix("mail-dispatch-"); | ||
| executor.initialize(); | ||
| return executor; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
services/spring-letter/src/main/java/tum/devoops/letterservice/service/MailDispatcher.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package tum.devoops.letterservice.service; | ||
|
|
||
| import io.micrometer.core.instrument.MeterRegistry; | ||
| import jakarta.mail.MessagingException; | ||
| import jakarta.mail.internet.MimeMessage; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.mail.javamail.JavaMailSender; | ||
| import org.springframework.mail.javamail.MimeMessageHelper; | ||
| import org.springframework.scheduling.annotation.Async; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| public class MailDispatcher { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(MailDispatcher.class); | ||
|
|
||
| private final JavaMailSender mailSender; | ||
| private final String from; | ||
| private final MeterRegistry meterRegistry; | ||
|
|
||
| public MailDispatcher(JavaMailSender mailSender, | ||
| @Value("${spring.mail.username}") String from, | ||
| MeterRegistry meterRegistry) { | ||
| this.mailSender = mailSender; | ||
| this.from = from; | ||
| this.meterRegistry = meterRegistry; | ||
| } | ||
|
|
||
| @Async | ||
| public void sendAsync(String to, String subject, String html) { | ||
| try { | ||
| MimeMessage message = mailSender.createMimeMessage(); | ||
| MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); | ||
| helper.setFrom(from); | ||
| helper.setTo(to); | ||
| helper.setSubject(subject); | ||
| helper.setText(html, true); | ||
| mailSender.send(message); | ||
| meterRegistry.counter("letters_sent_total", "status", "success").increment(); | ||
| } catch (MessagingException e) { | ||
| meterRegistry.counter("letters_sent_total", "status", "failure").increment(); | ||
| LOG.error("Failed to send mail to {}", to, e); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Catch Spring's
MailExceptionto properly track sending failures.mailSender.send(message)throws Spring'sMailException(an uncheckedRuntimeException), not the checkedMessagingException(which is only thrown byMimeMessageHelpersetup methods). By only catchingMessagingException, actual delivery failures (like connection refused or SMTP timeouts) will bypass this catch block. This will cause thefailuremetric to be missed, and the exception will be swallowed by Spring's async executor.Catch
Exception(or add a catch block forMailException) to ensure all delivery failures are correctly logged and metered.🐛 Proposed fix
📝 Committable suggestion
🧰 Tools
🪛 PMD (7.26.0)
[Low] 44-44: InvalidLogMessageFormat (Error Prone): Too many arguments, expected 1 argument but found 2
(InvalidLogMessageFormat (Error Prone))
🤖 Prompt for AI Agents