1

I notice that when sending error emails using log4j, there's a pause in the program's execution as the SMTP protocol negotiates and commits.

What is the recommended way to make the sending of error emails asynchronous?

Does this have implications if an error is fatal to the process? I don't want the process to die before the email is sent, in the case of a terminal exception.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
  • 1
    For mail sending you should use multithreaded environment – Bhavik Ambani Dec 18 '12 at 11:03
  • Personally, [this example](http://stackoverflow.com/questions/4306212/use-log4j-to-send-email-reports) helped me. :) – Georgian Dec 18 '12 at 11:14
  • @BhavikAmbani, do you mean that I should spawn a new thread in order to call `_log.error(...)`. If so, that is a terrible idea! – Drew Noakes Dec 18 '12 at 11:31
  • @GGeorge, you'll notice that I've added an answer to that question :) There's no mention of asynchronous emails on that question at all. – Drew Noakes Dec 18 '12 at 11:32
  • Not at all rather it will be more convinient to you, condition that your performence can be improved. – Bhavik Ambani Dec 18 '12 at 11:33
  • @BhavikAmbani, what if a batch of tasks result in many failures. Imagine 1000 new threads being started immediately in order to send emails. Some platforms allocate upwards of 1MB per thread. A naive approach would cause an OOM exception. I could add a queue and producer/consumer, but surely this exists in the framework already. – Drew Noakes Dec 18 '12 at 11:35
  • You can use LinkedBlockedQue and all that for the memory and scheduling – Bhavik Ambani Dec 18 '12 at 11:36
  • @BhavikAmbani, cheers but the framework does support this, as I expected. – Drew Noakes Dec 18 '12 at 11:38
  • @DrewNoakes Obviously that will support that, if you want then I can paste some code with answer, but the condition is that you have to accept it :P – Bhavik Ambani Dec 18 '12 at 11:41
  • @BhavikAmbani, that seems an unreasonable request. If you want to answer, answer. If the answer is relevant, correct and useful, I will accept it. – Drew Noakes Dec 18 '12 at 11:43
  • @DrewNoakes Just joking relax – Bhavik Ambani Dec 18 '12 at 11:44

2 Answers2

3

The approach seems to be:

  1. Create your email appender, as usual
  2. 'Wrap' it with an AsyncAppender

The configuration would resemble:

<appender name="SmtpAppender" class="org.apache.log4j.net.SMTPAppender">
    <!-- ... -->
</appender>

<appender name="AsyncSmtpAppender" class="org.apache.log4j.AsyncAppender">
    <appender-ref ref="MAIL"/>
</appender>

However this doesn't address my concern about failing to send emails on fatal error conditions that bring down the process. Can someone please clarify?

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
0

If you can switch logging frameworks, logback sends email asynchronously (http://logback.qos.ch/manual/appenders.html#SMTPAppender) so there's no need for an async wrapper.

tobym
  • 1,354
  • 1
  • 10
  • 11