Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[servlet-dev] Semantics of Servlet.destroy


Hi,

Consider the following servlet:

public class TestServlet extends HttpServlet
{
Set<AsyncContext> async = new ConcurrentHashSet<>();

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
AsyncContext asyncContext = req.startAsync();
async.add(asyncContext);
}

@Override
public void destroy()
{
for (AsyncContext asyncContext : async)
{
try
{
if (asyncContext.getResponse() instanceof HttpServletResponse httpServletResponse)
httpServletResponse.sendError(503);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
finally
{
asyncContext.complete();
}
}
super.destroy();
}
}

A number of questions about this:
  1. Is it too late in the destroy method to complete async requests like this?
  2. Can doGet be called during/after destroy is called?
  3. What if there is an error page mapping for 503 that requires an error dispatch?
  4. What if that error dispatch calls startAsync?
Mine thoughts are:
  1. Not too late as we need to allow an application to clean up nicely if we are shutting down.
  2. Hopefully not
  3. Oh OK maybe doGet can be called again... maybe it needs a while(!async.isEmpty()) loop?
  4. Kill me now!
Your Thoughts?


--

Back to the top