Watching for File Existence

S

spacerobots

Hi,

I'm writing a java application (a servlet to be exact) that needs to
create a PDF. The problem I'm having is that I need to wait until the
PDF creator finishes its job and then pick up the new PDF and send it
off. So I want to wait without using up too much CPU, but at the same
time without waiting a moment longer than I have to after that PDF is
ready to go.

The basic idea is that it is taking in this file via the request
object and will then invoke an external application (via command line)
to print to the default printer. The default printer is PDF writer.
I can configure my PDF writer to write the file to disk using a custom
filename. My plan is in the doPost method to take the file out of the
request, save it to disk with a unique name, and then call the command
line application to print. I can then watch a directory for a file
with <unique name>.pdf to appear. The naive approach I'm thinking of
is to call the command line print, check if the pdf exists, wait 1/2
second or 1 second, check again, wait again, check again, etc until
the file has been created.

This servlet is going to see multiple simultaneous requests. It's
running Windows Server 2003. Any thoughts?

Thanks in advance,
-Ryan
 
R

Robert Mark Bram

Hi Ryan,
object and will then invoke an external application (via command line)
to print to the default printer. The default printer is PDF writer.

If you are using an external process, you could try waiting for it to
complete.

String [] printCommandArray; // Init this with your command
Process printProcess = Runtime.getRuntime().exec(printCommandArray);
printProcess.waitFor();

Rob
:)
 
M

Matt Humphrey

| Hi,
|
| I'm writing a java application (a servlet to be exact) that needs to
| create a PDF. The problem I'm having is that I need to wait until the
| PDF creator finishes its job and then pick up the new PDF and send it
| off. So I want to wait without using up too much CPU, but at the same
| time without waiting a moment longer than I have to after that PDF is
| ready to go.
|
| The basic idea is that it is taking in this file via the request
| object and will then invoke an external application (via command line)
| to print to the default printer. The default printer is PDF writer.
| I can configure my PDF writer to write the file to disk using a custom
| filename. My plan is in the doPost method to take the file out of the
| request, save it to disk with a unique name, and then call the command
| line application to print. I can then watch a directory for a file
| with <unique name>.pdf to appear. The naive approach I'm thinking of
| is to call the command line print, check if the pdf exists, wait 1/2
| second or 1 second, check again, wait again, check again, etc until
| the file has been created.
|
| This servlet is going to see multiple simultaneous requests. It's
| running Windows Server 2003. Any thoughts?

Normally, watching a directory for a file to appear is a poor technique
because simple applications create the file and then append to it. Waiting
1/2 or 1 second may not be enough to guarantee that the program is finished
writing unless you know already that file output is atomic. You need to
wait somewhat longer than the longest possible run each time or you'll get a
corrupted file.

A much better technique is to wrap the external app in a little launch
script so that when the task is finished it renames the output file. That
is, if it's supposed to produce <unique-name>.pdf, have the PDF writer
produce <unique-name>-X.pdf and have the script rename it upon completion.
File renaming is atomic so that when your polling servlet sees the file
appear it can grab the contents immediately knowing that it is complete.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
L

Lew

spacerobots wrote
| I'm writing a java application (a servlet to be exact) that needs to
| create a PDF. The problem I'm having is that I need to wait until the
| PDF creator finishes its job and then pick up the new PDF and send it
| off. So I want to wait without using up too much CPU, but at the same
| time without waiting a moment longer than I have to after that PDF is
| ready to go.
|
| The basic idea is that it is taking in this file via the request
| object and will then invoke an external application (via command line)
| to print to the default printer. The default printer is PDF writer.
| I can configure my PDF writer to write the file to disk using a custom
| filename. My plan is in the doPost method to take the file out of the
| request, save it to disk with a unique name, and then call the command
| line application to print. I can then watch a directory for a file
| with <unique name>.pdf to appear. The naive approach I'm thinking of
| is to call the command line print, check if the pdf exists, wait 1/2
| second or 1 second, check again, wait again, check again, etc until
| the file has been created.
|
| This servlet is going to see multiple simultaneous requests. It's
| running Windows Server 2003. Any thoughts?

Better yet, build a logic module around
<http://www.lowagie.com/iText/>
to generate the PDF on the fly and send it back over the response OutputStream.

Waiting for the subroutine to finish will make your action synchronous, and
you won't need to worry about messy ol' files.
 
S

spacerobots

Hi Ryan,
object and will then invoke an external application (via command line)
to print to the default printer. The default printer is PDF writer.

If you are using an external process, you could try waiting for it to
complete.

String [] printCommandArray; // Init this with your command
Process printProcess = Runtime.getRuntime().exec(printCommandArray);
printProcess.waitFor();

Rob
:)


Thanks for your input everybody. This is perfect and I think it's the
route I'll go, the application I'm printing from doesn't have any
hooks I can really use to snap into a PDF API like iText. I could
poll as well, but it's not as nice a solution. (I looked at the
PDFCreator and it seems to generate the file in a temporary directory
while it writes into it and then copy the completed file to the
specified destination, so it should work ok).

Again, thank you all for your expert advice!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top