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

Allow threads to timeout in the thread pool #206

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.Phaser;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import static java.lang.String.format;
import static org.simplejavamail.converter.EmailConverter.mimeMessageToEML;
Expand Down Expand Up @@ -192,9 +194,19 @@ the proxy bridge server (or connection pool in async mode) while a non-async ema
smtpRequestsPhaser.register();
if (async) {
// start up thread pool if necessary
if (executor == null || executor.isShutdown()) {
executor = Executors.newFixedThreadPool(operationalConfig.getThreadPoolSize(),
new NamedThreadFactory("Simple Java Mail async mail sender"));
if (executor == null) {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
operationalConfig.getThreadPoolSize(),
operationalConfig.getThreadPoolSize(),
operationalConfig.getThreadPoolTimeout(),
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
new NamedThreadFactory("Simple Java Mail async mail sender")
);
if(operationalConfig.getThreadPoolTimeout() > 0) {
threadPoolExecutor.allowCoreThreadTimeOut(true);
}
executor = threadPoolExecutor;
}
configureSessionWithTimeout(session, operationalConfig.getSessionTimeout());
executor.execute(new Runnable() {
Expand Down Expand Up @@ -300,7 +312,7 @@ private void configureBounceToAddress(final Session session, final Email email)
}

/**
* We need to keep a count of running threads in case a proxyserver is running or a connection pool needs to be shut down.
* We need to keep a count of running threads in case a proxyserver is running
*/
private synchronized void checkShutDownRunningProcesses() {
smtpRequestsPhaser.arriveAndDeregister();
Expand All @@ -312,11 +324,6 @@ private synchronized void checkShutDownRunningProcesses() {
LOGGER.trace("stopping proxy bridge...");
proxyServer.stop();
}
// shutdown the threadpool, or else the Mailer will keep any JVM alive forever
// executor is only available in async mode
if (executor != null) {
executor.shutdown();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public class OperationalConfig {
*/
private final int threadPoolSize;

/**
* @see org.simplejavamail.mailer.MailerBuilder.MailerRegularBuilder#withThreadPoolTimeout(Integer)
*/
private final int threadPoolTimeout;

/**
* @see org.simplejavamail.mailer.MailerBuilder.MailerRegularBuilder#withTransportModeLoggingOnly(Boolean)
*/
Expand Down Expand Up @@ -56,6 +61,7 @@ public OperationalConfig(@Nonnull Properties properties, int sessionTimeout, int
this.properties = properties;
this.sessionTimeout = sessionTimeout;
this.threadPoolSize = threadPoolSize;
this.threadPoolTimeout = 1; // TODO wire it up to all the config files
this.transportModeLoggingOnly = transportModeLoggingOnly;
this.debugLogging = debugLogging;
this.sslHostsToTrust = Collections.unmodifiableList(sslHostsToTrust);
Expand All @@ -75,7 +81,14 @@ public int getSessionTimeout() {
public int getThreadPoolSize() {
return threadPoolSize;
}


/**
* @see org.simplejavamail.mailer.MailerBuilder.MailerRegularBuilder#withThreadPoolSize(Integer)
*/
public int getThreadPoolTimeout() {
return threadPoolTimeout;
}

/**
* @see org.simplejavamail.mailer.MailerBuilder.MailerRegularBuilder#withTransportModeLoggingOnly(Boolean)
*/
Expand Down