임. 일부 jsp 파일이 포함 된 war 파일이 있습니다. 데이터베이스를 주기적으로 업데이트하고 jsp 파일이 필요할 때 데이터베이스에서 데이터를 가져 오는 개별 Java 파일에 스레드가 있습니다.WAR에서 스레드를 자동으로 실행합니다.
이제 태스크를 주기적으로 수행하고 사용자가보기 구성 요소에서 업데이트 된 결과를 얻을 수 있도록 스레드를 war 아카이브에서 자동으로 시작해야합니다.
public class AttendanceThread {
private long DEFAULT_SLEEP_TIME = 10000;// 10 seconds.
private long MAX_SLEEP_TIME = 30000;// 30 seconds.
private int shift;
public void MonitorAtttendance() {
final UpdateAttendance updateObject = new UpdateAttendance();
Runnable attThread = new Runnable() {
@Override
public void run() {
try {
// logic to calculate the current shift time
updateObject.CheckEmpAttendance(shift);
updateObject.resetUpdatedAttendance(shift);
Thread.sleep(MAX_SLEEP_TIME);
} else {
Thread.sleep(DEFAULT_SLEEP_TIME);
}
}// end of while (true)
} catch (Exception e) {
}
}// end of run()
}; // end of attThread definition
Thread t = new Thread(attThread);
t.setName(this.getClass().getSimpleName());
t.start();
}
public static void main(String args[]) {
// logger.log(Level.INFO, "Monitor Atttendance");
AttendanceThread ma = new AttendanceThread();
ma.MonitorAtttendance();
}
}
현재 테스트를 위해이 자바 파일을 separetely로 실행하여 스레드를 실행할 수 있고 JSP 파일에서 업데이트 된 결과를 얻을 수 있습니다.
웹 서버가 시작될 때 WAR 자체에서 또는 자동으로 스레드를 시작할 수있는 방법이 있습니까? 어떤 도움을 많이 주시면 감사하겠습니다. 웹 응용 프로그램은 다음과 같이 시작하면
public class ContextListenerExample implements ServletContextListener {
public void contextInitialized(ServletContextEvent e){
ServletContextcntxt = e.getServletContext();
//start your thread here
그리고 당신의 web.xml이 리스너를 정의하는, 그래서 시작 : : : 당신의 전쟁 파일에서
빈으로 연결해야합니다. –