자바 타이머 타이머 응용

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 시 이 므 로 현재 시작 시간 과 정시 작업 시간의 앞 뒤 를 잘 판단 해 야 합 니 다.

좋은 웹페이지 즐겨찾기