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:
- Is it too late in the destroy method to complete async requests like this?
- Can doGet be called during/after destroy is called?
- What if there is an error page mapping for 503 that requires an error dispatch?
- What if that error dispatch calls startAsync?
Mine thoughts are:
- Not too late as we need to allow an application to clean up nicely if we are shutting down.
- Hopefully not
- Oh OK maybe doGet can be called again... maybe it needs a while(!async.isEmpty()) loop?
- Kill me now!
--