Salut,
Cam Bazz wrote:
> Hello,
>
>
> How can one detect if a connection has been closed from servlet side?
>
> Namely, I have a file upload servlet. You upload an image, and the
> image is processed tru imagemagick, only after the image has been
> processed the servlet returns a success message.
>
> if the user presses the cancel button, which calls xhr.abort how can i
> detect this from the servlet side? it appears that regular servlets
> can not do it, so maybe a comet servlet can do?
I think regular Servlet can do it if they block on an inputStream.read()
as this method will return -1 when the client close the connection. But
it means a thread is blocked waiting for bytes.
>
> Jean Francois Arcand told me previously that onInterrupt method can be
> used to detect if connection is aborted.
>
> Now: how can I make a hybrid of a comet servlet and a regular file
> upload servlet, that will can detect user pressing the cancel button?
> Within a regular servlet, the file upload logic can detect that the
> upload is not complete, thus rollback changes - but if the user
> presses the cancel button, while image magick is processing, there is
> no way to detect this and thus rollback.
You need to register your CometHandler to get notified asynchronously
when bytes are available. Mainly, you need to (assuming you know how
Comet works):
(1) Invoke CometContext.addCometHandler(..)
https://grizzly.dev.java.net/nonav/apidocs/com/sun/grizzly/comet/CometContext.html#addCometHandler(com.sun.grizzly.comet.CometHandler)
(2) CometContext.registerAsyncWrite(YourCometHandler);
https://grizzly.dev.java.net/nonav/apidocs/com/sun/grizzly/comet/CometContext.html#registerAsyncRead(com.sun.grizzly.comet.CometHandler)
Then your CometHandler.onEvent() will be invoked with a CometEvent of
type READ, which will means bytes are available to be read using the
CometReader passed to you via CometEvent.attachment(). If the client
close the connection, your CometHandler.onInterrupt(...) will be invoked.
Let me try to day to write a demo that demonstrate the concept. At least
by the end of the week.
A+
-- Jeanfrancois
>
> Best.
> -C.B.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe_at_grizzly.dev.java.net
> For additional commands, e-mail: dev-help_at_grizzly.dev.java.net
>