-
-
Notifications
You must be signed in to change notification settings - Fork 273
Closed
Description
Hello,
In production I had this error with simple-java-mail 4.4.5:
java.util.concurrent.RejectedExecutionException: Task sendMail process rejected from java.util.concurrent.ThreadPoolExecutor@79627f99[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 3]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
at org.simplejavamail.mailer.internal.mailsender.MailSender.send(MailSender.java:196)
at org.simplejavamail.mailer.Mailer.sendMail(Mailer.java:364)
The call was: mailer.sendMail(email, true);
By looking at these classes:
- the 4.4.5 version: https://github.com/bbottema/simple-java-mail/blob/4.4.5/src/main/java/org/simplejavamail/mailer/internal/mailsender/MailSender.java
- the latest version: https://github.com/bbottema/simple-java-mail/blob/develop/modules/simple-java-mail/src/main/java/org/simplejavamail/mailer/internal/mailsender/MailSenderImpl.java
There is indeed a bug:
- in the synchronized
checkShutDownRunningProcesses
method, the pool is shutdown if there is no more mail to send, - in the synchronized
send
method, the pool is started if it is null or shutdown.
But the thing is, calling theshutdown
method on aThreadPoolExecutor
is not a synchronized operation, it just Initiates an orderly shutdown.
My main question is actually: why shutting down the pool if there is no mail to send?
In the comment before the shutdown is is written shutdown the threadpool, or else the Mailer will keep any JVM alive forever. But actually by default, the thread factory in the ThreadPoolExecutor
build non daemon threads, so it will not keep the JVM alive.