Skip to content

Commit 4032490

Browse files
committed
Allow threads to timeout in the thread pool
1 parent 76289b3 commit 4032490

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

src/main/java/org/simplejavamail/mailer/internal/mailsender/MailSender.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
import java.util.List;
2424
import java.util.Properties;
2525
import java.util.concurrent.ExecutorService;
26-
import java.util.concurrent.Executors;
26+
import java.util.concurrent.LinkedBlockingQueue;
2727
import java.util.concurrent.Phaser;
28+
import java.util.concurrent.ThreadPoolExecutor;
29+
import java.util.concurrent.TimeUnit;
2830

2931
import static java.lang.String.format;
3032
import static org.simplejavamail.converter.EmailConverter.mimeMessageToEML;
@@ -192,9 +194,19 @@ the proxy bridge server (or connection pool in async mode) while a non-async ema
192194
smtpRequestsPhaser.register();
193195
if (async) {
194196
// start up thread pool if necessary
195-
if (executor == null || executor.isShutdown()) {
196-
executor = Executors.newFixedThreadPool(operationalConfig.getThreadPoolSize(),
197-
new NamedThreadFactory("Simple Java Mail async mail sender"));
197+
if (executor == null) {
198+
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
199+
operationalConfig.getThreadPoolSize(),
200+
operationalConfig.getThreadPoolSize(),
201+
operationalConfig.getThreadPoolTimeout(),
202+
TimeUnit.MILLISECONDS,
203+
new LinkedBlockingQueue<Runnable>(),
204+
new NamedThreadFactory("Simple Java Mail async mail sender")
205+
);
206+
if(operationalConfig.getThreadPoolTimeout() > 0) {
207+
threadPoolExecutor.allowCoreThreadTimeOut(true);
208+
}
209+
executor = threadPoolExecutor;
198210
}
199211
configureSessionWithTimeout(session, operationalConfig.getSessionTimeout());
200212
executor.execute(new Runnable() {
@@ -300,7 +312,7 @@ private void configureBounceToAddress(final Session session, final Email email)
300312
}
301313

302314
/**
303-
* We need to keep a count of running threads in case a proxyserver is running or a connection pool needs to be shut down.
315+
* We need to keep a count of running threads in case a proxyserver is running
304316
*/
305317
private synchronized void checkShutDownRunningProcesses() {
306318
smtpRequestsPhaser.arriveAndDeregister();
@@ -312,11 +324,6 @@ private synchronized void checkShutDownRunningProcesses() {
312324
LOGGER.trace("stopping proxy bridge...");
313325
proxyServer.stop();
314326
}
315-
// shutdown the threadpool, or else the Mailer will keep any JVM alive forever
316-
// executor is only available in async mode
317-
if (executor != null) {
318-
executor.shutdown();
319-
}
320327
}
321328
}
322329

src/main/java/org/simplejavamail/mailer/internal/mailsender/OperationalConfig.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public class OperationalConfig {
2929
*/
3030
private final int threadPoolSize;
3131

32+
/**
33+
* @see org.simplejavamail.mailer.MailerBuilder.MailerRegularBuilder#withThreadPoolTimeout(Integer)
34+
*/
35+
private final int threadPoolTimeout;
36+
3237
/**
3338
* @see org.simplejavamail.mailer.MailerBuilder.MailerRegularBuilder#withTransportModeLoggingOnly(Boolean)
3439
*/
@@ -56,6 +61,7 @@ public OperationalConfig(@Nonnull Properties properties, int sessionTimeout, int
5661
this.properties = properties;
5762
this.sessionTimeout = sessionTimeout;
5863
this.threadPoolSize = threadPoolSize;
64+
this.threadPoolTimeout = 1; // TODO wire it up to all the config files
5965
this.transportModeLoggingOnly = transportModeLoggingOnly;
6066
this.debugLogging = debugLogging;
6167
this.sslHostsToTrust = Collections.unmodifiableList(sslHostsToTrust);
@@ -75,7 +81,14 @@ public int getSessionTimeout() {
7581
public int getThreadPoolSize() {
7682
return threadPoolSize;
7783
}
78-
84+
85+
/**
86+
* @see org.simplejavamail.mailer.MailerBuilder.MailerRegularBuilder#withThreadPoolSize(Integer)
87+
*/
88+
public int getThreadPoolTimeout() {
89+
return threadPoolTimeout;
90+
}
91+
7992
/**
8093
* @see org.simplejavamail.mailer.MailerBuilder.MailerRegularBuilder#withTransportModeLoggingOnly(Boolean)
8194
*/

0 commit comments

Comments
 (0)