Search in sources :

Example 1 with RecurrenceList

use of org.pentaho.platform.scheduler2.recur.RecurrenceList in project pentaho-platform by pentaho.

the class QuartzSchedulerIT method runComplexTriggerTest.

@Test
public void runComplexTriggerTest() throws SchedulerException {
    Calendar calendar = Calendar.getInstance();
    // seconds
    final int testTime = 7;
    int startingMinute = calendar.get(Calendar.MINUTE);
    int startingSec = calendar.get(Calendar.SECOND) + 1;
    // we can't finish until end of the minute - sleep for this time
    if (startingSec > 59 - testTime) {
        sleep(59 - startingSec);
        startingSec = 0;
        startingMinute++;
        if (startingMinute > 59) {
            startingMinute = startingMinute % 60;
        }
    }
    int endingSecond = startingSec + 5;
    RecurrenceList recurrenceList = new RecurrenceList(startingSec, endingSecond);
    SequentialRecurrence sequentialRecurrence = new SequentialRecurrence(startingSec, endingSecond);
    IncrementalRecurrence incrementalRecurrence = new IncrementalRecurrence(startingSec, 1);
    ComplexJobTrigger jobTrigger = new ComplexJobTrigger();
    jobTrigger.setHourlyRecurrence((ITimeRecurrence) null);
    jobTrigger.setMinuteRecurrence(startingMinute);
    jobTrigger.setSecondRecurrence(recurrenceList);
    System.out.println(jobTrigger.toString());
    String jobName = "complexJob1";
    Job job = scheduler.createJob(jobName, TestAction.class, new HashMap<String, Serializable>(), jobTrigger);
    Assert.assertTrue(job.getJobName().contains(jobName));
    Assert.assertEquals(job.getSchedulableClass(), TestAction.class.getName());
    Assert.assertEquals(job.getJobTrigger().toString(), jobTrigger.toString());
    jobTrigger.setSecondRecurrence(sequentialRecurrence);
    jobName = "complexJob2";
    job = scheduler.createJob(jobName, TestAction2.class, new HashMap<String, Serializable>(), jobTrigger);
    Assert.assertTrue(job.getJobName().contains(jobName));
    Assert.assertEquals(job.getSchedulableClass(), TestAction2.class.getName());
    Assert.assertEquals(job.getJobTrigger().toString(), jobTrigger.toString());
    jobTrigger.setSecondRecurrence(incrementalRecurrence);
    jobName = "complexJob3";
    job = scheduler.createJob(jobName, TestAction3.class, new HashMap<String, Serializable>(), jobTrigger);
    Assert.assertTrue(job.getJobName().contains(jobName));
    Assert.assertEquals(job.getSchedulableClass(), TestAction3.class.getName());
    Assert.assertEquals(job.getJobTrigger().toString(), jobTrigger.toString());
    // 
    // Wait for the jobs to complete, then check that each action executed the correct number of times
    // 
    sleep(testTime);
    Assert.assertEquals("Job did not run the correct number of times", 2, TestAction.counter);
    Assert.assertEquals("Job did not run the correct number of times", 6, TestAction2.counter);
    System.out.println("Counter = " + TestAction3.counter);
    Assert.assertTrue("Job did not run.", TestAction3.counter > 3);
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) Calendar(java.util.Calendar) RecurrenceList(org.pentaho.platform.scheduler2.recur.RecurrenceList) SequentialRecurrence(org.pentaho.platform.scheduler2.recur.SequentialRecurrence) ComplexJobTrigger(org.pentaho.platform.api.scheduler2.ComplexJobTrigger) IncrementalRecurrence(org.pentaho.platform.scheduler2.recur.IncrementalRecurrence) Job(org.pentaho.platform.api.scheduler2.Job) Test(org.junit.Test)

Example 2 with RecurrenceList

use of org.pentaho.platform.scheduler2.recur.RecurrenceList in project pentaho-platform by pentaho.

the class SchedulerResourceUtilTest method testConvertScheduleRequestToJobTrigger_ComplexJobTrigger_daysOfMonth.

