use of org.pentaho.platform.api.scheduler2.recur.ITimeRecurrence in project pentaho-platform by pentaho.
the class SchedulerResourceUtilTest method testConvertScheduleRequestToJobTrigger_ComplexJobTrigger_weeksOfMonth.
@Test
public void testConvertScheduleRequestToJobTrigger_ComplexJobTrigger_weeksOfMonth() throws Exception {
complex.setDaysOfWeek(new int[] { 1, 5 });
complex.setWeeksOfMonth(new int[] { 3, 4 });
when(scheduleRequest.getComplexJobTrigger()).thenReturn(complex);
IJobTrigger trigger = SchedulerResourceUtil.convertScheduleRequestToJobTrigger(scheduleRequest, scheduler);
assertNotNull(trigger);
assertTrue(trigger instanceof ComplexJobTrigger);
ComplexJobTrigger trig = (ComplexJobTrigger) trigger;
List<ITimeRecurrence> recurrences = trig.getDayOfWeekRecurrences().getRecurrences();
assertEquals(4, recurrences.size());
QualifiedDayOfWeek rec = (QualifiedDayOfWeek) recurrences.get(0);
assertEquals("MON", rec.getDayOfWeek().toString());
assertEquals("FOURTH", rec.getQualifier().toString());
rec = (QualifiedDayOfWeek) recurrences.get(1);
assertEquals("MON", rec.getDayOfWeek().toString());
assertEquals("LAST", rec.getQualifier().toString());
rec = (QualifiedDayOfWeek) recurrences.get(2);
assertEquals("FRI", rec.getDayOfWeek().toString());
assertEquals("FOURTH", rec.getQualifier().toString());
rec = (QualifiedDayOfWeek) recurrences.get(3);
assertEquals("FRI", rec.getDayOfWeek().toString());
assertEquals("LAST", rec.getQualifier().toString());
}
use of org.pentaho.platform.api.scheduler2.recur.ITimeRecurrence in project pentaho-platform by pentaho.
the class QuartzSchedulerIT method testStartAndEndDate.
@Test
public void testStartAndEndDate() throws SchedulerException {
ComplexJobTrigger startEndJobTrigger = new ComplexJobTrigger();
ComplexJobTrigger startJobTrigger = new ComplexJobTrigger();
ComplexJobTrigger endJobTrigger = new ComplexJobTrigger();
startEndJobTrigger.setHourlyRecurrence((ITimeRecurrence) null);
startJobTrigger.setHourlyRecurrence((ITimeRecurrence) null);
endJobTrigger.setHourlyRecurrence((ITimeRecurrence) null);
startEndJobTrigger.setMinuteRecurrence((ITimeRecurrence) null);
startJobTrigger.setMinuteRecurrence((ITimeRecurrence) null);
endJobTrigger.setMinuteRecurrence((ITimeRecurrence) null);
startEndJobTrigger.setSecondRecurrence(new IncrementalRecurrence(0, 1));
startJobTrigger.setSecondRecurrence(new IncrementalRecurrence(0, 1));
endJobTrigger.setSecondRecurrence(new IncrementalRecurrence(0, 1));
Calendar calendar = Calendar.getInstance();
int startingMin = calendar.get(Calendar.MINUTE);
int startingSec = calendar.get(Calendar.SECOND) + 3;
if (startingSec > 59) {
startingSec = startingSec % 60;
startingMin++;
if (startingMin > 59) {
startingMin = startingMin % 60;
}
}
calendar.set(Calendar.MINUTE, startingMin);
calendar.set(Calendar.SECOND, startingSec);
startEndJobTrigger.setStartTime(calendar.getTime());
startJobTrigger.setStartTime(calendar.getTime());
calendar.add(Calendar.SECOND, 3);
startEndJobTrigger.setEndTime(calendar.getTime());
endJobTrigger.setEndTime(calendar.getTime());
int startEndCounter = TestAction.counter;
int startCounter = TestAction2.counter;
int endCounter = TestAction3.counter;
Job job = scheduler.createJob("startEndJob", TestAction.class, jobParams, startEndJobTrigger);
Job job2 = scheduler.createJob("startJob", TestAction2.class, jobParams, startJobTrigger);
Job job3 = scheduler.createJob("endJob", TestAction3.class, jobParams, endJobTrigger);
try {
sleep(2);
Assert.assertEquals(startEndCounter, TestAction.counter);
Assert.assertEquals(startCounter, TestAction2.counter);
Assert.assertTrue(endCounter != TestAction3.counter);
endCounter = TestAction3.counter;
sleep(3);
Assert.assertTrue(startEndCounter != TestAction.counter);
Assert.assertTrue(startCounter != TestAction2.counter);
Assert.assertTrue(endCounter != TestAction3.counter);
sleep(3);
startEndCounter = TestAction.counter;
startCounter = TestAction2.counter;
endCounter = TestAction3.counter;
sleep(3);
Assert.assertEquals(startEndCounter, TestAction.counter);
Assert.assertTrue(startCounter != TestAction2.counter);
Assert.assertEquals(endCounter, TestAction3.counter);
} finally {
scheduler.removeJob(job.getJobId());
scheduler.removeJob(job2.getJobId());
scheduler.removeJob(job3.getJobId());
}
Assert.assertEquals(0, scheduler.getJobs(null).size());
}
use of org.pentaho.platform.api.scheduler2.recur.ITimeRecurrence in project pentaho-platform by pentaho.
the class QuartzSchedulerIT method testPauseAndResumeScheduler.
@Test
public void testPauseAndResumeScheduler() throws SchedulerException {
Calendar calendar = Calendar.getInstance();
int startingSec = calendar.get(Calendar.SECOND) + 1;
if (startingSec > 59) {
startingSec = startingSec % 60;
}
ComplexJobTrigger complexJobTrigger = new ComplexJobTrigger();
complexJobTrigger.setHourlyRecurrence((ITimeRecurrence) null);
complexJobTrigger.setMinuteRecurrence((ITimeRecurrence) null);
complexJobTrigger.setSecondRecurrence(startingSec);
String jobName = "complexJob1";
int counter = TestAction.counter;
scheduler.pause();
scheduler.createJob(jobName, TestAction.class, jobParams, complexJobTrigger);
sleep(3);
Assert.assertEquals("Job executed while scheduler is supposedly paused!", counter, TestAction.counter);
Assert.assertEquals("Scheduler is not aware of the dormant job!", 1, scheduler.getJobs(null).size());
scheduler.start();
sleep(3);
Assert.assertTrue("Job did not execute after scheduler started back up!", counter != TestAction.counter);
}
use of org.pentaho.platform.api.scheduler2.recur.ITimeRecurrence in project pentaho-platform by pentaho.
the class QuartzScheduler method parseRecurrence.
private static List<ITimeRecurrence> parseRecurrence(String cronExpression, int tokenIndex) {
List<ITimeRecurrence> timeRecurrence = new ArrayList<ITimeRecurrence>();
// $NON-NLS-1$
String delims = "[ ]+";
String[] tokens = cronExpression.split(delims);
if (tokens.length > tokenIndex) {
String timeTokens = tokens[tokenIndex];
// $NON-NLS-1$
tokens = timeTokens.split(",");
if ((tokens.length > 1) || !(tokens[0].equals("*") || tokens[0].equals("?"))) {
// $NON-NLS-1$ //$NON-NLS-2$
RecurrenceList timeList = null;
for (String token : tokens) {
if (listPattern.matcher(token).matches()) {
if (timeList == null) {
timeList = new RecurrenceList();
}
timeList.getValues().add(Integer.parseInt(token));
} else {
if (timeList != null) {
timeRecurrence.add(timeList);
timeList = null;
}
if (sequencePattern.matcher(token).matches()) {
// $NON-NLS-1$
String[] days = token.split("-");
timeRecurrence.add(new SequentialRecurrence(Integer.parseInt(days[0]), Integer.parseInt(days[1])));
} else if (intervalPattern.matcher(token).matches()) {
// $NON-NLS-1$
String[] days = token.split("/");
timeRecurrence.add(new IncrementalRecurrence(Integer.parseInt(days[0]), Integer.parseInt(days[1])));
} else if ("L".equalsIgnoreCase(token)) {
timeRecurrence.add(new QualifiedDayOfMonth());
} else {
throw new IllegalArgumentException(Messages.getInstance().getErrorString(// $NON-NLS-1$
"ComplexJobTrigger.ERROR_0001_InvalidCronExpression"));
}
}
}
if (timeList != null) {
timeRecurrence.add(timeList);
}
}
} else {
throw new IllegalArgumentException(Messages.getInstance().getErrorString(// $NON-NLS-1$
"ComplexJobTrigger.ERROR_0001_InvalidCronExpression"));
}
return timeRecurrence;
}
use of org.pentaho.platform.api.scheduler2.recur.ITimeRecurrence in project pentaho-platform by pentaho.
the class QuartzScheduler method createComplexTrigger.
public static ComplexJobTrigger createComplexTrigger(String cronExpression) {
ComplexJobTrigger complexJobTrigger = new ComplexJobTrigger();
complexJobTrigger.setHourlyRecurrence((ITimeRecurrence) null);
complexJobTrigger.setMinuteRecurrence((ITimeRecurrence) null);
complexJobTrigger.setSecondRecurrence((ITimeRecurrence) null);
for (ITimeRecurrence recurrence : parseRecurrence(cronExpression, 6)) {
complexJobTrigger.addYearlyRecurrence(recurrence);
}
for (ITimeRecurrence recurrence : parseRecurrence(cronExpression, 4)) {
complexJobTrigger.addMonthlyRecurrence(recurrence);
}
List<ITimeRecurrence> dayOfWeekRecurrences = parseDayOfWeekRecurrences(cronExpression);
List<ITimeRecurrence> dayOfMonthRecurrences = parseRecurrence(cronExpression, 3);
if ((dayOfWeekRecurrences.size() > 0) && (dayOfMonthRecurrences.size() == 0)) {
for (ITimeRecurrence recurrence : dayOfWeekRecurrences) {
complexJobTrigger.addDayOfWeekRecurrence(recurrence);
}
} else if ((dayOfWeekRecurrences.size() == 0) && (dayOfMonthRecurrences.size() > 0)) {
for (ITimeRecurrence recurrence : dayOfMonthRecurrences) {
complexJobTrigger.addDayOfMonthRecurrence(recurrence);
}
}
for (ITimeRecurrence recurrence : parseRecurrence(cronExpression, 2)) {
complexJobTrigger.addHourlyRecurrence(recurrence);
}
for (ITimeRecurrence recurrence : parseRecurrence(cronExpression, 1)) {
complexJobTrigger.addMinuteRecurrence(recurrence);
}
for (ITimeRecurrence recurrence : parseRecurrence(cronExpression, 0)) {
complexJobTrigger.addSecondRecurrence(recurrence);
}
return complexJobTrigger;
}
Aggregations