Java Web 应用程序中的计划任务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4127434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:54:03  来源:igfitidea点击:

Scheduled task in a web application?

javatomcatservletsscheduled-taskstomcat6

提问by Bart van Heukelom

I'm building a statistics apps for an online game, built using the servlet API in Java (to be deployed on Tomcat). It's easy enough to let the game send a message to the stats server every time a user logs in, because handling requests is what Servlets/Tomcat are for.

我正在为在线游戏构建一个统计应用程序,使用 Java 中的 servlet API 构建(将部署在 Tomcat 上)。让游戏在用户每次登录时向统计服务器发送一条消息是很容易的,因为处理请求是 Servlets/Tomcat 的用途。

I also need to periodically initiate requests on the stats server though, for example to retrieve the number of online users from the game server or the number of fans from our Facebook page.

不过,我还需要定期在统计服务器上发起请求,例如从游戏服务器检索在线用户数量或从我们的 Facebook 页面检索粉丝数量。

It would be easy to just start a thread in the app's main servlet and let that do the data retrieval once in a while, but it feels a bit strange because all other threads are created by Tomcat.

在应用程序的主 servlet 中启动一个线程并让它偶尔进行数据检索会很容易,但感觉有点奇怪,因为所有其他线程都是由 Tomcat 创建的。

  1. Is doing it like that ok?
  2. If not, what is the recommended way of doing this?
  3. Is it even correct to use servlets for something like this? What's the alternative?
  1. 这样做好吗?
  2. 如果没有,推荐的这样做的方法是什么?
  3. 将 servlet 用于这样的事情是否正确?什么是替代方案?

Note after first answers: I'm not looking for a solution to the problem of timing or concurrency, because I can easily handle both. I just need to know how to properly start a pro-active process in a servlet container.

第一个答案后请注意:我不是在寻找时间或并发问题的解决方案,因为我可以轻松处理两者。我只需要知道如何在 servlet 容器中正确启动主动进程。

回答by vaskin

Quartz is your best bet, and most highly configurable. It has CRON based interface or a more dynamic way to generate jobs that are relative from a specific event, if your use case calls for it Quartz can do it. It has the ability to persist jobs to the database so they can survive restarts.

Quartz 是您最好的选择,也是高度可配置的。它具有基于 CRON 的接口或更动态的方式来生成与特定事件相关的作业,如果您的用例需要它,Quartz 可以做到。它具有将作业持久化到数据库的能力,这样它们就可以在重启后继续存在。

http://www.quartz-scheduler.org/

http://www.quartz-scheduler.org/

Make configurations in web.xml like this to auto-start it:

像这样在 web.xml 中进行配置以自动启动它:

  <servlet> 
    <servlet-name>QuartzInitializer</servlet-name>
    <display-name>Quartz Initializer Servlet</display-name>
    <servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
    <load-on-startup>1</load-on-startup>

    <init-param>
      <param-name>shutdown-on-unload</param-name>
      <param-value>true</param-value>
    </init-param>

    <init-param>
      <param-name>start-scheduler-on-load</param-name>
      <param-value>true</param-value>
    </init-param>

  </servlet> 

回答by Brian Clozel

You should consider:

你应该考虑:

Don't bother reinventing the wheel, Quartz and other products already handle Threads/timeouts/concurrency issues for you!

不要费心重新发明轮子,Quartz 和其他产品已经为您处理线程/超时/并发问题!