Search in sources :

Example 1 with IncrementalRecurrence

use of org.pentaho.platform.scheduler2.recur.IncrementalRecurrence 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 IncrementalRecurrence

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

the class JaxWsSchedulerServiceIT method testCreateComplexJob.

@Test
public void testCreateComplexJob() throws SchedulerException {
    int startingMinute = (Calendar.getInstance().get(Calendar.MINUTE)) % 60;
    ComplexJobTrigger jobTrigger = new ComplexJobTrigger();
    jobTrigger.setMinuteRecurrence(new IncrementalRecurrence(startingMinute, 1));
    jobTrigger.setHourlyRecurrence((ITimeRecurrence) null);
    System.out.println(jobTrigger.toString());
    schedulerSvc.createComplexJob("test job", jobParams, jobTrigger);
    int tries = 0;
    do {
        sleep(10);
    } while (!MyAction.executed && tries++ < 13);
    Assert.assertTrue("the action was not executed in the expected window", MyAction.executed);
}
Also used : ComplexJobTrigger(org.pentaho.platform.api.scheduler2.ComplexJobTrigger) IncrementalRecurrence(org.pentaho.platform.scheduler2.recur.IncrementalRecurrence) Test(org.junit.Test)

Example 3 with IncrementalRecurrence

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

the class JaxWsSchedulerServiceIT method testUpdateComplexJob.

/*
  public static class TestQuartzScheduler extends QuartzScheduler {
    @Override
    protected String getCurrentUser() {
      SecurityHelper.getInstance().becomeUser( TEST_USER );
      return super.getCurrentUser();
    }
  }
*/
@Test(timeout = 1000 * 5 * 60)
public void testUpdateComplexJob() throws SchedulerException {
    long start = System.currentTimeMillis() + 1000;
    long end = System.currentTimeMillis() + 1000 + 5 * 60 * 60 * 100;
    int startingMinute = (Calendar.getInstance().get(Calendar.MINUTE) + 10) % 60;
    ComplexJobTrigger jobTrigger = new ComplexJobTrigger();
    jobTrigger.setStartTime(new Date(start));
    jobTrigger.setEndTime(new Date(end));
    jobTrigger.setMinuteRecurrence(new IncrementalRecurrence(startingMinute, 1));
    jobTrigger.setHourlyRecurrence((ITimeRecurrence) null);
    System.out.println(jobTrigger.toString());
    String jobId = schedulerSvc.createComplexJob("test job", jobParams, jobTrigger);
    Assert.assertEquals(1, schedulerSvc.getJobs().length);
    jobTrigger = new ComplexJobTrigger();
    start = System.currentTimeMillis() + 2 * 1000;
    end = System.currentTimeMillis() + 1000 + 7 * 60 * 60 * 100;
    jobTrigger.setStartTime(new Date(start));
    jobTrigger.setEndTime(new Date(end));
    startingMinute = (Calendar.getInstance().get(Calendar.MINUTE) + 20) % 60;
    jobTrigger.setMinuteRecurrence(new IncrementalRecurrence(startingMinute, 5));
    jobTrigger.setHourlyRecurrence((ITimeRecurrence) null);
    System.out.println(jobTrigger.toString());
    HashMap<String, ParamValue> newJobParams = new HashMap<String, ParamValue>(jobParams);
    newJobParams.put("newKey", new StringParamValue(""));
    schedulerSvc.updateJobToUseComplexTrigger(jobId, newJobParams, jobTrigger);
    Assert.assertEquals(1, schedulerSvc.getJobs().length);
    Job job = schedulerSvc.getJobs()[0];
    jobTrigger = (ComplexJobTrigger) job.getJobTrigger();
    Assert.assertEquals((Integer) startingMinute, ((IncrementalRecurrence) jobTrigger.getMinuteRecurrences().get(0)).getStartingValue());
    Assert.assertEquals((Integer) 5, ((IncrementalRecurrence) jobTrigger.getMinuteRecurrences().get(0)).getIncrement());
    Assert.assertTrue(job.getJobParams().containsKey("newKey"));
    Assert.assertEquals(new Date(start), jobTrigger.getStartTime());
    Assert.assertEquals(new Date(end), jobTrigger.getEndTime());
}
Also used : ComplexJobTrigger(org.pentaho.platform.api.scheduler2.ComplexJobTrigger) HashMap(java.util.HashMap) IncrementalRecurrence(org.pentaho.platform.scheduler2.recur.IncrementalRecurrence) ListParamValue(org.pentaho.platform.scheduler2.ws.ListParamValue) ParamValue(org.pentaho.platform.scheduler2.ws.ParamValue) StringParamValue(org.pentaho.platform.scheduler2.ws.StringParamValue) MapParamValue(org.pentaho.platform.scheduler2.ws.MapParamValue) StringParamValue(org.pentaho.platform.scheduler2.ws.StringParamValue) Job(org.pentaho.platform.api.scheduler2.Job) Date(java.util.Date) Test(org.junit.Test)

Example 4 with IncrementalRecurrence

use of org.pentaho.platform.scheduler2.recur.IncrementalRecurrence 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)

Example 5 with IncrementalRecurrence

use of org.pentaho.platform.scheduler2.recur.IncrementalRecurrence 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)

Aggregations

IncrementalRecurrence (org.pentaho.platform.scheduler2.recur.IncrementalRecurrence)8 Test (org.junit.Test)5 ComplexJobTrigger (org.pentaho.platform.api.scheduler2.ComplexJobTrigger)5 RecurrenceList (org.pentaho.platform.scheduler2.recur.RecurrenceList)5 SequentialRecurrence (org.pentaho.platform.scheduler2.recur.SequentialRecurrence)5 Job (org.pentaho.platform.api.scheduler2.Job)3 ITimeRecurrence (org.pentaho.platform.api.scheduler2.recur.ITimeRecurrence)3 QualifiedDayOfWeek (org.pentaho.platform.scheduler2.recur.QualifiedDayOfWeek)3 ArrayList (java.util.ArrayList)2 Calendar (java.util.Calendar)2 HashMap (java.util.HashMap)2 Serializable (java.io.Serializable)1 Date (java.util.Date)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 StringParamValue (org.pentaho.platform.scheduler2.ws.StringParamValue)1