如何从 Java 每天运行一个任务?

发布于 2024-08-06 10:05:21 字数 85 浏览 8 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

青衫儰鉨ミ守葔 2024-08-13 10:05:21

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Quartz is 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.

Jcrontab is 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.

Essiembre J2EE Scheduler is 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.

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

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.

Fulcrum Scheduler provides 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.

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

Job Scheduler is 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.

JDRing is 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.

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

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

Super with SuperScheduler and SuperWatchdog is 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.

source: Open Source Job Schedulers in Java

穿透光 2024-08-13 10:05:21

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

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

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

Look into TimerTask and Timer - both are in that version of the 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.

江挽川 2024-08-13 10:05:21

您是否可以使用诸如 cron4j 之类的库?这将使您的工作变得更加轻松!

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

你另情深 2024-08-13 10:05:21
  • Java 定时器可以按时间间隔、预先固定的时间等运行任意作业。
  • Quartz
  • 如果你真的想了解它的基本情况,请将其包装在 shell 脚本中并放入 cron 中。
  • Java Timers can run an arbitrary job at intervals, pre-fixed times, etc.etc.
  • Quartz library
  • If you really want to be bare-bones about it, wrap it in a shell script and put in in cron.
甜柠檬 2024-08-13 10:05:21

你必须使用 Quartz

但我不知道是谁第一个推出 Quartz 的。

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

You have to use Quartz

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

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.

洒一地阳光 2024-08-13 10:05:21

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

这会暂停我的 Java 线程吗等一下?

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

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

[]的,

以及过去

Maybe, that recent post helps you:

Will this pause my Java thread for a minute?

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?

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.

[]'s,

And Past

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文