halt servlet response
I'm facing the following problem. i have a servlet that serves a client request with a video clip. But this video clip is a product of another thread (transcoder). If the clip is not ready to be downloaded because the transcoder thread haven't finished its job, the client request fails! Any suggestions about how to deal with this case? how can i halt the response of the servlet until the transcoded clip is ready by the thread?
开发者_高级运维Thanks in advance! Antonis
The most straight-forward thing to do here would be to use a Future. Submit request to the transcoder, and let it return a Future
immediately. Then the HTTP thread can block on this future calling get
until the video is ready.
Joining doesn't sound like a good option to me. Thread#join blocks until the target thread terminates, but whether a thread terminates after doing a job is implementation detail. For example, if the transcoder would use a cached thread pool, the app. breaks.
You can delay the execution of the servlet with the regular means (sleep, wait, join, Future#get).
In your case, it sounds like you want to join the Thread (or get the Future) that transcodes the video.
However, you should really only do this if the time you need to block is just a few seconds. Otherwise, the client browser might time out the request, or the user experience is just bad. If the transcoding work takes longer, consider outputting something like a progress bar, that keeps polling the server until the task is finished and only then tries to load the video clip.
精彩评论