是否可以在 Web 应用程序中运行 cron 作业?
在java web应用程序(servlets/spring mvc)中,使用tomcat,是否可以运行cron作业类型服务?
例如,每 15 分钟清除一次日志数据库。
您能否以独立于容器的方式执行此操作,或者必须使用 tomcat 或其他容器来运行?
请指定该方法是保证在特定时间运行还是每 15 分钟运行一次,但如果应用程序回收,则可能会重置等(如果您使用计时器,则在 .net 中就是这样)
In a java web application (servlets/spring mvc), using tomcat, is it possible to run a cron job type service?
e.g. every 15 minutes, purge the log database.
Can you do this in a way that is container independent, or it has to be run using tomcat or some other container?
Please specify if the method is guaranteed to run at a specific time or one that runs every 15 minutes, but may be reset etc. if the application recycles (that's how it is in .net if you use timers)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如第 23 章调度和线程池中所述, Spring 通过 Timer 和 Quartz Scheduler 的集成类提供调度支持 (http:// www.quartz-scheduler.org/)。对于简单的需求,我建议使用 JDK
Timer
。请注意,Java 调度程序通常用于触发 Java 面向业务的作业。对于系统管理任务(如您给出的示例),您应该更喜欢 cron 和传统管理工具(bash 等)。
As documented in Chapter 23. Scheduling and Thread Pooling, Spring has scheduling support through integration classes for the
Timer
and the Quartz Scheduler (http://www.quartz-scheduler.org/). For simple needs, I'd recommend to go with the JDKTimer
.Note that Java schedulers are usually used to trigger Java business oriented jobs. For sysadmin tasks (like the example you gave), you should really prefer cron and traditional admin tools (bash, etc).
如果您使用 Spring,则可以使用内置的 Quartz 或 Timer 挂钩。请参阅http://static.springsource.org/spring/docs /2.5.x/reference/scheduling.html
If you're using Spring, you can use the built-in Quartz or Timer hooks. See http://static.springsource.org/spring/docs/2.5.x/reference/scheduling.html
它将是特定于容器的。您可以在 Java 中使用 Quartz 或仅使用 Java 的调度并发实用程序(< a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html" rel="nofollow noreferrer">
ScheduledExecutorService
) 或作为操作系统级 cron 作业。每 15 分钟似乎有点极端。一般来说,我还建议您只截断/删除不再写入的日志文件(它们通常会在一夜之间滚动)。
It will be container-specific. You can do it in Java with Quartz or just using Java's scheduling concurrent utils (
ScheduledExecutorService
) or as an OS-level cron job.Every 15 minutes seems extreme. Generally I'd also advise you only to truncate/delete log files that are no longer being written to (and they're generally rolled over overnight).
作业是面向批次的。通过手动触发或 cron 风格(如您似乎想要的)。
我还是不明白你的 webapp 和 cron 式工作之间的关系?我能想到的唯一的 Web 应用程序用例是,您希望有一个 HTTP 端点来触发作业(但这与您关于“cron 风格”的说法相反)。
通常使用专用框架来解决“批处理作业”问题。我可以推荐石英。
Jobs are batch oriented. Either by manual trigger or cron-style (as you seem to want).
Still I don't get your relation between webapp and cron-style job? The only webapp use-case I could think of is, that you want to have a HTTP endpoint to trigger a job (but this opposes your statement about being 'cron-style').
Generally use a dedicated framework, which solves the problem-area 'batch-jobs'. I can recommend quartz.