tags:

views:

198

answers:

5

HI

I want to run a method in my program evry X hours, how to do that ? Im googling and there is nothing :/

+6  A: 

You could take a look at the Timer class, but the best option is to use a ScheduledExecutorService:

e.g. This will beep at a scheduled rate:

import static java.util.concurrent.TimeUnit.*;
 class BeeperControl {
    private final ScheduledExecutorService scheduler =
       Executors.newScheduledThreadPool(1);

    public void beepForAnHour() {

        final Runnable beeper = new Runnable() {
                public void run() { 
                    System.out.println("beep"); 
                }
            };

        final ScheduledFuture<?> beeperHandle =
            scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);

        scheduler.schedule(new Runnable() {
                public void run() { 
                    beeperHandle.cancel(true); 
                }
        }, 60 * 60, SECONDS);
    }
 }
Jon
These are good options for a process that is running continuously, and includes some pre-defined tasks. For "durable" tasks (jobs that need to be scheduled dynamically, and that need to be saved in case of an application crash), check out the Quartz Scheduler (http://www.opensymphony.com/quartz)
erickson
just a minor nitpick... if I'm reading that code correctly, it doesn't actually "beep" as you describe, but instead prints the string "beep". IIRC, if you instead printed the string "\a", it would actually make an audible beep.
rmeador
@rmeador - only if it's a console app, otherwise you have to do something like java.awt.Toolkit.getDefaultToolkit().beep()
Mike
A: 

Scheduled Task (in Windows) or Cron (in Unix)

Adrian Godong
Based on the requirements, a cron or scheduled task will not work. The requirement is to run a particular function in an application, not to run an application. AFAIK, cron and scheduled task can only run scripts and applications and have no control over the flow of a particular application.
Thomas Owens
A: 

You could save the time at a certain point, than start a timer. When the time is up, you run the method and restart the timer.

Mike
+9  A: 

You could consider Quartz.

It is some sort of cron that runs inside java. I admit though that it is probably an overkill if you want to schedule only one job.

idrosid
Reading between the lines, I'd guess that Quartz is overkill and probably too difficult to use in this case. In fact, despite it's technical drawbacks, java.util.Timer is likely to be a better fit than ExecutorService.
erickson
+2  A: 

I use the Quartz framework for most of my scheduling ( http://www.opensymphony.com/quartz/ ) but if you're doing something simple, java.util.Timer is fine.

// in a class body...

public static void main( String[] argv ) {
    Timer timer = new Timer();
    int secondsBetweenRuns = 3600;
    timer.schedule( new MyOwnTask(), 0, secondsBetweenRuns * 1000 );
}

static class MyOwnTask extends TimerTask {

    public void run() {
     doWhateverYouNeedToDoEveryHour();
    }
}
Hugi