tags:

views:

1057

answers:

6

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.

+3  A: 

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.

Gandalf
+3  A: 

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

Phill Sacre
this seems like a possibility depending on whether this library will be allowed to be used
ferrari fan
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)
StaxMan
+1  A: 

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.

OscarRyz
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.
Jay
+2  A: 
  • 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.
Steve B.
A: 

Maybe, that recent post helps you:

http://stackoverflow.com/questions/1487083/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: http://stackoverflow.com/questions/1487083/will-this-pause-my-java-thread-for-a-minute/1487891#1487891

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

apast
Fast crontab how to:http://www.crontabrocks.org/[]'s, And Past
apast
+5  A: 

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

JuanZe