步骤一:
实现每天固定点刷新:
FixTimeSchedule fixTimeSchedule = new FixTimeSchedule();
String dFlashTime = SCPConfigration.GetIntance().get("scp.config.current.leftduration.flashTime");
fixTimeSchedule.dayOfDelay(dFlashTime, new CurrentLeftDurationHandle());
package com.mobile263.util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class FixTimeSchedule {
private static Log logger = LogFactory.getLog(FixTimeSchedule.class);
private static DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm");
private static DateFormat dayFormat = new SimpleDateFormat("yy-MM-dd");
/**
* 每天按固定时间time,执行指定任务
*/
public void dayOfDelay(String fixTime, Runnable runnable) {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
long oneDay = 24 * 60 * 60 * 1000;
long initDelay = getTimeMillis(fixTime) - System.currentTimeMillis();
initDelay = initDelay > 0 ? initDelay : oneDay + initDelay;
executor.scheduleAtFixedRate(runnable, initDelay, oneDay, TimeUnit.MILLISECONDS);
}
/**
* 获取给定时间对应的毫秒数
*
* @param string
* "HH:mm:ss"
* @return
*/
private static long getTimeMillis(String fixTime) {
try {
Date currentDate = dateFormat.parse(dayFormat.format(new Date()) + " " + fixTime);
return currentDate.getTime();
} catch (ParseException e) {
logger.error(e.getMessage());
}
return 0;
}
}
步骤二:
判断这个时间点是否是1号
package com.mobile263.scp.service;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* 控制通话剩余时长
*/
public class CurrentLeftDurationHandle implements Runnable {
private static Log logger = LogFactory.getLog(CurrentLeftDurationHandle.class);
private EMSSubsMasterDAO emsSubsMasterDAO = new EMSSubsMasterDAO();
private EmsOutCallControlDAO emsOutCallControlDAO = new EmsOutCallControlDAO();
@Override
public void run(){
logger.info("CurrentLeftDurationHandle enter");
try {
//判断当天是不是1号,是1号就执行
if(!isFirstDay()) {
logger.info("CurrentLeftDurationHandle left");
return;
}
//执行需要执行的任务。
} catch (Exception e) {
logger.error("CurrentLeftDurationHandle thread error !!! :" + e.getMessage());
}
logger.info("CurrentLeftDurationHandle left");
}
public boolean isFirstDay() {
// 获取服务器的时间
Date d = new Date();
Calendar c = Calendar.getInstance();
c.setTime(d);
if (c.get(Calendar.DAY_OF_MONTH) == 1) {
return true;
}
return false;
}
}
完美~~~