java web应用程序的初始化和关闭
我正在尝试实现网络应用程序的初始化和关闭。这包括初始化和关闭:
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常对于Web初始化和关闭,您将编写一个 ServletContextListener。
执行此操作的步骤是:
javax.Servlet.ServletContextListener
的类web.xml
部署描述符以注册您刚刚创建的类部署应用程序时,
contextInitialized
方法将被调用。您可以在此处放置您想要的所有初始化。应用程序关闭时contextDestroyed
方法将被调用。Usually for Web initialization and shutdown, you will write a ServletContextListener.
The steps to do this are:
javax.Servlet.ServletContextListener
web.xml
deployment descriptor to register the class you've just createdWhen you deploy your application,
contextInitialized
method will be called. You can place all initialization you want here. On application shutdowncontextDestroyed
method will be called.也可以使用 HTTP Servlet,但侦听器是更好的选择。
您必须使用 扩展一个类HttpServlet 并在 web.xml 中设置以下内容:
该类可以覆盖 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:
The class can overwrite the init and the destroy method.
但您仍然希望以这样的方式管理您的资源:如果应用程序崩溃并且未调用正常的关闭例程,这些资源不会泄漏。
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.