Skip to content

Commit 9cd148f

Browse files
committed
#466: implemented AutoCloseable so that the connection pools shut down when Spring wants to shut down the application. Note: this does NOT work with try..with resource in combination with async emails being sent. And using it with try..with resource when not async doesn't make sense, because why have a connection pool in that case.
1 parent bcae134 commit 9cd148f

File tree

2 files changed

+14
-2
lines changed
  • modules
    • core-module/src/main/java/org/simplejavamail/api/mailer
    • simple-java-mail/src/main/java/org/simplejavamail/mailer/internal

2 files changed

+14
-2
lines changed

modules/core-module/src/main/java/org/simplejavamail/api/mailer/Mailer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.jetbrains.annotations.Nullable;
88
import org.simplejavamail.MailException;
99
import org.simplejavamail.api.email.Email;
10-
import org.simplejavamail.api.email.EmailWithDefaultsAndOverridesApplied;
1110
import org.simplejavamail.api.mailer.config.EmailGovernance;
1211
import org.simplejavamail.api.mailer.config.OperationalConfig;
1312
import org.simplejavamail.api.mailer.config.ProxyConfig;
@@ -34,7 +33,7 @@
3433
* @see MailerRegularBuilder
3534
* @see Email
3635
*/
37-
public interface Mailer {
36+
public interface Mailer extends AutoCloseable {
3837
/**
3938
* In case Simple Java Mail falls short somehow, you can get a hold of the internal {@link Session} instance to debug or tweak. Please let us know
4039
* why you are needing this on <a href="https://github.com/bbottema/simple-java-mail/issues">simple-java-mail/issues</a>.

modules/simple-java-mail/src/main/java/org/simplejavamail/mailer/internal/MailerImpl.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.List;
2727
import java.util.Properties;
2828
import java.util.concurrent.CompletableFuture;
29+
import java.util.concurrent.ExecutionException;
2930
import java.util.concurrent.Future;
3031
import java.util.concurrent.atomic.AtomicInteger;
3132

@@ -445,4 +446,16 @@ public OperationalConfig getOperationalConfig() {
445446
public EmailGovernance getEmailGovernance() {
446447
return emailGovernance;
447448
}
449+
450+
/**
451+
* NOTE: this doesn't work with try-with resource if emails are sent asynchronously. This auto-close is only
452+
* meant for Spring integration, for when Spring wants to close the Mailer bean.
453+
*
454+
* @see <a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/DisposableBean.html">Spring DisposableBean</a>
455+
* @see Mailer#close()
456+
*/
457+
@Override
458+
public void close() throws ExecutionException, InterruptedException {
459+
shutdownConnectionPool().get();
460+
}
448461
}

0 commit comments

Comments
 (0)