java 是否可以在 Web 应用程序中运行 cron 作业?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2039349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:05:20  来源:igfitidea点击:

Is it possible to run a cron job in a web application?

javatomcatcronscheduled-tasksscheduler

提问by mrblah

In a java web application (servlets/spring mvc), using tomcat, is it possible to run a cron job type service?

在使用 tomcat 的 Java Web 应用程序(servlets/spring mvc)中,是否可以运行 cron 作业类型服务?

e.g. every 15 minutes, purge the log database.

例如,每 15 分钟清除一次日志数据库。

Can you do this in a way that is container independent, or it has to be run using tomcat or some other container?

您可以以独立于容器的方式执行此操作,还是必须使用 tomcat 或其他容器运行?

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)

请指定该方法是保证在特定时间运行还是每 15 分钟运行一次,但如果应用程序回收,则可能会重置等(如果您使用计时器,则在 .net 中就是这样)

采纳答案by Pascal Thivent

As documented in Chapter 23. Scheduling and Thread Pooling, Spring has scheduling support through integration classes for the Timerand the Quartz Scheduler (http://www.quartz-scheduler.org/). For simple needs, I'd recommend to go with the JDK Timer.

第 23 章调度和线程池中所述,Spring 通过TimerQuartz 调度器 ( http://www.quartz-scheduler.org/) 的集成类提供调度支持。对于简单的需求,我建议使用 JDK Timer

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

请注意,Java 调度程序通常用于触发面向Java业务的作业。对于 sysadmin 任务(如您给出的示例),您确实应该更喜欢 cron 和传统的管理工具(bash 等)。

回答by danben

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

如果您使用的是 Spring,则可以使用内置的 Quartz 或 Timer 钩子。见http://static.springsource.org/spring/docs/2.5.x/reference/scheduling.html

回答by cletus

It will be container-specific. You can do it in Java with Quartzor just using Java's scheduling concurrent utils (ScheduledExecutorService) or as an OS-level cron job.

它将是特定于容器的。您可以使用Quartz在 Java 中完成,也可以仅使用 Java 的调度并发工具 ( ScheduledExecutorService) 或作为操作系统级别的 cron 作业。

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

每 15 分钟一次似乎很极端。通常,我还建议您只截断/删除不再写入的日志文件(它们通常会在一夜之间滚动)。

回答by manuel aldana

Jobs are batch oriented. Either by manual trigger or cron-style (as you seem to want).

作业是面向批处理的。通过手动触发或 cron 样式(如您所愿)。

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').

我仍然不明白 webapp 和 cron-style 工作之间的关系?我能想到的唯一 webapp 用例是,您希望有一个 HTTP 端点来触发作业(但这与您关于“cron 风格”的说法相反)。

Generally use a dedicated framework, which solves the problem-area 'batch-jobs'. I can recommend quartz.

通常使用专用框架来解决问题区域“批处理作业”。我可以推荐石英。