JAVA编码(12)——系统定时操作平台操作
package cn.com.sinosoft.test;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
public class TestMain {
private static SimpleDateFormat dateFormat;
private static CommonTask task;
private static Date mPlanStartTime;
private static Date mStartTime;
private static long mPeriod = 1000;
private static Timer timer;
public static void main(String[] args) throws ParseException {
dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
mPlanStartTime = dateFormat.parse("2015-02-28 09:05:40");
timer = new Timer();
setStartTime();
preventStartExecution();
executeTask();
}
private static void preventStartExecution() {
Calendar calendar = Calendar.getInstance();
Calendar calendartemp = Calendar.getInstance();
calendartemp.setTime(mPlanStartTime);
calendar.set(Calendar.HOUR_OF_DAY, calendartemp.get(Calendar.HOUR_OF_DAY));
calendar.add(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.MINUTE, calendartemp.get(Calendar.MINUTE));
calendar.set(Calendar.SECOND, calendartemp.get(Calendar.SECOND));
System.out.println("防止开机启动:" + calendar.getTime().toString());
System.out.println("测试时间:" + calendartemp.getTime().toString());
}
private static void setStartTime() {
long tProjectTime = mPlanStartTime.getTime();//系统计划执行时间(毫秒)
long tCurrentTime = new Date().getTime();//系统当前时间(毫秒)
if (tCurrentTime > tProjectTime) {
long tNextDayProjectTime = ((tCurrentTime - tProjectTime)/mPeriod + 1) * mPeriod + tProjectTime;
mStartTime = new Date(tNextDayProjectTime);
} else {
mStartTime = mPlanStartTime;
}
System.out.println("系统计划执行时间:" + mPlanStartTime.toString());
System.out.println("系统实际执行时间:" + mStartTime.toString());
}
public static void executeTask(){
System.out.println("开始测试。。。");
//在指定时间执行
// task = new CommonTask("【任务一】");
// timer.schedule(task, mStartTime);
//两秒后执行
// task = new CommonTask("【任务二】");
// timer.schedule(task, 2000);
//指定时间开始执行,后重复一秒执行一次
// task = new CommonTask("【任务三】");
// timer.schedule(task, mStartTime, mPeriod);
//四秒后开始执行,后重复一秒执行一次
// task = new CommonTask("【任务四】");
// timer.schedule(task, 4000, mPeriod);
//指定时间开始执行,后重复一秒执行一次
// task = new CommonTask("【任务五】");
// timer.scheduleAtFixedRate(task, mStartTime, mPeriod);
//六秒后开始执行,后重复一秒执行一次
// task = new CommonTask("【任务六】");
// timer.scheduleAtFixedRate(task, 6000, mPeriod);
//取消定时任务
// task = new CommonTask("【任务七】");
// timer.schedule(task, mStartTime);
// task.cancel();
// task = new CommonTask("【任务八】");
// timer.schedule(task, 8000);
// task.cancel();
//从计时器的任务队列中移除所有已取消的任务
// timer.purge();
//schedule 与 scheduleAtFixedRate 比较
// task = new CommonTask("【任务九】");
// timer.schedule(task, mStartTime,mPeriod);
//我就相中【任务十】这个定时任务了
task = new CommonTask("【任务十】");
timer.scheduleAtFixedRate(task, mStartTime, mPeriod);
//终止定时器,丢弃所有已经安排的定时任务
// timer.cancel();
}
}
==================================================================
package cn.com.sinosoft.test;
import java.util.Date;
import java.util.TimerTask;
public class CommonTask extends TimerTask{
private String name = null;
public CommonTask(){}
public CommonTask(String name){
this.name = name;
}
@Override
public void run() {
System.out.println("我是任务" + this.name
+ "被执行了,执行时间为:" + new Date(this.scheduledExecutionTime()));
}
@Override
public boolean cancel() {
System.out.println("我是任务" + this.name
+ "被取消了,取消时间为:" + new Date());
return super.cancel();
}
}
==================================================================
package cn.com.sinosoft.test;
import java.util.Date;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.log4j.Logger;
public class ContextListener implements ServletContextListener{
private Logger logger = Logger.getLogger(ContextListener.class);
public ContextListener() {
System.out.println("监听器启动。。。");
logger.info("监听器启动"+new Date().toString());
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
logger.info("监听器初始化开始" + new Date().toString());
autoStartTimerTask();
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("监听器销毁。。。");
logger.info("监听器销毁" + new Date().toString());
}
private void autoStartTimerTask() {
logger.info("定时器启动开始。。。");
//以下是整个业务逻辑的展开。。。
}
}
以上是 JAVA编码(12)——系统定时操作平台操作 的全部内容, 来源链接: utcz.com/z/394110.html