注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

断尘居

温柔的男人像海洋。

 
 
 
 
 

日志

 
 

java定时器的两种写法  

2011-11-23 15:00:55|  分类: JDK |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
1)
public class LdapToDBTask extends TimerTask {
private static Logger log = Logger.getLogger(LdapToDBTask.class);

public void run() {
if (isTimer()) {
service s = new service();
s.operateForNewLdapSchema();
}

}

private boolean isTimer() {
boolean flag = false;
String tmp = GetProps.getInstance().getProperties("LDAPTODB");
if (tmp == null || tmp.length() == 0) {
tmp = "00:30";
}
Calendar cal = Calendar.getInstance();
int hour = cal.get(Calendar.HOUR_OF_DAY);
int min = cal.get(Calendar.MINUTE);
log.debug(tmp + "=================" + hour + ":" + min);
String[] temp = tmp.split(":");
if (temp.length == 2) {
if ((Integer.parseInt(temp[0]) == hour)
&& (Integer.parseInt(temp[1]) == min)) {
return true;
}
} else {
log.debug("=the timer time format is not right!=");
}
return flag;
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

}

}


2)

public class TimerCompanyDispatch {

private static final Logger log = Logger
.getLogger(TimerCompanyDispatch.class);
private static Timer instanceTimer = new Timer();

public static void main(String[] args) {
try {
// log.debug("=begin="+DateAndStringUtils.parseDateToStrWithYYYYMMDD(DateAndStringUtils.addDaysToSpecifiedDate(Calendar.getInstance().getTime(),
// 1))+" 03:00:00"+"=end=");
log.debug("=schedule =begin=");
String strCurrentHourMinute = DateAndStringUtils
.parseDateToStrWithHHMMSS(Calendar.getInstance().getTime());
log.debug("=strCurrentHourMinute is:=" + strCurrentHourMinute
+ "=end=");
int incrementDays = Integer.parseInt(strCurrentHourMinute
.replaceAll(":", "")) < Integer
.parseInt(PropertyFileReaderUtils
.getInstance()
.getProperties(
"START_TIME_OF_TIMER_FOR_COMPANY_DISPATCH")
.trim().replaceAll(":", "")) ? 0 : 1;
log.debug("=incrementDays is:=" + incrementDays + "=end=");

instanceTimer
.schedule(
new ScheduleTask(),
DateAndStringUtils.constructDateByString(DateAndStringUtils
.parseDateToStrWithYYYYMMDD(DateAndStringUtils
.addDaysToSpecifiedDate(Calendar
.getInstance().getTime(),
incrementDays))
+ " "
+ PropertyFileReaderUtils
.getInstance()
.getProperties(
"START_TIME_OF_TIMER_FOR_COMPANY_DISPATCH")),
24 * 60 * 60 * 1000);

// instanceTimer.schedule(new ScheduleTaskTest(),
// DateAndStringUtils.constructDateByString("2009-01-04 13:45:00"),
// 3000);
// instanceTimer.schedule(new ScheduleTaskTest(),new Date());

log.debug("=schedule =end=");
} catch (Exception exp) {
log.debug("=the exception information as the following:=");
exp.printStackTrace();
instanceTimer.cancel();
log.debug("=an exception happens to scheduler task!=");
}
}

public static void stopTimer() {
log.info("=in process of method stopTimer from CompanyDispatchServlet=");
instanceTimer.cancel();
log.info("=out of method stopTimer from CompanyDispatchServlet=");
}// end of method stopTimer

static class ScheduleTaskTest extends java.util.TimerTask {
public void run() {
log.debug("=the method run is running!=");
}

}

public static boolean gatherDocAutomatically(String paramDateFolderName) {
boolean isSuccess = false;
................
return isSuccess;
}

static class ScheduleTask extends java.util.TimerTask {

public void run() {
// TODO Auto-generated method stub
log.info("=in process of method run from ScheduleTask=");

try {
........
} catch (Exception exp) {
exp.printStackTrace();
}

}//end of run method
}

}

  评论这张
 
阅读(802)| 评论(3)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017