Skip to content

Commit

Permalink
#148: Added support for handling async results including exception
Browse files Browse the repository at this point in the history
  • Loading branch information
bbottema committed Jul 1, 2018
1 parent baf46b4 commit 6acfc71
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/main/java/org/simplejavamail/mailer/Mailer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.EnumSet;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Future;

import static java.lang.String.format;
import static org.simplejavamail.internal.util.MiscUtil.valueNullOrEmpty;
Expand Down Expand Up @@ -236,10 +237,11 @@ public final void sendMail(final Email email) {
* @see MailSender#send(Email, boolean)
* @see #validate(Email)
*/
public final synchronized void sendMail(final Email email, @SuppressWarnings("SameParameterValue") final boolean async) {
public final synchronized Future<?> sendMail(final Email email, @SuppressWarnings("SameParameterValue") final boolean async) {
if (validate(email)) {
mailSender.send(email, async);
return mailSender.send(email, async);
}
throw new AssertionError("Email not valid, but no MailException was thrown for it");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.Phaser;

import static java.lang.String.format;
Expand Down Expand Up @@ -177,10 +178,11 @@ private static AnonymousSocks5Server configureSessionWithProxy(@Nonnull final Pr
* @param email The information for the email to be sent.
* @param async If false, this method blocks until the mail has been processed completely by the SMTP server. If true, a new thread is started to
* send the email and this method returns immediately.
* @return A {@link Future} or null if not <em>async</em>.
* @throws MailException Can be thrown if an email isn't validating correctly, or some other problem occurs during connection, sending etc.
* @see Executors#newFixedThreadPool(int)
*/
public final synchronized void send(final Email email, final boolean async) {
public final synchronized Future<?> send(final Email email, final boolean async) {
/*
we need to track even non-async emails to prevent async emails from shutting down
the proxy bridge server (or connection pool in async mode) while a non-async email is still being processed
Expand All @@ -196,7 +198,7 @@ the proxy bridge server (or connection pool in async mode) while a non-async ema
executor = Executors.newFixedThreadPool(operationalConfig.getThreadPoolSize());
}
configureSessionWithTimeout(session, operationalConfig.getSessionTimeout());
executor.execute(new Runnable() {
return executor.submit(new Runnable() {
@Override
public void run() {
sendMailClosure(session, email);
Expand All @@ -209,6 +211,7 @@ public String toString() {
});
} else {
sendMailClosure(session, email);
return null;
}
}

Expand Down

0 comments on commit 6acfc71

Please sign in to comment.