Search in sources :

Example 11 with ITimeRecurrence

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());
}
Also used : QualifiedDayOfWeek(org.pentaho.platform.scheduler2.recur.QualifiedDayOfWeek) ITimeRecurrence(org.pentaho.platform.api.scheduler2.recur.ITimeRecurrence) ComplexJobTrigger(org.pentaho.platform.api.scheduler2.ComplexJobTrigger) IJobTrigger(org.pentaho.platform.api.scheduler2.IJobTrigger) Test(org.junit.Test)

Example 12 with ITimeRecurrence

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());
}
Also used : ComplexJobTrigger(org.pentaho.platform.api.scheduler2.ComplexJobTrigger) IncrementalRecurrence(org.pentaho.platform.scheduler2.recur.IncrementalRecurrence) Calendar(java.util.Calendar) Job(org.pentaho.platform.api.scheduler2.Job) Test(org.junit.Test)

Example 13 with ITimeRecurrence

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);
}
Also used : ComplexJobTrigger(org.pentaho.platform.api.scheduler2.ComplexJobTrigger) Calendar(java.util.Calendar) Test(org.junit.Test)

Example 14 with ITimeRecurrence

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;
}
Also used : ITimeRecurrence(org.pentaho.platform.api.scheduler2.recur.ITimeRecurrence) QualifiedDayOfMonth(org.pentaho.platform.scheduler2.recur.QualifiedDayOfMonth) IncrementalRecurrence(org.pentaho.platform.scheduler2.recur.IncrementalRecurrence) RecurrenceList(org.pentaho.platform.scheduler2.recur.RecurrenceList) ArrayList(java.util.ArrayList) SequentialRecurrence(org.pentaho.platform.scheduler2.recur.SequentialRecurrence)

Example 15 with ITimeRecurrence

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;
}
Also used : ITimeRecurrence(org.pentaho.platform.api.scheduler2.recur.ITimeRecurrence) ComplexJobTrigger(org.pentaho.platform.api.scheduler2.ComplexJobTrigger)

Aggregations

ComplexJobTrigger (org.pentaho.platform.api.scheduler2.ComplexJobTrigger)13 Test (org.junit.Test)12 ITimeRecurrence (org.pentaho.platform.api.scheduler2.recur.ITimeRecurrence)10 RecurrenceList (org.pentaho.platform.scheduler2.recur.RecurrenceList)8 IncrementalRecurrence (org.pentaho.platform.scheduler2.recur.IncrementalRecurrence)7 IJobTrigger (org.pentaho.platform.api.scheduler2.IJobTrigger)6 Calendar (java.util.Calendar)4 Job (org.pentaho.platform.api.scheduler2.Job)4 QualifiedDayOfWeek (org.pentaho.platform.scheduler2.recur.QualifiedDayOfWeek)4 SequentialRecurrence (org.pentaho.platform.scheduler2.recur.SequentialRecurrence)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Serializable (java.io.Serializable)1 Date (java.util.Date)1 CronJobTrigger (org.pentaho.platform.api.scheduler2.CronJobTrigger)1 QualifiedDayOfMonth (org.pentaho.platform.scheduler2.recur.QualifiedDayOfMonth)1 DayOfWeek (org.pentaho.platform.scheduler2.recur.QualifiedDayOfWeek.DayOfWeek)1 ListParamValue (org.pentaho.platform.scheduler2.ws.ListParamValue)1 MapParamValue (org.pentaho.platform.scheduler2.ws.MapParamValue)1 ParamValue (org.pentaho.platform.scheduler2.ws.ParamValue)1