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 ;
28
30
29
31
import static java .lang .String .format ;
30
32
import static org .simplejavamail .converter .EmailConverter .mimeMessageToEML ;
@@ -192,14 +194,31 @@ the proxy bridge server (or connection pool in async mode) while a non-async ema
192
194
smtpRequestsPhaser .register ();
193
195
if (async ) {
194
196
// start up thread pool if necessary
195
- if (executor == null || executor .isTerminated ()) {
196
- executor = Executors .newFixedThreadPool (operationalConfig .getThreadPoolSize ());
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
+ });
197
212
}
198
213
configureSessionWithTimeout (session , operationalConfig .getSessionTimeout ());
199
214
executor .execute (new Runnable () {
200
215
@ Override
201
216
public void run () {
202
- sendMailClosure (session , email );
217
+ try {
218
+ sendMailClosure (session , email );
219
+ } catch (Exception e ) {
220
+ LOGGER .error ("Failed to send email" , e );
221
+ }
203
222
}
204
223
205
224
@ Override
@@ -300,7 +319,7 @@ private void configureBounceToAddress(final Session session, final Email email)
300
319
}
301
320
302
321
/**
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.
322
+ * We need to keep a count of running threads in case a proxyserver is running
304
323
*/
305
324
private synchronized void checkShutDownRunningProcesses () {
306
325
smtpRequestsPhaser .arriveAndDeregister ();
@@ -313,11 +332,6 @@ private synchronized void checkShutDownRunningProcesses() {
313
332
LOGGER .trace ("stopping proxy bridge..." );
314
333
proxyServer .stop ();
315
334
}
316
- // shutdown the threadpool, or else the Mailer will keep any JVM alive forever
317
- // executor is only available in async mode
318
- if (executor != null ) {
319
- executor .shutdown ();
320
- }
321
335
}
322
336
}
323
337
0 commit comments