gpt4 book ai didi

java - 在 Tomcat 关闭之前触发一个操作来执行持久性提交

转载 作者:行者123 更新时间:2023-11-28 23:08:18 24 4
gpt4 key购买 nike

我们最近更改了系统,将最新数据保存在内存中,而不是写入数据库。 (在I/O比较空闲的时候会写入数据库)

但是,如果在需要关闭的情况下,现在我们会卡住,可能有一些数据尚未写入数据库。

当 tomcat 出现“关闭”问题时,Tomcat 中是否有触发调用 servlet 方法或某种执行操作的方法?我们正在使用以下设置:

操作系统:Windows Server 2008

Tomcat :5.5.30

知道如何执行上述操作吗?

注意:此时,我们从 windows 服务关闭 tomcat。

最佳答案

就像其他人指出的那样,看起来像 Servlet.destroy()可能是你想要的。

public void destroy()

Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. This method is only called once all threads within the servlet's service method have exited or after a timeout period has passed. After the servlet container calls this method, it will not call the service method again on this servlet.

This method gives the servlet an opportunity to clean up any resources that are being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the servlet's current state in memory.

这是否符合您的要求?您还可以实现 context listener :

Handling Servlet Life-Cycle Events

You can monitor and react to events in a servlet's life cycle by defining listener objects whose methods get invoked when life cycle events occur. To use these listener objects, you must define the listener class and specify the listener class.

那里也有例子。

关于java - 在 Tomcat 关闭之前触发一个操作来执行持久性提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3667719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com