如何每天从 Java 运行任务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1498837/
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 16:47:02  来源:igfitidea点击:

How to run a task daily from Java?

javatimerjdk1.4

提问by Jaime Garcia

How can I run a task daily at a specified time (say 11:00 am) using java.util.Timer? I'm using JDK 1.4.2, I know it's old, but it's what the project requires.

如何每天在指定时间(例如上午 11:00)使用 运行任务java.util.Timer?我使用的是 JDK 1.4.2,我知道它很旧,但这正是项目所需要的。

回答by JuanZe

Quartz is the most well known solution to schedule processes in Java environments, but you have a lot of options. Check this list:

Quartz 是在 Java 环境中调度进程的最著名的解决方案,但您有很多选择。检查这个列表:

Quartzis an open source job scheduling system that can be integrated with, or used along side virtually any J2EE or J2SE application. Quartz can be used to create simple or complex schedules.

Quartz是一个开源作业调度系统,可以与几乎任何 J2EE 或 J2SE 应用程序集成或一起使用。Quartz 可用于创建简单或复杂的计划。

Jcrontabis designed to be extended and integrated with any project. Reads and stores the tasks to execute in a file, a database or an EJB and provides a web UI and a basic swing GUI.

Jcrontab旨在扩展并与任何项目集成。读取并存储要在文件、数据库或 EJB 中执行的任务,并提供 Web UI 和基本的 Swing GUI。

Essiembre J2EE Scheduleris a simple task scheduling mechanism for J2EE applications. This library can be considered a wrapper around the Timer and TimerTask classes found in the standard Java API. Configuration for tasks to be executed and their frequency is XML-based.

Essiembre J2EE Scheduler是一种用于 J2EE 应用程序的简单任务调度机制。这个库可以被认为是标准 Java API 中 Timer 和 TimerTask 类的包装器。要执行的任务及其频率的配置是基于 XML 的。

cron4jis a scheduler for the Java 2 platform which is very similar to the UNIX cron daemon.

cron4j是 Java 2 平台的调度程序,它与 UNIX cron 守护进程非常相似。

Oddjob's goal is to provide some order and visibility to all the batch files and cron jobs that tie an enterprise's critical business processes together.

Oddjob的目标是为将企业的关键业务流程联系在一起的所有批处理文件和 cron 作业提供某种顺序和可见性。

Fulcrum Schedulerprovides a scheduler service. It is based on the TurbineScheduler provided with Turbine, but all older stuff has been removed. Currently ONLY the non persistent Scheduler is done. It loads scheduled jobs from the component config xml file.

Fulcrum Scheduler提供调度服务。它基于 Turbine 提供的 TurbineScheduler,但所有旧的东西都已被删除。目前只完成了非持久性调度程序。它从组件配置 xml 文件加载计划作业。

Gos4j-Goal Oriented Scheduling for Java- is a way of organising processing priorities based on goals.

Gos4j-面向 Java 的目标调度 - 是一种基于目标组织处理优先级的方法。

Job Scheduleris a batch program operating as a demon, and can be controlled using a graphical user interface. The Job Scheduler uses an XML configuration for the scheduled programs, scripts and for the timing and frequency of task processing. An API is available that hands control of events and logging to your jobs.

Job Scheduler是一个像恶魔一样运行的批处理程序,可以使用图形用户界面进行控制。Job Scheduler 对计划的程序、脚本以及任务处理的时间和频率使用 XML 配置。有一个 API 可用于控制事件并记录到您的作业。

JDRingis a lightweight Java scheduling library that is simple and small, but still supports ringing alarms at specified intervals, as one-time events, or on complex schedules with full cron-like control.

JDRing是一个轻量级的 Java 调度库,它简单而小巧,但仍支持按指定时间间隔、作为一次性事件或具有完全类似 cron 控制的复杂计划的响铃警报。

jBatchEngineis a batch job spooler written in Java. In constrast to time driven schedulers like Cron, jBatchEngine is event driven.

