Solved

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

Posted on 2003-11-21
8
394 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Struts tiles for layout structure along with Angular JS. 4 176
parse example 13 95
withoutString  challenge 40 216
import as existing maven project 3 37
No single Antivirus application (despite claims by manufacturers) will catch or protect you from all Virus / Malware or Spyware threats. That doesn't stop you from further protecting yourself however - and this article is to show you how.
This article summaries thoughts and ideas from two years of sustained use. It provides good reasoning to make the jump to Windows 10.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

756 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