在Java Web应用程序中运行常规的后台事件 [英] Running a regular background event in Java web app
问题描述
我目前的计划是当第一个用户(可能是我)进入网站时启动一个后台线程,直到等待时间(小时),然后启动事件阻止其他人(我是一个Windows程序员的交易,所以我认为事件和WaitOnMultipleObjects),直到它完成。
Jeff如何在Asp.Net中做到这一点,他的方法适用于Java网络应用程序世界?
我认为开发一个运行后台任务的自定义解决方案并不总是值得的,所以我建议使用 Java中的Quartz Scheduler
在您的情况下(需要在Web应用程序中运行后台任务)可以使用ServletContextListener在分发到在启动Web容器时初始化引擎。
之后,您有许多启动(触发)您的后台任务(作业)的可能性,例如您可以使用日历或类似cron的表达式。在您的情况下,您最可能应该通过 SimpleTrigger 可以让您以固定的时间间隔运行作业。
这些作业本身也可以在Quartz中轻松描述,但是您没有提供任何关于您的内容的详细信息需要运行,所以我不能提供这方面的建议。
In podcast #15, Jeff mentioned he twittered about how to run a regular event in the background as if it was a normal function - unfortunately I can't seem to find that through twitter. Now I need to do a similar thing and are going to throw the question to the masses.
My current plan is when the first user (probably me) enters the site it starts a background thread that waits until the alloted time (hourly on the hour) and then kicks off the event blocking the others (I am a Windows programmer by trade so I think in terms of events and WaitOnMultipleObjects) until it completes.
How did Jeff do it in Asp.Net and is his method applicable to the Java web-app world?
I think developing a custom solution for running background tasks doesn't always worth, so I recommend to use the Quartz Scheduler in Java.
In your situation (need to run background tasks in a web application) you could use the ServletContextListener included in the distribution to initialize the engine at the startup of your web container.
After that you have a number of possibilities to start (trigger) your background tasks (jobs), e.g. you can use Calendars or cron-like expressions. In your situation most probably you should settle with SimpleTrigger that lets you run jobs in fixed, regular intervals.
The jobs themselves can be described easily too in Quartz, however you haven't provided any details about what you need to run, so I can't provide a suggestion in that area.
这篇关于在Java Web应用程序中运行常规的后台事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!