@Test
public void testConvertScheduleRequestToJobTrigger_ComplexJobTrigger_daysOfMonth() throws Exception {
    complex.setDaysOfMonth(new int[] { 1, 25 });
    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.getDayOfMonthRecurrences().getRecurrences();
    assertEquals(2, recurrences.size());
    RecurrenceList rec = (RecurrenceList) recurrences.get(0);
    assertEquals(1, rec.getValues().get(0).intValue());
    rec = (RecurrenceList) recurrences.get(1);
    assertEquals(25, rec.getValues().get(0).intValue());
}
Also used : ITimeRecurrence(org.pentaho.platform.api.scheduler2.recur.ITimeRecurrence) ComplexJobTrigger(org.pentaho.platform.api.scheduler2.ComplexJobTrigger) RecurrenceList(org.pentaho.platform.scheduler2.recur.RecurrenceList) IJobTrigger(org.pentaho.platform.api.scheduler2.IJobTrigger) Test(org.junit.Test)

Example 3 with RecurrenceList

use of org.pentaho.platform.scheduler2.recur.RecurrenceList in project pentaho-platform by pentaho.

the class SchedulerResourceUtilTest method testConvertScheduleRequestToJobTrigger_ComplexJobTrigger_daysOfWeek.

@Test
public void testConvertScheduleRequestToJobTrigger_ComplexJobTrigger_daysOfWeek() throws Exception {
    complex.setDaysOfWeek(new int[] { 1, 5 });
    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(2, recurrences.size());
    RecurrenceList rec = (RecurrenceList) recurrences.get(0);
    assertEquals(2, rec.getValues().get(0).intValue());
    rec = (RecurrenceList) recurrences.get(1);
    assertEquals(6, rec.getValues().get(0).intValue());
}
Also used : ITimeRecurrence(org.pentaho.platform.api.scheduler2.recur.ITimeRecurrence) ComplexJobTrigger(org.pentaho.platform.api.scheduler2.ComplexJobTrigger) RecurrenceList(org.pentaho.platform.scheduler2.recur.RecurrenceList) IJobTrigger(org.pentaho.platform.api.scheduler2.IJobTrigger) Test(org.junit.Test)

Example 4 with RecurrenceList

use of org.pentaho.platform.scheduler2.recur.RecurrenceList in project pentaho-platform by pentaho.

the class SchedulerResourceUtilTest method testConvertScheduleRequestToJobTrigger_ComplexJobTrigger_monthsOfYear.

@Test
public void testConvertScheduleRequestToJobTrigger_ComplexJobTrigger_monthsOfYear() throws Exception {
    complex.setMonthsOfYear(new int[] { 1, 8 });
    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.getMonthlyRecurrences().getRecurrences();
    assertEquals(2, recurrences.size());
    RecurrenceList rec = (RecurrenceList) recurrences.get(0);
    assertEquals(2, rec.getValues().get(0).intValue());
    rec = (RecurrenceList) recurrences.get(1);
    assertEquals(9, rec.getValues().get(0).intValue());
}
Also used : ITimeRecurrence(org.pentaho.platform.api.scheduler2.recur.ITimeRecurrence) ComplexJobTrigger(org.pentaho.platform.api.scheduler2.ComplexJobTrigger) RecurrenceList(org.pentaho.platform.scheduler2.recur.RecurrenceList) IJobTrigger(org.pentaho.platform.api.scheduler2.IJobTrigger) Test(org.junit.Test)

Example 5 with RecurrenceList

use of org.pentaho.platform.scheduler2.recur.RecurrenceList in project pentaho-platform by pentaho.

the class QuartzScheduler method parseDayOfWeekRecurrences.

