如何在长时间关闭期间处理servlet请求 [英] How to process servlet requests during long shutdown

查看:112
本文介绍了如何在长时间关闭期间处理servlet请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要在我们的Servlet应用程序中实现一个优雅的关闭机制。

We need to implement a graceful shutdown mechanism into our Servlet application.

编辑:我们希望尽可能简化,这将处理发送的kill信号通过操作系统的功能。这将允许系统管理员使用内置的shell实用程序(在Windows上执行kill或taskkill),否则他们必须安装另一个实用程序才能与服务器对话。

We want to make it simple as possible, which would be handling a kill signal sent via operating system's function. This would allow system admins to use built in shell utilities (kill or taskkill on Windows), otherwise they would have to install another utility just to "talk" with server.

此机制分两个阶段进行:

This mechanism works in two phases:


  1. 关闭请求时,拒绝某些关键活动

  2. 阻止直到以前发起的关键行动已经完成;这些可能需要几个小时

阶段#1在我们的DAO层中实现。
阶段#2在我们的ServletContextListener#contextDestroyed方法中实现

Phase #1 is implemented in our DAO layer. Phase #2 is implemented in our ServletContextListener#contextDestroyed method

我们的问题是,一旦调用了contextDestroyed,Servlet容器就会停止为进一步的HTTP请求提供服务。

Our problem is that once contextDestroyed is called the Servlet container stops servicing further HTTP requests.

编辑:当有人在服务器进程上调用操作系统的kill函数时调用contextDestroyed。

contextDestroyed is called when someone is calling the operating system's kill function on server's process.

我们想让阶段#2期间应用程序处于活动状态,通知用户某些活动不可用。

We would like to let the application alive during Phase #2, notifying the users that some activities are unavailable.

推荐答案

使用过滤器以保留所有关键请求的列表。

Use a filter to keep a list of all critical requests.

当收到准备关闭请求时,过滤器应该开始拒绝一些请求。

When the "prepare shutdown" request is received, the filter should start denying some requests.

编写一个servlet,告诉你队列中还剩下多少关键作业。

Write a servlet that tells you how many critical jobs are still left in the queue.

在关机工具中,发送准备关机。对servlet进行轮询以获取关键作业的数量。当它达到0时,发送实际的shutdown命令。

In the shutdown tool, send the "prepare shutdown". The poll the servlet for the number of critical jobs. When this reaches 0, send the actual shutdown command.

要实现这一点,请在业务层中创建一个服务于此的服务。请注意,所有内容都必须在之前发生 <$ em $ c> contextDestroyed()!您的特殊应用程序关闭不适合世界的J2EE视图,因此您必须自己管理它。

To make this happen, create a service in the business layer which orchestrates this. Note that everything must happen before contextDestroyed() is being called! Your special application shutdown doesn't fit into the J2EE view of the world, so you have to manage it yourself.

该服务应该能够告诉感兴趣的各方何时正在关闭,有多少关键作业仍在运行等等。然后,Servlet和过滤器可以使用此服务拒绝请求或告知剩余的作业数量。

The service should be able to tell interested parties when a shutdown is in progress, how many critical jobs are still running, etc. Servlets and filters can then use this service to deny requests or tell how many jobs are left.

所有工作都完成了,拒绝所有请求,除了访问关闭信息servlet,然后告诉应用程序现在已经准备好死亡。

When all jobs are done, deny all requests except access to the "shutdown info" servlet which should then tell that the app is now ready for death.

编写一个工具给出管理员有一个很好的用户界面来启动你的应用程序的关闭。

Write a tool which gives the administrators a nice UI to initiate shutdown of your app.

你可能会想要阻止操作系统关闭你的应用程序。 不要这样做。

You may feel tempted to prevent the OS from shutting down your application. Don't do that.

您应该做的是编写一个特殊的工具来使用我描述的两阶段流程来关闭您的应用程序以上。这应该是关闭的标准方法。

What you should do is write a special tool to shut down your application using the two phase process that I described above. This should be the standard way to shutdown.

是的,管理员会抱怨它。在Unix上,您可以通过将其放入init脚本来隐藏此工具,因此没有人会注意到。在Windows上可能有类似的解决方案。

Yes, administrators will complain about it. On Unix, you can hide this tool by putting it into the init script, so no one will notice. There might be a similar solution on Windows.

如果出现(非)预期的情况,应该始终能够停止服务器,例如:你的错误关机代码,电源故障期间的紧急关机,应用程序代码中的错误或Murphy发生时。

Killing the server should always be possible to be able to stop it in case of (un)expected circumstances like: bugs in your shutdown code, emergency shutdown during power failure, bugs in your application code, or when Murphy happens.

这篇关于如何在长时间关闭期间处理servlet请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