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

Apply configurable timeouts when sending emails #85

Closed
amanteaux opened this issue Jul 10, 2017 · 3 comments
Closed

Apply configurable timeouts when sending emails #85

amanteaux opened this issue Jul 10, 2017 · 3 comments

Comments

@amanteaux
Copy link
Contributor

I will start with my use case: I encountered an OutOfMemoryError on a production server that was due to emails that were queuing.
To give an insight of what where going, here is what was contained in the heap space:
image

When the production team detected the error, they restarted the server, here is an extract of the logs:
logs_exact.txt

I think the problem is that in MailSender the executor threads were all waiting to get a connection from the SMTP server and during this time mails were added to the executor queue until the server run out of memory. So the real problem is that the SMTP server was down and no timeout are configured in SMTPTransport class.
To solve this problem, a solution would be to add a timeout in MailSender that will cancel the sending task if it takes more that X seconds/minutes like in https://stackoverflow.com/a/2759040/3790208.

Moreover, though the code is really nicely written in MailSender, I noticed 2 things that can be improved:

  • line 165: a Runnable should be provided instead of a Thread object that adds a little overhead,
  • line 159: sendMailClosure should be wrapped in a try catch to at least show the date of the exception.

I can make a pull request if that is ok.

@bbottema
Copy link
Owner

By all means, give it a shot. I'm on my phone right now, I'll give a proper reply when I get a chance.

@amanteaux
Copy link
Contributor Author

Thank you for your quick answer! I will try to put up something this week.

amanteaux added a commit to amanteaux/simple-java-mail that referenced this issue Jul 17, 2017
@bbottema bbottema added this to the 4.3.0 milestone Aug 9, 2017
bbottema added a commit that referenced this issue Aug 12, 2017
bbottema added a commit that referenced this issue Aug 12, 2017
@bbottema bbottema self-assigned this Aug 12, 2017
@bbottema bbottema changed the title Add timeout to send email asynchronously Apply configurable timeouts when sending emails Aug 12, 2017
@bbottema
Copy link
Owner

bbottema commented Aug 12, 2017

Released in 4.3.0.

Default timeout set to 1 minute. Can be set programmatically (mailer.setSessionTimeout(int)), or with property simplejavamail.defaults.sessiontimeoutmillis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants