在 Grails 应用程序中何处放置重复任务?
我正在学习 grails,我想包括一个在我的应用程序运行时每五秒触发一次的重复任务,并且应该能够访问我的域对象等。在 Grails 中完成此任务的正确方法是什么?
我考虑启动 定时器 < code>BootStrap.groovy,但这会被处理并终止计时器。
I'm learning grails, and I would like to include a recurring task that fires every five seconds while my app is running, and should have access to my domain objects and such. What is the proper way to accomplish this in Grails?
I considered starting a Timer in BootStrap.groovy
, but that would get disposed and kill the timer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我从未使用过它,但 Grails Quartz 插件 应该可以让您做您想做的事情。
I've never used it but the Grails Quartz plugin should let you do what you want.
Quartz (http://grails.org/plugin/quartz) 允许您以几乎相同的方式定义重复任务就像 CRON 任务在单个服务器上运行一样。
您可以像这样将其安装在您的项目中:
安装后,您可以使用以下命令创建一个新作业:
然后您可以像这样安排它:
如果您喜欢 CRON 格式,您可以使用类似的格式安排触发器:
但是,因为grails 应用程序可以跨多个服务器部署(QA、staging、部署、负载均衡器...)。quartz 插件允许特定进程运行,无论它部署在哪台服务器上。
需要注意的一件事是服务器时钟是同步的 - 否则你可能会得到一些奇怪的功能(特别是如果多个服务器共享同一个数据库)。
Quartz (http://grails.org/plugin/quartz) lets you define recurring tasks in much the same way as a CRON task would run on a single server.
You can install it in your project like this:
Once it's installed, you can create a new job with:
Then you can schedule it like so:
If you prefer the CRON formatting, you can schedule your trigger using a similar format:
However, since the grails app could be deployed across several servers (QA, staging, deploy, load balancer ...) the quartz plugin lets the specific process run regardless of which server it's deployed on.
One thing to keep an eye on is that the server clocks are synchronized - otherwise you could end up with some strange functionality (particularly if several servers share the same database).