运行 Tomcat 中的计划任务

Running scheduled task in Tomcat

我的问题是关于 this answer 这里似乎适用于我的情况 (tomcat)。但是我看到它使用 newSingleThreadScheduledExecutor()。在我的例子中,必须执行的周期性任务可能会持续很长时间,我想确保它 不会 阻止我的网站,直到它完成(运行 作为一个分开的 Thread)。此外,我想确保我的任务 Runnable 能够共享网站正在使用的 mySQL 连接池(通过休眠)。 那么,这仍然是正确的方法还是我必须使用其他方法?

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这样的框架配置一个公共连接池,并在JNDI 上查找资源。查找 DataSource 并且连接工作终止后,return 连接返回池。

这个方法很好。