我的问题是关于 this answer这似乎适合我的情况(tomcat)。但是我看到它使用了 newSingleThreadScheduledExecutor() 。就我而言,必须执行的定期任务可能会持续很长时间,我想确保它在完成之前不会阻止我的网站(作为单独的线程
)。此外,我想确保我的任务 Runnable
将能够共享网站正在使用的 mySQL 连接池(通过 hibernate)。
那么,这仍然是正确的方法还是我必须使用其他方法?
最佳答案
I want to make sure that it will not block my web site until it has completed (run as a separated Thread)
HTTP 连接器线程池和分配给运行计时器任务的线程池不同。它们彼此不依赖,也不会阻止您的网站。
In addition I want to make sure that my task Runnable will be able to share mySQL connection pool (through hibernate) that the web site is using. So, is that still the correct approach or do I have to use something else?
使用类似 commons DBCP 的框架配置公共(public)连接池并在 JNDI 上查找资源。一旦您查找到该 DataSource
并且连接上的工作已终止,请将连接返回到池中。
这个方法很好。
关于java - Tomcat 中运行计划任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28542629/