jBatchEngine是一个用 Java 编写的批处理作业假脱机程序。与 Cron 等时间驱动的调度程序相比,jBatchEngine 是事件驱动的。

MyBatchFrameworkis an open-source lightweight framework designed to create easily robust and manageable batch programs into the Java language.

MyBatchFramework是一个开源轻量级框架,旨在为 Java 语言创建轻松健壮且易于管理的批处理程序。

Super with SuperSchedulerand SuperWatchdogis a Java job scheduler with rich GUI for all applications. It is platform neutral. Especially good to be a job scheduler for Linux and Solaris. It provides a super set of functionalities of the Scheduler of Microsoft Windows. It provides event-triggered scheduling. It can schedule tasks in a distributed environment. A task will be executed once and only once among all machines in the network. All tasks are holiday adjustable. Even every job is a STANDBY job, the history will be a good trace for important tasks. It supports Internationalization.

Super with SuperScheduler和 SuperWatchdog是一个 Java 作业调度器,为所有应用程序提供丰富的 GUI。它是平台中立的。尤其适合作为 Linux 和 Solaris 的作业调度程序。它提供了 Microsoft Windows 调度程序的一组超级功能。它提供事件触发的调度。它可以在分布式环境中调度任务。任务将在网络中的所有机器中执行一次且仅执行一次。所有任务都是假期可调的。即使每个作业都是 STANDBY 作业,历史记录也将是重要任务的良好踪迹。它支持国际化。

source: Open Source Job Schedulers in Java

来源:Java 中的开源作业调度程序

回答by Gandalf

Look into TimerTask and Timer - both are in that version of the JDK.

查看 TimerTask 和 Timer - 两者都在该版本的 JDK 中。

Timer :
public void schedule(TimerTask task, Date firstTime, long period)
public void scheduleAtFixedRate(TimerTask task, Date firstTime, long period)

Set it to run the first Date you want then the number of milliseconds in one day as your delay.

将它设置为运行您想要的第一个日期,然后将一天中的毫秒数作为您的延迟。

回答by Gandalf

Is it possible for you to use a library such as cron4j? It will make your job much easier!

您是否可以使用诸如cron4j 之类的库?它会让你的工作更轻松!

回答by Steve B.

  • Java Timers can run an arbitrary job at intervals, pre-fixed times, etc.etc.
  • Quartzlibrary
  • If you really want to be bare-bones about it, wrap it in a shell script and put in in cron.
  • Java 定时器可以以间隔、预先固定的时间等方式运行任意作业。
  • 石英
  • 如果你真的想简单地了解它,请将它包装在一个 shell 脚本中并放入 cron 中。

回答by OscarRyz

You have to use Quartz

你必须使用石英

I have never know who launches Quartz in first place though.

不过,我永远不知道是谁首先推出了 Quartz。

If you have an application server or similar artifact you can configure the Quartz job there and have it run your task at the given time.

如果您有应用程序服务器或类似的工件,您可以在那里配置 Quartz 作业并让它在给定时间运行您的任务。

回答by Andre Pastore

Maybe, that recent post helps you:

也许,最近的帖子可以帮助您:

Will this pause my Java thread for a minute?

这会暂停我的 Java 线程一分钟吗?

My response for that question is use a java built in implementation based on java.util.Time and java.util.TimerTask classes: Will this pause my Java thread for a minute?

我对这个问题的回答是使用基于 java.util.Time 和 java.util.TimerTask 类的 Java 内置实现: 这会暂停我的 Java 线程一分钟吗?

Or, you can use the crontab service for *nix platforms (available for Windows platforms too). That's the simplest and light-weight style to run a standalone job periodically.

或者,您可以将 crontab 服务用于 *nix 平台(也可用于 Windows 平台)。这是定期运行独立作业的最简单和轻量级的样式。

[]'s,

[] 的,

And Past

和过去