private static List<ITimeRecurrence> parseDayOfWeekRecurrences(String cronExpression) {
    List<ITimeRecurrence> dayOfWeekRecurrence = new ArrayList<ITimeRecurrence>();
    // $NON-NLS-1$
    String delims = "[ ]+";
    String[] tokens = cronExpression.split(delims);
    if (tokens.length >= 6) {
        String dayOfWeekTokens = tokens[5];
        // $NON-NLS-1$
        tokens = dayOfWeekTokens.split(",");
        if ((tokens.length > 1) || !(tokens[0].equals("*") || tokens[0].equals("?"))) {
            // $NON-NLS-1$ //$NON-NLS-2$
            RecurrenceList dayOfWeekList = null;
            for (String token : tokens) {
                if (listPattern.matcher(token).matches()) {
                    if (dayOfWeekList == null) {
                        dayOfWeekList = new RecurrenceList();
                    }
                    dayOfWeekList.getValues().add(Integer.parseInt(token));
                } else {
                    if (dayOfWeekList != null) {
                        dayOfWeekRecurrence.add(dayOfWeekList);
                        dayOfWeekList = null;
                    }
                    if (sequencePattern.matcher(token).matches()) {
                        // $NON-NLS-1$
                        String[] days = token.split("-");
                        dayOfWeekRecurrence.add(new SequentialRecurrence(Integer.parseInt(days[0]), Integer.parseInt(days[1])));
                    } else if (intervalPattern.matcher(token).matches()) {
                        // $NON-NLS-1$
                        String[] days = token.split("/");
                        dayOfWeekRecurrence.add(new IncrementalRecurrence(Integer.parseInt(days[0]), Integer.parseInt(days[1])));
                    } else if (qualifiedDayPattern.matcher(token).matches()) {
                        // $NON-NLS-1$
                        String[] days = token.split("#");
                        dayOfWeekRecurrence.add(new QualifiedDayOfWeek(Integer.parseInt(days[1]), Integer.parseInt(days[0])));
                    } else if (lastDayPattern.matcher(token).matches()) {
                        DayOfWeek dayOfWeek = DayOfWeek.values()[(Integer.parseInt(token.substring(0, token.length() - 1)) - 1) % 7];
                        dayOfWeekRecurrence.add(new QualifiedDayOfWeek(DayOfWeekQualifier.LAST, dayOfWeek));
                    } else if (dayOfWeekRangePattern.matcher(token).matches()) {
                        // $NON-NLS-1$
                        String[] days = token.split("-");
                        int start = DayOfWeek.valueOf(days[0]).ordinal();
                        int finish = DayOfWeek.valueOf(days[1]).ordinal();
                        dayOfWeekRecurrence.add(new SequentialRecurrence(start, finish));
                    } else {
                        dayOfWeekList = new RecurrenceList();
                        dayOfWeekList.getValues().add(DayOfWeek.valueOf(token).ordinal());
                        dayOfWeekRecurrence.add(dayOfWeekList);
                        dayOfWeekList = null;
                    // } else {
                    // throw new IllegalArgumentException(Messages.getInstance().getErrorString(
                    // "ComplexJobTrigger.ERROR_0001_InvalidCronExpression")); //$NON-NLS-1$
                    }
                }
            }
            if (dayOfWeekList != null) {
                dayOfWeekRecurrence.add(dayOfWeekList);
            }
        }
    } else {
        throw new IllegalArgumentException(Messages.getInstance().getErrorString(// $NON-NLS-1$
        "ComplexJobTrigger.ERROR_0001_InvalidCronExpression"));
    }
    return dayOfWeekRecurrence;
}
Also used : QualifiedDayOfWeek(org.pentaho.platform.scheduler2.recur.QualifiedDayOfWeek) DayOfWeek(org.pentaho.platform.scheduler2.recur.QualifiedDayOfWeek.DayOfWeek) QualifiedDayOfWeek(org.pentaho.platform.scheduler2.recur.QualifiedDayOfWeek) ITimeRecurrence(org.pentaho.platform.api.scheduler2.recur.ITimeRecurrence) 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)

Aggregations

RecurrenceList (org.pentaho.platform.scheduler2.recur.RecurrenceList)9 ITimeRecurrence (org.pentaho.platform.api.scheduler2.recur.ITimeRecurrence)7 Test (org.junit.Test)6 ComplexJobTrigger (org.pentaho.platform.api.scheduler2.ComplexJobTrigger)6 IncrementalRecurrence (org.pentaho.platform.scheduler2.recur.IncrementalRecurrence)5 SequentialRecurrence (org.pentaho.platform.scheduler2.recur.SequentialRecurrence)5 IJobTrigger (org.pentaho.platform.api.scheduler2.IJobTrigger)4 QualifiedDayOfWeek (org.pentaho.platform.scheduler2.recur.QualifiedDayOfWeek)3 ArrayList (java.util.ArrayList)2 Serializable (java.io.Serializable)1 Calendar (java.util.Calendar)1 HashMap (java.util.HashMap)1 Job (org.pentaho.platform.api.scheduler2.Job)1 QualifiedDayOfMonth (org.pentaho.platform.scheduler2.recur.QualifiedDayOfMonth)1 DayOfWeek (org.pentaho.platform.scheduler2.recur.QualifiedDayOfWeek.DayOfWeek)1