Skip to content
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

SBHWR-14: added async email executor #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/main/java/org/wyona/webapp/config/AsyncExecutorConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.wyona.webapp.config;

import java.util.concurrent.Executor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@EnableAsync
@ConfigurationProperties(prefix = "email.executor")
public class AsyncExecutorConfig
{

private int maxThreads;
private int maxPoolSize;
private int corePoolSize;

@Bean(name = "emailAsyncExecutor")
public Executor asyncExecutor()
{
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(maxThreads);
executor.setThreadNamePrefix("EmailAsyncExecutorThread-");
executor.initialize();
return executor;
}

public void setMaxThreads(int maxThreads) {
this.maxThreads = maxThreads;
}

public void setMaxPoolSize(int maxPoolSize) {
this.maxPoolSize = maxPoolSize;
}

public void setCorePoolSize(int corePoolSize) {
this.corePoolSize = corePoolSize;
}
}
22 changes: 22 additions & 0 deletions src/main/java/org/wyona/webapp/mail/AsyncEmailSender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.wyona.webapp.mail;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Transport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class AsyncEmailSender {

private final static Logger LOGGER = LoggerFactory.getLogger(AsyncEmailSender.class);

@Async("emailAsyncExecutor")
public void sendMailAsync(final Message message) throws MessagingException {
LOGGER.info("Async method started");
Transport.send(message) ;
LOGGER.info("Async method completed");
}
}
7 changes: 5 additions & 2 deletions src/main/java/org/wyona/webapp/mail/EmailSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ public class EmailSender {
@Value("${from.email.address}")
private String fromEmail;

private AsyncEmailSender asyncEmailSender;

@Autowired
public EmailSender(EmailSenderCofig config, EmailValidation emailValidation){
public EmailSender(EmailSenderCofig config, EmailValidation emailValidation, AsyncEmailSender asyncEmailSender){
this.emailValidation = emailValidation;
this.asyncEmailSender = asyncEmailSender;

Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
Expand Down Expand Up @@ -60,7 +63,7 @@ public void sendEmailGreeting(String email, String subject, String text, boolean

Message message = composeMessage(email, subject, text, isHTMLMessage, attachment);

Transport.send(message);
asyncEmailSender.sendMailAsync(message);
}

private void validateParameters(String email, String subject, String text) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ app.mail.port=587
app.mail.username=USERNAME
app.mail.password=PASSWORD

email.executor.corePoolSize=3
email.executor.maxPoolSize=3
email.executor.maxThreads=100

#app.mail.host=mail.wyona.com
#app.mail.port=587
#app.mail.username=USERNAME
Expand Down