|
25 | 25 | import java.util.concurrent.ExecutorService;
|
26 | 26 | import java.util.concurrent.Executors;
|
27 | 27 | import java.util.concurrent.Phaser;
|
28 |
| -import java.util.concurrent.ThreadFactory; |
29 |
| -import java.util.concurrent.atomic.AtomicInteger; |
30 | 28 |
|
31 | 29 | import static java.lang.String.format;
|
32 | 30 | import static org.simplejavamail.converter.EmailConverter.mimeMessageToEML;
|
@@ -72,7 +70,7 @@ public class MailSender {
|
72 | 70 | * Only set when {@link ProxyConfig} is provided with authentication details.
|
73 | 71 | */
|
74 | 72 | @Nullable
|
75 |
| - private AnonymousSocks5Server proxyServer = null; |
| 73 | + private AnonymousSocks5Server proxyServer; |
76 | 74 |
|
77 | 75 | /**
|
78 | 76 | * Allows us to manage how many thread we run at the same time using a thread pool.
|
@@ -194,21 +192,9 @@ the proxy bridge server (or connection pool in async mode) while a non-async ema
|
194 | 192 | smtpRequestsPhaser.register();
|
195 | 193 | if (async) {
|
196 | 194 | // start up thread pool if necessary
|
197 |
| - if (executor == null) { |
198 |
| - executor = Executors.newFixedThreadPool(operationalConfig.getThreadPoolSize(), new ThreadFactory() { |
199 |
| - final AtomicInteger threadCounter = new AtomicInteger(0); |
200 |
| - @Override |
201 |
| - public Thread newThread(Runnable r) { |
202 |
| - Thread thread = new Thread(r, "Simple Java Mail async mail sender #" + threadCounter.getAndIncrement()); |
203 |
| - if (!thread.isDaemon()) { |
204 |
| - thread.setDaemon(true); |
205 |
| - } |
206 |
| - if (thread.getPriority() != Thread.NORM_PRIORITY) { |
207 |
| - thread.setPriority(Thread.NORM_PRIORITY); |
208 |
| - } |
209 |
| - return thread; |
210 |
| - } |
211 |
| - }); |
| 195 | + if (executor == null || executor.isShutdown()) { |
| 196 | + executor = Executors.newFixedThreadPool(operationalConfig.getThreadPoolSize(), |
| 197 | + new NamedThreadFactory("Simple Java Mail async mail sender")); |
212 | 198 | }
|
213 | 199 | configureSessionWithTimeout(session, operationalConfig.getSessionTimeout());
|
214 | 200 | executor.execute(new Runnable() {
|
@@ -269,7 +255,6 @@ private void sendMailClosure(@Nonnull final Session session, @Nonnull final Emai
|
269 | 255 |
|
270 | 256 | try {
|
271 | 257 | synchronized (this) {
|
272 |
| - //noinspection ConstantConditions |
273 | 258 | if (needsAuthenticatedProxy() && !proxyServer.isRunning()) {
|
274 | 259 | LOGGER.trace("starting proxy bridge");
|
275 | 260 | proxyServer.start();
|
@@ -319,19 +304,23 @@ private void configureBounceToAddress(final Session session, final Email email)
|
319 | 304 | }
|
320 | 305 |
|
321 | 306 | /**
|
322 |
| - * We need to keep a count of running threads in case a proxyserver is running |
| 307 | + * We need to keep a count of running threads in case a proxyserver is running or a connection pool needs to be shut down. |
323 | 308 | */
|
324 | 309 | private synchronized void checkShutDownRunningProcesses() {
|
325 | 310 | smtpRequestsPhaser.arriveAndDeregister();
|
326 | 311 | LOGGER.trace("SMTP request threads left: {}", smtpRequestsPhaser.getUnarrivedParties());
|
327 | 312 | // if this thread is the last one finishing
|
328 | 313 | if (smtpRequestsPhaser.getUnarrivedParties() == 0) {
|
329 | 314 | LOGGER.trace("all threads have finished processing");
|
330 |
| - //noinspection ConstantConditions |
331 | 315 | if (needsAuthenticatedProxy() && proxyServer.isRunning() && !proxyServer.isStopping()) {
|
332 | 316 | LOGGER.trace("stopping proxy bridge...");
|
333 | 317 | proxyServer.stop();
|
334 | 318 | }
|
| 319 | + // shutdown the threadpool, or else the Mailer will keep any JVM alive forever |
| 320 | + // executor is only available in async mode |
| 321 | + if (executor != null) { |
| 322 | + executor.shutdown(); |
| 323 | + } |
335 | 324 | }
|
336 | 325 | }
|
337 | 326 |
|
@@ -389,7 +378,6 @@ public synchronized void testConnection() {
|
389 | 378 | logSession(session, "connection test");
|
390 | 379 |
|
391 | 380 | try (Transport transport = session.getTransport()) {
|
392 |
| - //noinspection ConstantConditions |
393 | 381 | if (needsAuthenticatedProxy() && !proxyServer.isRunning()) {
|
394 | 382 | LOGGER.trace("starting proxy bridge for testing connection");
|
395 | 383 | proxyServer.start();
|
|
0 commit comments