I have a web service which generates a bunch of files on the server (say tomcat), which get zipped up and streamed back to the client through the servlet’s OutputStream.
This part is clear. The part that I am struggling with is how to manage these files so that if two client happen to use this feature at the same time.
I can think of several possible solutions and I am not sure what is the simplest and best way to handle this and would love to hear some thoughts.
For example: I could create a directory with the time-stamp appended to the directory name e.g. tmp_123142345 and delete this folder after I’m done writing the zip file to the stream. It’s very unlikely that two people will try to download the same file in the same millisecond.
But, is there a better way?
It’s unlikely, but not impossible. You will probably want a solution that you can boast in board meeting that it’s collision free. 🙂
Have a global synchronized counter or better
AtomicLongcounter. Create the directory name like"tmp_"+ (atomicLongCounter.addAndGet(1))So each will have unique directory even if they landed at the same millisecond.