How to run a task daily from Java?
Asked Answered
H

6

10

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.

Hades answered 30/9, 2009 at 15:22 Comment(0)
L
34

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

Levenson answered 30/9, 2009 at 15:55 Comment(0)
A
4

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.

Abbreviation answered 30/9, 2009 at 15:26 Comment(0)
H
3

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

Hibben answered 30/9, 2009 at 15:26 Comment(2)
this seems like a possibility depending on whether this library will be allowed to be usedHades
I second this suggestion -- cron4j is a delight, and works like a charm, is much simpler than quartz (which works, too, it's just rather big and often more than you really need; and has some kinks for simpler use cases)Pandarus
Y
2
  • 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.
Yves answered 30/9, 2009 at 15:29 Comment(0)
V
1

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.

Violist answered 30/9, 2009 at 15:29 Comment(1)
Quartz has a lot of fancy features, so if your requirements are elaborate, it may be a good choice. But if you have a simple "fire this off at 5:00 every day", I'd just use Timer.Nordau
L
0

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

Laconic answered 30/9, 2009 at 15:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.