Re: deleteOnExit() doesn't work in Tomcat on temp files
Noel J. Bergman wrote: Instead, use the PhantomReference-based code that I contributed to Jakarta Commons. The main reason why people (misguidedly) use deleteOnExit() is to be able to generate temporary files that you can return URLs for (e.g. you generate a .PDF report, and generate an HTTP redirect to /foobar.pdf). And of course, have those files be cleaned up later. You want to wait for a little while before deleting the file (5 or 10 minutes, or some other configurable timeout), to allow the user to do things like reload the URL. Also, some browsers (Mozilla in particular) reload the URL when you Print a page. I'm not sure a reference-based cleanup would be appropriate, unless it's possible to somehow force a reference to be kept for such a period, and then magically cleaned up. This is why I suggested an explicit cleanup thread where you could register the files you have generated, and have it clean them up after a few minutes. I know it's bad to start up threads like this, but in the absence of anything better (that I know of), ... - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
deleteOnExit() doesn't work in Tomcat on temp files
I've run into a bug that I only experience in Tomcat. If, within a servlet, I create a temporary file using File.createTempFile(...), Tomcat puts the file in its temp directory, which is fine. However, if I call deleteOnExit() on the file, the file doesn't get deleted when Tomcat is shut down. This happens even when the temporary is never opened. I am using Tomcat 5.5 on WindowsXP. Tomcat is installed as a service, and I am stopped and restarting it using the monitor program. I also tried rebooting my machine (to make sure Java had exited) but the files were still there. Has anyone else run into this? Is there a workaround? Thanks, --Paul Lynch p.s. Here's my test servlet's code: // TempFileTest import java.util.*; import java.io.*; import javax.servlet.http.*; /** * This class is a servlet used to test the problem with File.deleteOnExit() */ public class TempFileTest extends HttpServlet { /** * This method should be called automatically when the servlet is * initialized. */ public void init() { try { File f = File.createTempFile(TEMP_FILE_TEST, null); f.deleteOnExit(); } catch (Exception e) { System.out.println(TempFileTest could not create the temp file!); e.printStackTrace(); } } // init } // TempFileTest -- Mailblocks - A Better Way to Do Email http://about.mailblocks.com/info - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: deleteOnExit() doesn't work in Tomcat on temp files
[EMAIL PROTECTED] wrote: I am using Tomcat 5.5 on WindowsXP. Tomcat is installed as a service, Somehow when Tomcat is installed as a Windows service, the shutdown doesn't go through the orderly steps (i.e. servlet destroy()s aren't called, and I suspect the process is just *killed* instead of being made to exit normally). You may want to have a separate mechanism to delete these temp files instead of waiting for the tomcat exit, anyway. If your Tomcat process lives a long time, you may run out of temp file space - you should have a separate thread or something, and queue up your files to be deleted by that thread, based on some aging criteria.. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: deleteOnExit() doesn't work in Tomcat on temp files
DO NOT EVER use deleteOnExit(). Especially not in something like a web app, which has an indeterminate but generally long lifetime. Consider the API removed from Java. Period. Instead, use the PhantomReference-based code that I contributed to Jakarta Commons. (A) it works. (B) it cleans up during runtime, rather than whenever the JVM should happen to exit cleanly through the right path. (C) It doesn't leak memory like a pig. --- Noel - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]