I got this problem:
I have a text field,
There should be a CRON expression written, and later on saved.
Now I need a method to convert the CRON string (here are some random examples: http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/crontrigger.html) to java ScheduleExpression (http://docs.oracle.com/javaee/6/api/javax/ejb/ScheduleExpression.html)
But, I have no idea how to do it...
I have a timer based execution system, that run only on days, weeks and months, but now I need to implement the CRON models, so that the executions can be run on a specific period of time...
here is a little code, just to back me up:
@Resource
private TimerService timerService;
@Timeout
public void execute(Timer timer) {
Script s = (Script) timer.getInfo();
execute(s, true);
System.out.println("Timer Service : " + s.getScriptId());
System.out.println("Current Time : " + new Date());
System.out.println("Next Timeout : " + timer.getNextTimeout());
System.out.println("Time Remaining : " + timer.getTimeRemaining());
System.out.println("____________________________________________");
Date today = new Date();
if (s.getTimerSetup().getEndDate() <= today.getTime()) {
stopTimer(s);
}
}
@Override
public void startTimer(Script s) {
if (s.getTimerSetup().getTimerRepeat().equals("0")) {
return;
}
s.setStatus(true);
em.merge(s);
em.flush();
if (s.getTimerSetup().getEndDate() > System.currentTimeMillis()) {
long timeOut = 1L;
String timerRepeat = s.getTimerSetup().getTimerRepeat();
if (timerRepeat.equals("1")) {// day
timeOut = 1000L * 60L * 60L * 24L;
} else if (timerRepeat.equals("2")) {// week
timeOut = 1000L * 60L * 60L * 24L * 7L;
} else if (timerRepeat.equals("3")) {// month
timeOut = 1000L * 60L * 60L * 24L * 30L;
} else {
return; //Here is the part where the cron string is detected
}
long initialTimeOut = s.getTimerSetup().getStartDate() - System.currentTimeMillis();
if (initialTimeOut < 0) {
long initCheck = initialTimeOut * -1;
while (initCheck > timeOut) {
initCheck -= timeOut;
}
initialTimeOut = timeOut - initCheck;
}
Boolean found = false;
if (timerService.getAllTimers().size() == 0) {
System.out.println("Started the timer for the script: " + s.getFileName());
timerService.createTimer(initialTimeOut, timeOut, s);
} else {
for (Timer timer : timerService.getAllTimers()) {
if (((Script) timer.getInfo()).getScriptId() == s.getScriptId()) {
System.out.println("This script's timer was already started!");
found = true;
}
}
if (!found) {
System.out.println("Started the timer for the script: " + s.getFileName());
timerService.createTimer(initialTimeOut, timeOut, s);
found = true;
}
}
} else {
System.out.println("The script's end date has expired");
}
}
I marked the place where the cron string is detected (in the if statement) And I need now to transform the string to a ScheduleExpression.
And after that to run it with the normal timers. (but that comes later :))
Please help. Thanks in advance.