alanwilliamson

java.io.PrintWriter not responding to flush()

Discovered a real subtle problem with our MailCatcher open source project last night.  MailCatcher  is an implementation of the Apache Mailet API, and one of the many things it does is to manage spools and outgoing emails to various hosts.  Everything worked perfectly on the Windows development machine (jdk1.5 / WindowsXP) but upon deploying to production, (jdk1.5 / Linux) all outgoing email would hang.

When transporting email via SMTP, after you send the main mail body, you terminate the message with a single <dot><cr>.  This was sent, but a response was never received from the remote mail server to say it had successfully received the mail body.

The code was pretty standard:
  PrintWriter out = new PrintWriter( inSocket.getOutputStream(), true );
...
out.println( "." );
out.flush();
At first I thought it was a problem with the remote mail server, but the same thing happenned no matter which host I sent email to.  I then ran up another instance of MailCatcher in debug mode to test the outgoing aspect.  That way I could see the incoming traffic on a different machine.  Sure enough, the remote mail server never received the <dot>.  Very bizarre. Creating the PrintWriter class with auto-flush on never made any difference either.

Finally I ditched the PrintWriter class and moved to BufferedWriter and the whole thing instantly burst into life.  I had a look around to see if anyone had experienced this, and could only find a couple of things with people talking about flushing within a Servlet, but this was more to do with the way the Servlet API works as oppose to a problem with the PrintWriter class itself.

Comments

Ravinder, can you make sure you are using the latest release:

MailCatcher Release

I have just made an update to it, nothing drastic, more around the shutdown area.

As for the logging API, yes anything is possible. Personally I am not a big fan of many of the logging libraries; too complicated and heavy weight IMHO. If you want to add it in then by all means jump in and do it, its open source after all. One thing though, I wanted to keep the logging output as close to sendmail/postfix as possible so other Linux utilities could utilise the MailCatcher output.

left by Alan Williamson — Friday, 24 November 2006 12:17 PM — web site

Is it possible for you to use any logging api instead of the direct to file as you do now? Oh btw saw you had fixed my previous reported bugs. Nice.....

left by Ravinder Singh — Friday, 24 November 2006 12:12 PM

Ravinder, thanks for that. Can you send me an email?

I am not seeing the bugs you are seeing. As for the sendMail() one you reported just there, the collection is loaded up with MailAddress objects as it should be.

thanks

left by Alan Williamson — Friday, 24 November 2006 12:09 PM — web site

another bug; The method sendMail(MimeMessage mime); in the class MailCatcherContext seems to put wrong types of objects in the collection. The collection is assumed to contain MailAddress objects, not Address instances.... Looking forward to release 1.0:)

left by Ravinder Singh — Friday, 24 November 2006 12:03 PM

oooh, i am doing some stuff with this code at the moment, and will check to make sure.

left by Alan Williamson — Thursday, 23 November 2006 2:36 PM — web site

Eh, I don't know for sure, but it seems to be a bug when you return the recipients from the MailData class. It seems to return the sender array instead of the recipient array...

left by Ravinder Singh — Thursday, 23 November 2006 2:33 PM

PrintWriter eats exceptions. Use PrintWriter.checkError() instead of flush().

left by Bob Lee — Friday, 4 August 2006 6:10 PM — web site
left by Mikael Gueck — Friday, 4 August 2006 4:43 PM

Leave Comment

please note, all comments will be moderated for spam and abuse before being publicly posted.


 

Recent JAVA posts

Latest CFML posts

Other Bits'n'Bobs


 
Site Links
Recommended Sites/Blogs