Timer class to call class on set time and after certain interval.... like cron


**************************object to call******************************
package com.infodart.timer;

import java.util.Date;

public class Target {

public java.util.Date callTargetMethod(){

System.out.println("Target.callTargetMethod()");
Date d = new Date();
return d;
}
}




**********************************TimerClass*************

package com.infodart.timer;

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


public class MyTimerTask extends TimerTask {
private final static long ONCE_PER_DAY = 1000*60*60*24;

//private final static int ONE_DAY = 1;
private final static int TWO_AM = 2;
private final static int ZERO_MINUTES = 0;


@Override
public void run() {

Target target = new Target();
Date result = target.callTargetMethod();
System.out.println("result is "+result);

}
private static Date ScheduleTimerStart(){

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY,17);
cal.set(Calendar.MINUTE,8);
cal.set(Calendar.SECOND,0);
cal.set(Calendar.MILLISECOND,0);

Date d = cal.getTime();

System.out.println(" scheduled start time "+ d);
return d;
}

//call this method from your servlet init method
public static void startTask(){
MyTimerTask task = new MyTimerTask();
Timer timer = new Timer(); 
timer.schedule(task,ScheduleTimerStart(),1000*10);// for your case u need to give 1000*60*60*24
}
public static void main(String args[]){
startTask();

}

}

Comments

Popular posts from this blog

Conversion from Clob To String in java and Vice versa

Portable Java 8/ JDK8 setup without admin rights

Multi Threading , Producer consumer problem in Java