Java 每天凌晨2点运行计时器任务
我有一个计时器ejb,它控制需要切换的应用程序何时运行。我需要让它每天凌晨两点运行Java 每天凌晨2点运行计时器任务,java,timer,ejb,Java,Timer,Ejb,我有一个计时器ejb,它控制需要切换的应用程序何时运行。我需要让它每天凌晨两点运行 public void fireInTwentyFourHours() throws EJBException TimerService theTimerService = mySessionCtx.getTimerService(); String aLabel = "24 Hours Interval"; //theTimerService.createTimer(ne
public void fireInTwentyFourHours() throws EJBException
TimerService theTimerService = mySessionCtx.getTimerService();
String aLabel = "24 Hours Interval";
//theTimerService.createTimer(new Date(),86400000, aLabel);
String stage = Stage.getStage();
List timerObjects = (List)theTimerService.getTimers();
if(null != timerObjects && timerObjects.size() > 0) {
for(int timerCount=0,size=timerObjects.size();timerCount<size;timerCount++) {
Timer timer = (Timer)timerObjects.get(timerCount);
timer.cancel();
}
}
if(stage.equalsIgnoreCase("P")){
theTimerService.createTimer(getRunTime(),86400000, aLabel);
} else {
theTimerService.createTimer(new Date(),86400000, aLabel);
}
}
private Date getRunTime() {
Calendar gc = new GregorianCalendar();
int year = gc.get(Calendar.YEAR);
int month = gc.get(Calendar.MONTH);
int date = gc.get(Calendar.DAY_OF_MONTH);
Calendar newDate = new GregorianCalendar(year,month,date,17,30,0);
return newDate.getTime();
}
第三个参数是timerConfig如何设置。它会使代码在0200运行吗?将日历转换为日期并使用,java.io.Serializable)@ScaryWombat我添加了一个代码使其在0200运行。但是如何将timerconfig设置为createIntervalTimer方法中的第三个参数呢
public Date getTime(){
Calendar c = Calendar.getInstance();
Date d = c.getTime();
return d;
}
public void fireInTwentyFourHours() throws EJBException
TimerService theTimerService = mySessionCtx.getTimerService();
String aLabel = "24 Hours Interval";
//theTimerService.createTimer(new Date(),86400000, aLabel);
String stage = Stage.getStage();
List timerObjects = (List)theTimerService.getTimers();
if(null != timerObjects && timerObjects.size() > 0) {
for(int timerCount=0,size=timerObjects.size();timerCount<size;timerCount++) {
Timer timer = (Timer)timerObjects.get(timerCount);
timer.cancel();
}
}
if(stage.equalsIgnoreCase("P")){
theTimerService.createIntervalTimer(getTime(),86400000, aLabel);
} else {
theTimerService.createIntervalTimer(new Date(),86400000, aLabel);
}
}