Watching for File Existence

Discussion in 'Java' started by spacerobots@gmail.com, Apr 30, 2007.

  1. Guest

    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
     
    , Apr 30, 2007
    #1
    1. Advertising

  2. 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
    :)
     
    Robert Mark Bram, May 1, 2007
    #2
    1. Advertising

  3. <> wrote in message
    news:...
    | 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.iviz.com/
     
    Matt Humphrey, May 1, 2007
    #3
  4. Lew Guest

    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
     
    Lew, May 1, 2007
    #4
  5. Guest

    On Apr 30, 5:20 pm, Robert Mark Bram <> 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!
     
    , May 1, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luis Esteban Valencia Muñoz
    Replies:
    3
    Views:
    1,421
    Scott Allen
    Jun 4, 2005
  2. Bomb Diggy
    Replies:
    11
    Views:
    6,184
    Ivan S Kirkpatrick
    Jul 3, 2003
  3. aljosa

    file parsing/watching + qt

    aljosa, May 4, 2005, in forum: Python
    Replies:
    2
    Views:
    334
    Swaroop C H
    May 4, 2005
  4. Benjamin D. LeMasurier

    watching a file

    Benjamin D. LeMasurier, Oct 1, 2004, in forum: C Programming
    Replies:
    5
    Views:
    311
    dandelion
    Oct 4, 2004
  5. Christopher Harrison

    Watching a File for Changes

    Christopher Harrison, Oct 18, 2005, in forum: C Programming
    Replies:
    8
    Views:
    332
    Netocrat
    Oct 19, 2005
Loading...

Share This Page