Web应用程序中的计划任务? [英] Scheduled task in a web application?

查看:101
本文介绍了Web应用程序中的计划任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

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

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.


  1. 这样做好吗?

  2. 如果没有,推荐的方法是什么?

  3. 将servlet用于这样的事情是否正确?有什么替代方案?

首先回答后注意:我不是在寻找解决时序或并发问题的方法,因为我可以轻松处理两者。我只需要知道如何在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.

推荐答案

Quartz是你最好的选择,并且最具可配置性。它具有基于CRON的接口或更动态的方式来生成相对于特定事件的作业,如果您的用例需要它,Quartz可以这样做。它能够将作业保存到数据库中,以便它们能够在重新启动后继续存在。

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.

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

在web.xml中进行配置,以便自动启动它:

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

  <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> 

这篇关于Web应用程序中的计划任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