Solved

Servlet has wait for file to be generated, but how? thread?

Posted on 2003-11-21
8
391 Views
Last Modified: 2010-04-01
hi, i have the following problem:
I´m using a servlet to provide downloads.
The files that the user wants to download are generated on the fly.
So, the user klickes a link to the servlet,
the servlet calls some functions to generate the file.
when the file is ready, i get a <filename>.go file.
in the servlet i wait for that file to appear.
i do that by:
        File gofile = new File(<pathtofile>);
        while (!gofile.exists())
        {
        }
thats not good i know, but it works. :-)
I tried to use sleep and wait functions, but i get something like: you are not the owner of the thread.
How can i make an servlet to run in an own thread that i can set to sleep(<sometime>) to check if the file exists every second without blocking the whole system.

by the way is there a way to reload the page that called the servlet?

thx, for any solutions or hints.

0
Comment
Question by:Koma666
  • 5
  • 3
8 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 125 total points
ID: 9796184
> How can i make an servlet to run in an own thread that i can set to sleep(<sometime>) to check if the file exists every second without blocking the whole system.

Try:

final File gofile = new File( "thefile.txt" ) ;
Thread checker = new Thread()
                          {
                             public void run()
                             {
                                 while( !gofile.exists() )
                                 {
                                    try { Thread.sleep( 1000 ) ; } catch( InterruptedException ex ) { }
                                 }
                             }
                          } ;
checker.start() ;
checker.join() ;
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9796191
> by the way is there a way to reload the page that called the servlet?

Not sure what you mean....what is the situation you want this to happen?
0
 
LVL 5

Author Comment

by:Koma666
ID: 9796225
>> by the way is there a way to reload the page that called the servlet?
>Not sure what you mean....what is the situation you want this to happen?

the user is on a page with the link to the download servlet
he klick the link.
lets say href='servlet/download?file=something'
so no new HTML page is opened, and i cant reload by something like Javascript:opener.location.reload();
the new page has application/octet-stream in the header.

the servlet waits for the file, then sends the file to the user.
after sending is complete the servlet deletes the file on the server.
after that i set the last download time.

on the page with the servlet link the last download time is shown, so i have to reload the page, because the time has changed.

i hope i explained it right. :-)

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 35

Expert Comment

by:TimYates
ID: 9796245
Can you write the file to the outputstream, then do a:

response.sendRedirect() ?

Not sure if you can...
0
 
LVL 5

Author Comment

by:Koma666
ID: 9796260
no, that dosn´t work.
0
 
LVL 5

Author Comment

by:Koma666
ID: 9796296
but the thread thing was exactly what i needed.
thx for the fast help.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9796298
Hmmmm...   Yeah...IllegalState...

I think you are going to have to use some javascript jiggery-pokery to get this working...

can you have the link for the download pop-up a window (that does the download), and then use javascript to refresh the current page?

if you increment the counter before all of the Thread stuff, then the counter should have increased?

Tim
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9796300
>  but the thread thing was exactly what i needed.

Glad I could help with the main problem :-)

Good luck!
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How do we balance the user experience (UX) with reasonable security measures? It can be done, if you keep these fundamentals in mind.
Each year, investment in cloud platforms grows more than 20% (https://www.immun.io/hubfs/Immunio_2016/Content/Marketing/Cloud-Security-Report-2016.pdf?submissionGuid=a8d80a00-6fee-4b85-81db-a4e28f681762) as an increasing number of companies begin to…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question