Java – how to process servlet requests during long shutdown periods

We need to implement an elegant shutdown mechanism in servlet applications

Editor: we want to simplify as much as possible, which will deal with termination signals sent through the functions of the operating system This will allow system administrators to use the built-in shell utility (kill or taskkill on Windows), otherwise they must install another utility to "talk" to the server

The mechanism is divided into two stages:

>In case of shutdown request, reject some key activities > block until the previously initiated key actions are completed; These may take several hours

Phase #1 is implemented in our Dao layer Phase #2 is implemented in our servletcontextlistener #contextdestroyed method

Our problem is that once contextdestroyed is called, the servlet container will stop serving further HTTP requests

Edit: when someone calls the kill function of the operating system on the server process, contextdestroyed. Will be called

We want to keep the application active during the phase #2, informing users that some activities are not available

Solution

Use the filter to keep a list of all critical requests

When a "ready to close" request is received, the filter should start rejecting some requests

Write a servlet that tells you how many critical jobs remain in the queue

In the shutdown tool, send "ready to shutdown" The servlet is polled for the number of critical jobs When this value reaches 0, the actual shutdown command is sent

To achieve this, create a service that serves this in the business layer Note that everything must happen before contextdestroyed() is called! Your special application closes the J2EE view that is not suitable for the world, so you must manage it yourself

The service should be able to tell interested parties when shutdown is in progress, how many key jobs are still running, etc Servlets and filters can then use this service to reject requests or tell the number of jobs remaining

After all jobs are completed, reject all requests except access to the "shutdown information" servlet, and then tell the application that it is now ready to die

Write a tool to provide a good UI for administrators to start the shutdown of applications

[Edit] you may want to prevent the operating system from closing your application Don't do that

What you should do is write a special tool to close your application using the two-phase process I described above This should be the standard method of closing

Yes, the administrator will complain about it On UNIX, you can hide this tool by putting it into the init script, so no one will notice There may be a similar solution on windows

Killing the server should always be able to stop it under (unexpected) conditions, such as errors in shutdown code, emergency shutdown during power failure, errors in application code or when Murphy occurs

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>