如何在基于servlet的Web应用程序中运行后台任务? [英] How to run a background task in a servlet based web application?

查看:318
本文介绍了如何在基于servlet的Web应用程序中运行后台任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java,我希望在我的应用程序中继续运行servlet,但我不知道该怎么做。我的servlet有一个方法,它每天从数据库中提供用户的计数以及整个数据库中用户的总数。所以我想保持servlet不断运行。

I'm using Java and I want to keep a servlet continuously running in my application, but I'm not getting how to do it. My servlet has a method which gives counts of the user from a database on a daily basis as well as the total count of the users from the whole database. So I want to keep the servlet continuously running for that.

推荐答案

你的问题是你误解了 servlet的。它的目的是对HTTP请求采取行动,仅此而已。您只需要一个每天运行一次的后台任务。

Your problem is that you misunderstand the purpose of the servlet. It's intented to act on HTTP requests, nothing more. You want just a background task which runs once on daily basis.

如果您的环境恰好支持EJB(例如WildFly,JBoss AS / EAP,TomEE,GlassFish等),然后使用 @Schedule 而不是。

If your environment happen to support EJB (e.g. WildFly, JBoss AS/EAP, TomEE, GlassFish, etc), then use @Schedule instead.

@Singleton
public class BackgroundJobManager {

    @Schedule(hour="0", minute="0", second="0", persistent=false)
    public void someDailyJob() {
        // Do your job here which should run every start of day.
    }

    @Schedule(hour="*/1", minute="0", second="0", persistent=false)
    public void someHourlyJob() {
        // Do your job here which should run every hour of day.
    }

    @Schedule(hour="*", minute="*/15", second="0", persistent=false)
    public void someQuarterlyJob() {
        // Do your job here which should run every 15 minute of hour.
    }

} 

是的,这就是全部。容器将自动拾取和管理它。

Yes, that's really all. The container will automatically pickup and manage it.

如果您的环境不支持EJB(即不是真正的Java EE服务器,例如Tomcat, Jetty等),使用 ScheduledExecutorService的 。这可以通过 ServletContextListener 。这是一个启动示例:

If your environment doesn't support EJB (i.e. not a real Java EE server, e.g. Tomcat, Jetty, etc), use ScheduledExecutorService. This can be initiated by a ServletContextListener. Here's a kickoff example:

@WebListener
public class BackgroundJobManager implements ServletContextListener {

    private ScheduledExecutorService scheduler;

    @Override
    public void contextInitialized(ServletContextEvent event) {
        scheduler = Executors.newSingleThreadScheduledExecutor();
        scheduler.scheduleAtFixedRate(new SomeDailyJob(), 0, 1, TimeUnit.DAYS);
        scheduler.scheduleAtFixedRate(new SomeHourlyJob(), 0, 1, TimeUnit.HOURS);
        scheduler.scheduleAtFixedRate(new SomeQuarterlyJob(), 0, 15, TimeUnit.MINUTES);
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        scheduler.shutdownNow();
    }

}

工作类看起来像这样:

public class SomeDailyJob implements Runnable {

    @Override
    public void run() {
        // Do your daily job here.
    }

}





public class SomeHourlyJob implements Runnable {

    @Override
    public void run() {
        // Do your hourly job here.
    }

}





public class SomeQuarterlyJob implements Runnable {

    @Override
    public void run() {
        // Do your quarterly job here.
    }

}



不要考虑使用 java.util.Timer / Java EE中的java.lang.Thread



永远不要在Java EE中直接使用 java.util.Timer 和/或 java.lang.Thread 。这是麻烦的秘诀。在这个与JSF相关的答案中可以找到一个详细的解释:使用计时器在JSF托管bean中为计划任务生成线程

Do not ever think about using java.util.Timer/java.lang.Thread in Java EE

Never directly use java.util.Timer and/or java.lang.Thread in Java EE. This is recipe for trouble. An elaborate explanation can be found in this JSF-related answer on the same question: Spawning threads in a JSF managed bean for scheduled tasks using a timer.

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

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