2017-03-02 15 views
0

SMS의 응답에 지속적으로 Timer 클래스가 있습니다.시작시 TimerTask 실행 Application Server

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); 

응용 프로그램 서버 (Tomcat, Glassfish)를 시작할 때이 타이머를 자동으로 실행할 수 있습니까? Spring MVC (Annotation)를 사용하고 있습니다.

+0

을해야 다음 : 왜'(1000)에 Thread.sleep;'? – Fildor

+1

어쩌면 여기보세요 : http://stackoverflow.com/a/2401536/1638059 – Schlangguru

+1

이 작업은 ['@ Scheduled'] (http://docs.spring.io/spring-framework/docs/current/javadoc)를 사용하여 수행 할 수 있습니다. -api/org/springframework/scheduling/annotation/Scheduled.html). 구현 세부 정보는 [here] (https://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html)를 참조하십시오. –

답변

0

curiositiy 중 트릭

@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 
    } 
}