2323import java .util .List ;
2424import java .util .Properties ;
2525import java .util .concurrent .ExecutorService ;
26- import java .util .concurrent .Executors ;
26+ import java .util .concurrent .LinkedBlockingQueue ;
2727import java .util .concurrent .Phaser ;
28+ import java .util .concurrent .ThreadPoolExecutor ;
29+ import java .util .concurrent .TimeUnit ;
2830
2931import static java .lang .String .format ;
3032import 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
0 commit comments