Thanks for the reply Kevin.

Indeed, I'm writing this servlet to be used by a single administrator and was 
attempting to use local variables of the servlet the indicate if the servlet had been 
initialized (init()) with an initial request.  Then the servlet would continually call 
itself (with a different "action" parameter) reporting the status of the thread until 
it has completed successfully or an exception encountered (using 
setHeader("refresh",...)).  Once that "instance" of the servlet was completed, I 
wanted to reinitialize it, perhaps by having it destroy() itself after the final 
"progress" check.  Additional attempts to call the initial request to the servlet 
should be blocked until this has been done because this is a simple one-version 
promotion/rollback utility.  I wouldn't want multiple users promoting the same files 
at the same time; that adds a level of complexity I don't want to deal with.

With this additional information, is this still inappropriate and if so, do you have 
any other suggestions for how I could better handle this type of exclusive file copy 
routine using servlets?

P.S. The physical files are being "locked" and "unlocked" by the thread process as 
well.

|-----Original Message-----
|From: A mailing list for discussion about Sun Microsystem's Java Servlet
|API Technology. [mailto:[EMAIL PROTECTED]]On Behalf Of Kevin
|Mukhar
|Sent: Tuesday, May 02, 2000 9:32 AM
|To: [EMAIL PROTECTED]
|Subject: Re: Synchronized servlets?
|
|
|Jason Coward wrote:
|>
|> Is it appropriate to declare an HttpServlet's service() [or
|doGet()/doPost()]
|> methods synchronized so that I can block additional requests as
|long as the
|> servlet's life cycle has not been terminated (i.e. it has been
|destroy()'ed)?
|
|No, absolutely not. the only exception might be if you are writing
|this servlet
|for your own use, and it will never be called by more than one
|person (you). If
|you intend to use this servlet where it could ever be called by
|more than one
|client, then you should not synchronize service(), doGet() or doPost().
|
|The proper solution is to make your servlet thread safe. That
|generally means
|- Don't use instance variables inside methods, use only locals
|- If you use instance or class variables inside methods, make them
|read-only
|(don't allow a thread to change the variable)
|- If you must change instance or class variables, protect them with a
|synchronized block.
|- If you must change instance or class variables, protect them with a
|synchronized method.
|- If you use system resources (files, etc.), you must protect them so that
|concurrent threads do not affect each other.
|
|> I am trying to write a file copy routine that runs as a
|background thread in a
|> servlet, initiated by the first request to the servlet, and
|destroyed once the
|> thread is completed upon receipt of a progress check from the
|client which
|> initiates the servlet.  The servlet calls itself repetitively (using
|> setHeader("refresh", ...)), reporting progress until the thread
|is completed.
|>
|> Also, can a servlet destroy() itself after the the response has
|been sent?  If
|> so, how would it be accomplished?
|
|A servlet could call its destroy() method at the end of processing
|a service
|request. However that's generally not done. There's no reason to
|destroy the
|servlet. The destroy method is provided for the servlet engine to
|call, not for
|the servlet itself to call.
|
|K Mukhar
|
|___________________________________________________________________________
|To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
|of the message "signoff SERVLET-INTEREST".
|
|Archives: http://archives.java.sun.com/archives/servlet-interest.html
|Resources: http://java.sun.com/products/servlet/external-resources.html
|LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
|

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to