java web应用程序的初始化和关闭

发布于 2024-10-06 02:42:17 字数 264 浏览 0 评论 0原文

我正在尝试实现网络应用程序的初始化和关闭。这包括初始化和关闭:

  • Hibernate (v3.6);
  • C3P0(v0.9.1.2);
  • EHCache(v2.3.0);
  • 石英(1.8.4);
  • 特定于我的网络应用程序的其他任务;

使用Tomcat 5.5.30和Java 6。我的想法是避免资源泄漏,主要是因为在开发环境中重新部署了webapp。

我应该如何实施这个?

I'm trying to implements initialization and shutdown of a webapp. That includes initialization and shutdown of:

  • Hibernate (v3.6);
  • C3P0 (v0.9.1.2);
  • EHCache (v2.3.0);
  • Quartz (1.8.4);
  • Other tasks specific to my webapp;

Using Tomcat 5.5.30 and Java 6. My idea is to avoid resource leaking, mostly because of the redeploy of the webapp in the development environment.

How should I implement this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仙女 2024-10-13 02:42:17

通常对于Web初始化和关闭,您将编写一个 ServletContextListener

执行此操作的步骤是:

  1. 编写一个实现 javax.Servlet.ServletContextListener 的类
  2. 将一个标记添加到 web.xml 部署描述符以注册您刚刚创建的类
  3. 部署您的应用程序

部署应用程序时, contextInitialized 方法将被调用。您可以在此处放置您想要的所有初始化。应用程序关闭时 contextDestroyed 方法将被调用。

Usually for Web initialization and shutdown, you will write a ServletContextListener.

The steps to do this are:

  1. Write a class that implements javax.Servlet.ServletContextListener
  2. Add a tag to web.xml deployment descriptor to register the class you've just created
  3. Deploy your application

When you deploy your application, contextInitialized method will be called. You can place all initialization you want here. On application shutdown contextDestroyed method will be called.

相权↑美人 2024-10-13 02:42:17

也可以使用 HTTP Servlet,但侦听器是更好的选择。

您必须使用 扩展一个类HttpServlet 并在 web.xml 中设置以下内容:

<servlet>
    <servlet-name>StartupServlet</servlet-name>
    <servlet-class>your.package.servlets.StartupServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

该类可以覆盖 init 和 destroy 方法。

Its also possible to use the HTTP Servlet instead but the listener is a better option.

You have to extend a class with HttpServlet and setting the following stuff to your web.xml:

<servlet>
    <servlet-name>StartupServlet</servlet-name>
    <servlet-class>your.package.servlets.StartupServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

The class can overwrite the init and the destroy method.

心的位置 2024-10-13 02:42:17

但您仍然希望以这样的方式管理您的资源:如果应用程序崩溃并且未调用正常的关闭例程,这些资源不会泄漏。

But still you want to manage your resources in such a way that they do not leak if the application crashes and normal shutdown routines are not called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文