Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Watching for File Existence

Reply
Thread Tools

Watching for File Existence

 
 
spacerobots@gmail.com
Guest
Posts: n/a
 
      04-30-2007
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

 
Reply With Quote
 
 
 
 
Robert Mark Bram
Guest
Posts: n/a
 
      05-01-2007
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


 
Reply With Quote
 
 
 
 
Matt Humphrey
Guest
Posts: n/a
 
      05-01-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
| 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 http://www.velocityreviews.com/forums/(E-Mail Removed) http://www.iviz.com/


 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      05-01-2007
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.

--
Lew
 
Reply With Quote
 
spacerobots@gmail.com
Guest
Posts: n/a
 
      05-01-2007
On Apr 30, 5:20 pm, Robert Mark Bram <(E-Mail Removed)> wrote:
> 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!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
distutils - script install check file existence before copy? bwooster47@gmail.com Python 0 03-04-2007 03:22 AM
what is the quickest way to check if I have write permissions to and existence of the file puzzlecracker C++ 4 10-31-2005 12:45 AM
An Automated process of watching a network file folder, reading a file in it and deleting the file using ASP.NET ? Luis Esteban Valencia Muņoz ASP .Net 3 06-04-2005 10:56 AM
Checking fprintf for errors and testing for existence of a file Jim Hunter C Programming 3 03-06-2005 05:29 PM
Checkup: better way to test for file existence? Matt C++ 14 02-22-2005 12:15 PM



Advertisments