Scheduler to run a program in java

by Vinoth 2009-03-16 11:36:48

The bellow code will run the java code or function at a schedule time.


import java.util.Timer;
import java.util.TimerTask;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Date;

public final class Schedule extends TimerTask {
public static void main (String arg[] ) {
TimerTask report = new Schedule();
Timer timer = new Timer();
timer.scheduleAtFixedRate(report, getTomorrowMorning4am(), fONCE_PER_DAY);
}
public void run(){//the function will run at the scheduled time
System.out.println("Sampling for urlstatus to repository");
}
private final static long fONCE_PER_DAY = 1000*60*60*24;
private final static int fONE_DAY = 2;
private final static int fFOUR_AM = 14;
private final static int fZERO_MINUTES = 22;
private static Date getTomorrowMorning4am(){
Calendar tomorrow = new GregorianCalendar();
tomorrow.add(Calendar.DATE, fONE_DAY);
Calendar result = new GregorianCalendar(
tomorrow.get(Calendar.YEAR),
tomorrow.get(Calendar.MONTH),
tomorrow.get(Calendar.DATE),
fFOUR_AM,
fZERO_MINUTES
);
return result.getTime();
}
}

Tagged in:

907
like
0
dislike
0
mail
flag

You must LOGIN to add comments