import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.ScheduleExpression;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerConfig;
import javax.ejb.TimerService;
/**
*
* @author mmazzocchetti
*/
public abstract class BaseTimer {
@Resource
private TimerService timerService;
private Logger logger;
private final String BASE_SCHEDULER
= "scheduler.";
/**
*
* @param name
*/
public BaseTimer
(String name
) {
this.name = name;
}
/**
*
*/
@PostConstruct
public void initTimer() {
getLogger().info("Initialization...");
try {
getLogger().info("Checking parameters...");
if (checkParameters()) {
getLogger().info("Parameters check successfull");
} else {
getLogger().info("Parameters check skipped");
}
getLogger().log(Level.SEVERE, "Error on parameters check: " + e.getMessage(), e);
return;
}
try {
if (isEnabled()) {
setTimer();
getLogger().info("Initialized...");
} else {
getLogger().info("Initialized (as disabled)...");
}
getLogger().log(Level.SEVERE, "Error on get properties timer: " + e.getMessage(), e);
return;
}
}
getLogger().fine("Setting a programmatic timeout every " + getSeconds() + " seconds for " + name + ".");
ScheduleExpression scheduleExpression = new ScheduleExpression();
scheduleExpression.second(getSeconds());
scheduleExpression.minute(getMinutes());
scheduleExpression.hour(getHours());
scheduleExpression.dayOfWeek(getDayOfTheWeek());
scheduleExpression.month(getMonth());
timerService.createCalendarTimer(scheduleExpression, new TimerConfig(name, false));
}
/**
*
* @param timer
*/
@Timeout
public void timerTick
(Timer timer
) {
getLogger().fine("Timer tick - Start");
executeTick();
getLogger().fine("Timer tick - End");
}
/**
*
* @return
*/
protected Logger getLogger() {
if (logger == null) {
logger = Logger.getLogger(this.getClass().getName());
}
return logger;
}
/**
*
*/
protected void removeTimer() {
if (timerService.getTimers() != null) {
for (Timer timer
: timerService.
getTimers()) {
if (timer.getInfo().equals(name)) {
timer.cancel();
getLogger().info("Timer removed");
}
}
}
}
private void executeTick() {
try {
if (!isInOperationTime()) {
logger.fine("Out of operation time window...");
return;
}
getLogger().fine("In operation time window...");
execute();
getLogger().log(Level.SEVERE, "Error on executeTick: " + e.getMessage(), e);
}
}
private boolean isInOperationTime() {
try {
start = getStartOperationalTime();
if (start == null) {
return true;
}
Date end
= getEndOperationalTime
();
if (end == null) {
return true;
}
return start.before(actual) && end.after(actual);
getLogger().log(Level.SEVERE, "Error on executeTick, for operational time: ", ex);
return false;
}
}
/**
*
* @return @throws Exception
*/
protected boolean isEnabled
() throws Exception {
return getProperty(BASE_SCHEDULER + getTimerNameProperties() + ".enabled", "false").equalsIgnoreCase("true");
}
/**
*
* @return @throws Exception
*/
start
= DateUtils.
setHours(start,
Integer.
parseInt(getProperty
(BASE_SCHEDULER
+ getTimerNameProperties
() + ".starttime",
"0")));
start = DateUtils.setMinutes(start, 0);
start = DateUtils.setSeconds(start, 0);
return start;
}
/**
*
* @return @throws Exception
*/
end
= DateUtils.
setHours(end,
Integer.
parseInt(getProperty
(BASE_SCHEDULER
+ getTimerNameProperties
() + ".endtime",
"0")));
end = DateUtils.setMinutes(end, 0);
end = DateUtils.setSeconds(end, 0);
return end;
}
/**
* Returns the default interval in milliseconds
*
* @return
* @throws java.lang.Exception
*/
protected int getSeconds
() throws Exception {
int seconds;
seconds
= Integer.
parseInt(getProperty
(BASE_SCHEDULER
+ getTimerNameProperties
() + ".seconds",
"0"));
return seconds;
}
/**
*
* @return @throws Exception
*/
protected int getMinutes
() throws Exception {
int minutes;
minutes
= Integer.
parseInt(getProperty
(BASE_SCHEDULER
+ getTimerNameProperties
() + ".minutes",
"0"));
return minutes;
}
/**
*
* @return @throws Exception
*/
int hours;
hours
= Integer.
parseInt(getProperty
(BASE_SCHEDULER
+ getTimerNameProperties
() + ".hours",
"0"));
return hours;
}
/**
*
* @return @throws Exception
*/
protected int getDayOfTheWeek
() throws Exception {
int dayOfTheWeek;
dayOfTheWeek
= Integer.
parseInt(getProperty
(BASE_SCHEDULER
+ getTimerNameProperties
() + ".day",
"0"));
return dayOfTheWeek;
}
/**
*
* @return @throws Exception
*/
int month;
month
= Integer.
parseInt(getProperty
(BASE_SCHEDULER
+ getTimerNameProperties
() + ".month",
"0"));
return month;
}
/**
*
* @return @throws Exception
*/
protected int getMaxRetries
() throws Exception {
int retries;
retries
= Integer.
parseInt(getProperty
(BASE_SCHEDULER
+ getTimerNameProperties
() + ".retries",
"2"));
return retries;
}
/**
*
* Da implementare con recupero da file di properties o tabella in base
* alle esigenze
*
* @param key
* @param defaultValue
* @return Stringa recuperata da configurazione o valore di default
*/
/**
* Restituisce il nome del timer che verrà cercato nelle properties
*
* @return Nome della properties da cercare
*/
protected abstract String getTimerNameProperties
();
/**
* Logiche di esecuzione al Tick del timer
*/
protected abstract void execute();
/**
* Effettua il check dei parametri. Implementazione opzionale, se vanno
* bene eventuali valori di default inserire un return true;
*
*
* @return Ritorna false se i parametri non sono stati verificati, true
* altrimenti
* @throws Exception
*/
protected abstract boolean checkParameters
() throws Exception;
}