Skip to main content

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

We addressed this with the concept of 'graceful shutdown'. Basically if the user cares about this sort of scenario they can initiate a graceful shutdown with a timeout. Once this is initiated the server will reject all new requests, and once all currently running requests are done it will start the shutdown sequence. 

I don't think there is any other sane way to handle this. If you start destroying resources while the server is still processing active requests some of those requests are going to attempt to use resources that have stopped (as generally a shutdown will also shutdown other things, like database connections). For non graceful shutdown we also add a message to the effect of 'This exception was generated after the server started shutdown' to log error messages so we don't waste time trying to debug user reports around this. 

Stuart

On Tue, 5 Dec 2023 at 08:38, Greg Wilkins via servlet-dev <servlet-dev@xxxxxxxxxxx> wrote:

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?


--
_______________________________________________
servlet-dev mailing list
servlet-dev@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/servlet-dev

Back to the top