Reputation: 867
I have a Timer class to response of SMS continuously.
public class MyRegularTask extends TimerTask {
public void run() {
Thread.sleep(1000);
answerLeftSMS();
// this.cancel(); I do not cancel this Timer
}
}
Currently I am running it by a action call.
Timer time = new Timer();
MyRegularTask taskTimer = new MyRegularTask();
time.schedule(taskTimer, 0, 60000);
Is there any way to run this timer automatically, when I start Application Server (Tomcat, Glassfish) ? I am using Spring MVC (Annotation).
Upvotes: 1
Views: 806
Reputation: 4289
Following should do the trick
@Component
public class MyRegularTask extends TimerTask {
@PostConstruct
public void init() {
Timer time = new Timer();
MyRegularTask taskTimer = new MyRegularTask();
time.schedule(taskTimer, 0, 60000);
}
public void run() {
Thread.sleep(1000);
answerLeftSMS();
// this.cancel(); I do not cancel this Timer
}
}
Upvotes: 1