자바 타이머 타이머 응용
2685 단어 자바
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.joda.time.DateTime;
import com.xes.task.BocmGenerateRefundTask;
import com.xes.task.BocmRefundHandleTask;
public class AutoRefundListener implements ServletContextListener{
private Timer timer = null;
private Timer timer1 = null;
@Override
public void contextDestroyed(ServletContextEvent arg0) {
if (timer != null) {
timer.cancel();
}
if(timer1 != null){
timer1.cancel();
}
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
Properties propertie = new Properties();
InputStream is = AutoRefreshCourseListener.class.getResourceAsStream("/bocmRefund.properties");
try {
propertie.load(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
// ,
long period = 24*60*60*1000;
//
DateTime currentDate = new DateTime(System.currentTimeMillis()).plusDays(1);
int y = Integer.parseInt(currentDate.toString("yyyy"));
int m = Integer.parseInt(currentDate.toString("MM"));
int d = Integer.parseInt(currentDate.toString("dd"));
int hh = Integer.parseInt(propertie.getProperty("hh"));
int mm = Integer.parseInt(propertie.getProperty("mm"));
int hh1 = Integer.parseInt(propertie.getProperty("bankhh"));
int mm1 = Integer.parseInt(propertie.getProperty("bankmm"));
timer = new Timer(true);
timer.schedule(new BocmGenerateRefundTask(), new DateTime(y,m,d,hh,mm,00,000).toDate(), period);
timer1 = new Timer(true);
timer1.schedule(new BocmRefundHandleTask(), new DateTime(y,m,d,hh1,mm1,00,000).toDate(), period);
}
}
설명:
1.ServletContextListener 프로젝트 시작 타이머 타이머 타이머,자바 타이머 타이머 시작
2.schedule 방법:다음 실행 시간 은 지난번 실제 실행 이 완 료 된 시간 과 비교 되 기 때문에 실행 시간 이 계속 연 기 됩 니 다.
3.scheduleAtFixed Rate 방법:다음 실행 시간 은 지난번 시작 시간 에 비해 늦 어 지지 않 습 니 다.
4.Timer 가 실 행 될 때 먼저 당일 시간 이 설 정 된 시간 을 초과 하 는 지 판단 하고 초과 하면 정시 임 무 를 먼저 실행 하 며 다음 운행 은 이번 운행 지연 간격 에 따라 실 행 됩 니 다.예 를 들 어 정시 임 무 는 새벽 1 시 에 실 행 됩 니 다.간격 은 24 시간 입 니 다.현재 7 시 에 시작 하 는 프로젝트 는 정시 임 무 를 수행 합 니 다.이렇게 되면 다음 실행 은 내일 7 시 이 므 로 현재 시작 시간 과 정시 작업 시간의 앞 뒤 를 잘 판단 해 야 합 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.