Estoy haciendo funcionar un programa con un Timer, todos los días a una hora específica, el caso es que si lo configuro para que se ejecute a las 0:01 el Timer no se ejecuta, pero por ejemplo si lo configuro para las 0:02 se ejecuta sin ningún tipo de problema, alguien me puede explicar porque pasa esto?
Dejo un ejemplo de lo como tengo todo montado, la clase que uso yo es muy similar a la descrita pero no idéntica, podrá ayudar a entender un poco mejor el problema que planteo.
si necesitáis que aclare algo más hacérmelo saber.
Gracias.
package com.autentia.tutorial.ejb.scheduler;
import java.util.Calendar;
import java.util.Collection;
import java.util.Iterator;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Implementación del servicio @local IAlarmScheduler.
*/
@Stateless
public class AlarmScheduler implements IAlarmScheduler {
private static final Log log = LogFactory.getLog(AlarmScheduler.class);
/** Injección del TimerService */
@Resource TimerService timerService;
/** Hora de ejecución: 23 horas */
private static final int START_HOUR = 23;
/** Minutos de ejecución: 0 minutos */
private static final int START_MINUTES = 0;
/** Segundos de ejecución: 00 */
private static final int START_SECONDS = 0;
/** Intervalo de la ejecución: 1440 = 24 horas */
private static final int INTERVAL_IN_MINUTES = 1440;
/**
* Levanta el servicio
*/
public void startUpTimer() {
log.info("startUpTimer - alarm scheduler service is active.");
shutDownTimer();
Calendar initialExpiration = Calendar.getInstance();
initialExpiration.set(Calendar.HOUR_OF_DAY, START_HOUR );
initialExpiration.set(Calendar.MINUTE, START_MINUTES);
initialExpiration.set(Calendar.SECOND, START_SECONDS);
long intervalDuration = new Integer(INTERVAL_IN_MINUTES).longValue()*60*1000;
log.info("startUpTimer - create new timer service at \""+initialExpiration.getTime()+"\", with \""+intervalDuration+"\" interval in milis.");
timerService.createTimer(initialExpiration.getTime(),intervalDuration,null);
}
/**
* Para el servicio
*/
public void shutDownTimer() {
Collection<Timer> timers = timerService.getTimers();
log.info("shutDownTimer - existing timers? " + timers);
if (timers != null)
{
for (Iterator iterator = timers.iterator(); iterator.hasNext();) {
Timer t = (Timer) iterator.next();
t.cancel();
log.info("shutDownTimer - timer \""+t+"\" canceled.");
}
}
}
/**
* método callback que se invocará al terminar el intervalo definido
*/
@Timeout
public void execute(Timer timer)
{
log.info("executing - " + timer.getInfo());
// TODO: implementar la lógica del proceso de alarmas.
}
}
long intervalDuration = new Integer(INTERVAL_IN_MINUTES).longValue()*60*1000;
se podría simplificar along intervalDuration = INTERVAL_IN_MINUTES*60*1000;