Madhan Sundararajan Devaki wrote:I believe, you should start an infinitely running Daemon Thread (from within the Servlet) that does what you want it to do.
That would be a blatant violation of the
J2EE specification.
The HTTP protocol is not a continuous-connection client/server architecture. As Bear said, it's about get in, do the work quickly, get out. If it wasn't for the magic of HttpSessions, each HTTP request would, in fact be totally independent of every other HTTP request/response process.
HTTP servers have an inherent "loop", where the server waits for incoming requests, parses them, then passes them on to threads from the internal thread pool. The thread should run the request/response lifecycle and then be returned to the pool so that it will be free for another, later request.
If an HTTP processor thread spawns child threads, they'll end up being returned to the pool, but they will no longer have the fundamental pooled thread characteristic of being just like every other thread in the pool, which means that the entire appserver can wildly malfunction in unpredictable ways. Including having other, equally invalid requests destroy or modify the child threads at any moment.
If you code an infinite (or long-running) loop in a servlet or
JSP, that will tie up the thread, and if enough requests do that, eventually the thread pool becomes emptied and there's no more resources to handle later requests. It's also useless, since HTTP clients and servers do not converse. The HTTP request is a stream of data that comes into the server as a
unit. That unit is them passed to the servlet. The servlet writes to the output stream. However, neither the servlet nor the output stream processes can/will accept any further input from the client. It takes an entirely new HTTP request to an entirely different servlet thread to accept more input.
It is possible, and not uncommon, for an appserver to run threads where the SERVER runs long-term asynchronous processes as a client to some other server, but those are not done through the appserver's serlvet/JSP functions.