我有一个定时器类,以响应短信连续。
public class MyRegularTask extends TimerTask {
public void run() {
Thread.sleep(1000);
answerLeftSMS();
// this.cancel(); I do not cancel this Timer
}
}目前,我是通过一个行动电话运行它。
Timer time = new Timer();
MyRegularTask taskTimer = new MyRegularTask();
time.schedule(taskTimer, 0, 60000);当我启动Application (Tomcat,Glassfish)时,有任何方法自动运行这个计时器吗?我正在使用Spring (注释)。
发布于 2017-03-02 12:11:45
如下所示应能奏效
@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
}
}https://stackoverflow.com/questions/42554576
复制相似问题