Schedule Java task for a specified time
Asked Answered
F

6

6

I would like to be able to schedule a task at a specific time in Java. I understand that the ExecutorService has the ability to schedule at periodic intervals, and after a specified delay, but I am looking more for a time of day as opposed to after a duration.

Is there a way to have, say, a Runnable execute at 2:00, or do I need to calculate the time between now and 2:00, and then schedule the runnable to execute after that delay?

Ferrosilicon answered 9/11, 2011 at 14:3 Comment(2)
I'm really looking for a strictly Java solution.Ferrosilicon
+1 you can do this in all operating systems. Windows is irritating, but a simple script can execute the program from a scheduled task. A side effect is easy testing (you can just run it) and administration is trivial.Prawn
J
4

You'll be wanting Quartz.

Jagannath answered 9/11, 2011 at 14:5 Comment(0)
S
7

you can use spring annotations too

@Scheduled(cron="*/5 * * * * MON-FRI")
public void doSomething() {
// something that should execute on weekdays only
}

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/scheduling.html

Soane answered 9/11, 2011 at 15:36 Comment(0)
W
6

this is how I've solved it using java7SE:

    timer = new Timer("Timer", true);
    Calendar cr = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    cr.setTimeInMillis(System.currentTimeMillis());
    long day = TimeUnit.DAYS.toMillis(1);
    //Pay attention - Calendar.HOUR_OF_DAY for 24h day model 
    //(Calendar.HOUR is 12h model, with p.m. a.m. )
    cr.set(Calendar.HOUR_OF_DAY, it.getHours());
    cr.set(Calendar.MINUTE, it.getMinutes());
    long delay = cr.getTimeInMillis() - System.currentTimeMillis();
    //insurance for case then time of task is before time of schedule
    long adjustedDelay = (delay > 0 ? delay : day + delay);
    timer.scheduleAtFixedRate(new StartReportTimerTask(it), adjustedDelay, day);
    //you can use this schedule instead is sure your time is after current time
    //timer.scheduleAtFixedRate(new StartReportTimerTask(it), cr.getTime(), day);

it happens to be trickier than I thought to do it correctly

Wellfound answered 7/9, 2012 at 7:23 Comment(1)
This solution doesn't deal with days that have less/more than 24 hours (daylight saving situations).Floor
J
4

You'll be wanting Quartz.

Jagannath answered 9/11, 2011 at 14:5 Comment(0)
I
2

Got myself on this situation this morning... This was my code to run at midnight

    scheduler = Executors.newScheduledThreadPool(1);
    Long midnight=LocalDateTime.now().until(LocalDate.now().plusDays(1).atStartOfDay(), ChronoUnit.MINUTES);
    scheduler.scheduleAtFixedRate(this, midnight, 1440,  TimeUnit.MINUTES);
Inexistent answered 4/8, 2014 at 13:41 Comment(0)
S
0

Check out Quartz. We use it for our production apps, and it's very good. It works pretty much like crontab. You can specify a time during a set schedule and it'll execute a callback at that time.

Shaefer answered 9/11, 2011 at 14:6 Comment(0)
R
0

user java.util.Timer. It has method new schedule(task, time) where time is a Date when you want to execute the task once.

Reddy answered 9/11, 2011 at 14:7 Comment(1)
I was under the impression that Timer was considered obsolete with Java 5's Executors--is that mistaken?Ferrosilicon

© 2022 - 2024 — McMap. All rights reserved.