现在项目中需要开发一个定时任务,去清理指定目录下过期的文件,有几种实现定时器的方法定时器代码如下
方法一:
public class FileProcesser
{
private static Logger logger = Logger.getLogger(FileProcesser.class);
public static void start()
{
int startHour = 0;
int startMinute = 3;
// 读取配置文件中启动定时任务时间
String[] startTime = FileConfig.fileConfig.get(
FileConfig.START_CLEARFILE_TIME).split(":");
try
{
startHour = Integer.parseInt(startTime[0]);
startMinute = Integer.parseInt(startTime[1]);
}
catch (Exception e)
{
logger.error("get Config Info fail", e);
}
scheduleProcessFile(getDelay(startHour, startMinute), 86400000);
}
private static void scheduleProcessFile(long delay, long period)
{
try
{
ScheduledExecutorService ses = Executors
.newSingleThreadScheduledExecutor();
ses.scheduleAtFixedRate(new FileProcessTask(), delay, period,
TimeUnit.MILLISECONDS);
}
catch (Exception e)
{
logger.error("scheduleProcessFile Exception", e);
}
}
private static long getDelay(int hour, int minuts)
{
// 获得定时时间毫秒
Calendar sendCal = Calendar.getInstance();
sendCal.set(sendCal.get(Calendar.YEAR), sendCal.get(Calendar.MONTH),
sendCal.get(Calendar.DAY_OF_MONTH), hour, minuts);
long sendAtTime = sendCal.getTimeInMillis();
// 获得当前时间毫秒
Calendar now = Calendar.getInstance();
long newTime = now.getTimeInMillis();
// 如果过了定时时间,24小时后在处理
if (newTime > sendAtTime)
{
sendCal.set(sendCal.get(Calendar.YEAR),
sendCal.get(Calendar.MONTH), sendCal
.get(Calendar.DAY_OF_MONTH) + 1, hour, minuts);
sendAtTime = sendCal.getTimeInMillis();
}
long delayTime = sendAtTime - newTime;
return delayTime;
}
}
方法二:
private Timer timer= null;
private void start()
{
// 清除过期的定时器任务,每2个小时过期时间间隔检查一次容器
TimerTask task = new TimerTask()
{
@Override
public void run()
{
// 处理定时任务
}
};
timer= new Timer(true);
timer.schedule(task, 0, 1000 * 60 * 60 * 2);
}
方法一:
public class FileProcesser
{
private static Logger logger = Logger.getLogger(FileProcesser.class);
public static void start()
{
int startHour = 0;
int startMinute = 3;
// 读取配置文件中启动定时任务时间
String[] startTime = FileConfig.fileConfig.get(
FileConfig.START_CLEARFILE_TIME).split(":");
try
{
startHour = Integer.parseInt(startTime[0]);
startMinute = Integer.parseInt(startTime[1]);
}
catch (Exception e)
{
logger.error("get Config Info fail", e);
}
scheduleProcessFile(getDelay(startHour, startMinute), 86400000);
}
private static void scheduleProcessFile(long delay, long period)
{
try
{
ScheduledExecutorService ses = Executors
.newSingleThreadScheduledExecutor();
ses.scheduleAtFixedRate(new FileProcessTask(), delay, period,
TimeUnit.MILLISECONDS);
}
catch (Exception e)
{
logger.error("scheduleProcessFile Exception", e);
}
}
private static long getDelay(int hour, int minuts)
{
// 获得定时时间毫秒
Calendar sendCal = Calendar.getInstance();
sendCal.set(sendCal.get(Calendar.YEAR), sendCal.get(Calendar.MONTH),
sendCal.get(Calendar.DAY_OF_MONTH), hour, minuts);
long sendAtTime = sendCal.getTimeInMillis();
// 获得当前时间毫秒
Calendar now = Calendar.getInstance();
long newTime = now.getTimeInMillis();
// 如果过了定时时间,24小时后在处理
if (newTime > sendAtTime)
{
sendCal.set(sendCal.get(Calendar.YEAR),
sendCal.get(Calendar.MONTH), sendCal
.get(Calendar.DAY_OF_MONTH) + 1, hour, minuts);
sendAtTime = sendCal.getTimeInMillis();
}
long delayTime = sendAtTime - newTime;
return delayTime;
}
}
方法二:
private Timer timer= null;
private void start()
{
// 清除过期的定时器任务,每2个小时过期时间间隔检查一次容器
TimerTask task = new TimerTask()
{
@Override
public void run()
{
// 处理定时任务
}
};
timer= new Timer(true);
timer.schedule(task, 0, 1000 * 60 * 60 * 2);
}