java - How to process servlet requests during long shutdown -
we need implement graceful shutdown mechanism our servlet application.
edit: want make simple possible, handling kill signal sent via operating system's function. allow system admins use built in shell utilities (kill or taskkill on windows), otherwise have install utility "talk" server.
this mechanism works in 2 phases:
- upon shutdown request, deny critical activities
- block until initiated critical actions completed; these may take several hours
phase #1 implemented in our dao layer. phase #2 implemented in our servletcontextlistener#contextdestroyed method
our problem once contextdestroyed called servlet container stops servicing further http requests.
edit: contextdestroyed called when calling operating system's kill function on server's process.
we let application alive during phase #2, notifying users activities unavailable.
use filter keep list of critical requests.
when "prepare shutdown" request received, filter should start denying requests.
write servlet tells how many critical jobs still left in queue.
in shutdown tool, send "prepare shutdown". poll servlet number of critical jobs. when reaches 0, send actual shutdown command.
to make happen, create service in business layer orchestrates this. note must happen before contextdestroyed()
being called! special application shutdown doesn't fit j2ee view of world, have manage yourself.
the service should able tell interested parties when shutdown in progress, how many critical jobs still running, etc. servlets , filters can use service deny requests or tell how many jobs left.
when jobs done, deny requests except access "shutdown info" servlet should tell app ready death.
write tool gives administrators nice ui initiate shutdown of app.
[edit] may feel tempted prevent os shutting down application. don't that.
what should write special tool shut down application using 2 phase process described above. should standard way shutdown.
yes, administrators complain it. on unix, can hide tool putting init script, no 1 notice. there might similar solution on windows.
killing server should possible able stop in case of (un)expected circumstances like: bugs in shutdown code, emergency shutdown during power failure, bugs in application code, or when murphy happens.
Comments
Post a Comment