views:

71

answers:

2

Hi all, Previously I asked this question: http://stackoverflow.com/questions/2941657/read-quicktime-movie-from-servlet-in-a-webpage Basically I used a servlet to stream a movie file to a quicktime plugin on browser (In order to play file outside the root). At the end Ryan Fernandes asked me the question regarding concurrency issue. I have not really got the issue here.

  • WHat's the major difference between using a servlet to stream the file and the convetional method Anyway, the server still have to stream the file to the client? If not, how actually the server streams file to the plugin in the later case?

  • Ryan also asked about performance with the use of servlet. Cause it uses up lots of memory? When we use backing bean or jsf, jsp (based on servlet), if many users access at the same time, the number of instances created is huge. It also needs lots of memory, how do we address it?

Please help me to address these questions. And if you have any good article regarding those issues: concurrency, multithread, performance can share with me, I am a novice. Thank you very much. Regards K.

A: 
  • Web servers are built to serve files. That is the main function of the web server. It is optimized to do that. You have to ask yourself: why not just put this filename.mov file somewhere on the server and let the server stream it? No java at all, just http://myserver.com/static/filename.mov. Of course, this is not always an option, but I guess Ryan was asking you if you have considered it at all.

  • I do not see any problem with concurrency in your code. All variables are local, every thread will get its own input and output streams. But same argument applies here: why not just let web server handle it? They suppose to be good in serving same file to multiple clients.

Georgy Bolyuba
+2  A: 

Well, If you mean this comment:

how many users are you expecting this servlet to concurrently service? Are you sure the I/O you've got going with creating the Fileinputstream is going to work for more than a few concurrent requests?

Then what he meant was that there could be trouble if you try opening the same file with 100 separate threads manually.

How webservers handle that situation should not really trouble you, as they can implement it any way they want it.

As about the main question, here is what I found in the javadoc:

class FileInputStream
public FileChannel getChannel()

Returns the unique FileChannel object associated with this file input stream.

class FileChannel

File channels are safe for use by multiple concurrent threads. The close method may be invoked at any time, as specified by the Channel interface. Only one operation that involves the channel's position or can change its file's size may be in progress at any given time; attempts to initiate a second such operation while the first is still in progress will block until the first operation completes. Other operations, in particular those that take an explicit position, may proceed concurrently; whether they in fact do so is dependent upon the underlying implementation and is therefore unspecified.

Max
Hi Max, so the problem here is to manage reading file concurrency, not with the use of servlet right?
Khue Vu
Yes, the comment you pointed to was referring to the file read concurrency problem. And it is perfectly solved using the FileInputStream, as you can see yourself from the javadoc.
Max
Thanks Max. You're right. I found that servlet is capable of handling concurrent requests
Khue Vu