Search in sources :

Example 21 with ComplexJobTrigger

use of org.pentaho.platform.api.scheduler2.ComplexJobTrigger 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 22 with ComplexJobTrigger

use of org.pentaho.platform.api.scheduler2.ComplexJobTrigger in project pentaho-platform by pentaho.

the class QuartzSchedulerIT method testRemoveJob.

@Test
public void testRemoveJob() throws SchedulerException {
    scheduler.pause();
    ComplexJobTrigger trigger = new ComplexJobTrigger();
    trigger.addYearlyRecurrence(2050);
    scheduler.createJob("testName1", TestAction.class, jobParams, trigger);
    Assert.assertEquals(1, scheduler.getJobs(null).size());
    for (Job oldJob : scheduler.getJobs(null)) {
        scheduler.removeJob(oldJob.getJobId());
    }
    Assert.assertEquals(0, scheduler.getJobs(null).size());
}
Also used : ComplexJobTrigger(org.pentaho.platform.api.scheduler2.ComplexJobTrigger) Job(org.pentaho.platform.api.scheduler2.Job) Test(org.junit.Test)

Example 23 with ComplexJobTrigger

use of org.pentaho.platform.api.scheduler2.ComplexJobTrigger in project pentaho-platform by pentaho.

the class QuartzScheduler method setJobTrigger.

private void setJobTrigger(Scheduler scheduler, Job job, Trigger trigger) throws SchedulerException, org.quartz.SchedulerException {
    QuartzJobKey jobKey = QuartzJobKey.parse(job.getJobId());
    String groupName = jobKey.getUserName();
    if (trigger instanceof SimpleTrigger) {
        SimpleTrigger simpleTrigger = (SimpleTrigger) trigger;
        SimpleJobTrigger simpleJobTrigger = new SimpleJobTrigger();
        simpleJobTrigger.setStartTime(simpleTrigger.getStartTime());
        simpleJobTrigger.setEndTime(simpleTrigger.getEndTime());
        simpleJobTrigger.setUiPassParam((String) job.getJobParams().get(RESERVEDMAPKEY_UIPASSPARAM));
        long interval = simpleTrigger.getRepeatInterval();
        if (interval > 0) {
            interval /= 1000;
        }
        simpleJobTrigger.setRepeatInterval(interval);
        simpleJobTrigger.setRepeatCount(simpleTrigger.getRepeatCount());
        job.setJobTrigger(simpleJobTrigger);
    } else if (trigger instanceof CronTrigger) {
        CronTrigger cronTrigger = (CronTrigger) trigger;
        ComplexJobTrigger complexJobTrigger = createComplexTrigger(cronTrigger.getCronExpression());
        complexJobTrigger.setUiPassParam((String) job.getJobParams().get(RESERVEDMAPKEY_UIPASSPARAM));
        complexJobTrigger.setCronString(((CronTrigger) trigger).getCronExpression());
        job.setJobTrigger(complexJobTrigger);
        if (trigger.getCalendarName() != null) {
            Calendar calendar = scheduler.getCalendar(trigger.getCalendarName());
            if (calendar instanceof QuartzSchedulerAvailability) {
                QuartzSchedulerAvailability quartzSchedulerAvailability = (QuartzSchedulerAvailability) calendar;
                complexJobTrigger.setStartTime(quartzSchedulerAvailability.getStartTime());
                complexJobTrigger.setEndTime(quartzSchedulerAvailability.getEndTime());
            }
        }
        complexJobTrigger.setCronString(((CronTrigger) trigger).getCronExpression());
    }
    int triggerState = scheduler.getTriggerState(job.getJobId(), groupName);
    switch(triggerState) {
        case Trigger.STATE_NORMAL:
            job.setState(JobState.NORMAL);
            break;
        case Trigger.STATE_BLOCKED:
            job.setState(JobState.BLOCKED);
            break;
        case Trigger.STATE_COMPLETE:
            job.setState(JobState.COMPLETE);
            break;
        case Trigger.STATE_ERROR:
            job.setState(JobState.ERROR);
            break;
        case Trigger.STATE_PAUSED:
            job.setState(JobState.PAUSED);
            break;
        default:
            job.setState(JobState.UNKNOWN);
            break;
    }
    job.setJobName(QuartzJobKey.parse(job.getJobId()).getJobName());
    job.setNextRun(trigger.getNextFireTime());
    job.setLastRun(trigger.getPreviousFireTime());
}
Also used : SimpleJobTrigger(org.pentaho.platform.api.scheduler2.SimpleJobTrigger) CronTrigger(org.quartz.CronTrigger) ComplexJobTrigger(org.pentaho.platform.api.scheduler2.ComplexJobTrigger) Calendar(org.quartz.Calendar) SimpleTrigger(org.quartz.SimpleTrigger)

Example 24 with ComplexJobTrigger

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

Example 25 with ComplexJobTrigger

use of org.pentaho.platform.api.scheduler2.ComplexJobTrigger in project pentaho-platform by pentaho.

the class QuartzScheduler method updateJob.

@Override
public void updateJob(String jobId, Map<String, Serializable> jobParams, IJobTrigger trigger) throws SchedulerException {
    QuartzJobKey jobKey = QuartzJobKey.parse(jobId);
    Trigger quartzTrigger = createQuartzTrigger(trigger, jobKey);
    quartzTrigger.setJobName(jobId);
    quartzTrigger.setJobGroup(jobKey.getUserName());
    Calendar triggerCalendar = quartzTrigger instanceof CronTrigger ? createQuartzCalendar((ComplexJobTrigger) trigger) : null;
    try {
        Scheduler scheduler = getQuartzScheduler();
        // int triggerState = scheduler.getTriggerState(jobId, jobKey.getUserName());
        // if (triggerState != Trigger.STATE_PAUSED) {
        // scheduler.pauseTrigger(jobId, jobKey.getUserName());
        // }
        JobDetail origJobDetail = scheduler.getJobDetail(jobId, jobKey.getUserName());
        if (origJobDetail.getJobDataMap().containsKey(RESERVEDMAPKEY_ACTIONCLASS)) {
            jobParams.put(RESERVEDMAPKEY_ACTIONCLASS, origJobDetail.getJobDataMap().get(RESERVEDMAPKEY_ACTIONCLASS).toString());
        } else if (origJobDetail.getJobDataMap().containsKey(RESERVEDMAPKEY_ACTIONID)) {
            jobParams.put(RESERVEDMAPKEY_ACTIONID, origJobDetail.getJobDataMap().get(RESERVEDMAPKEY_ACTIONID).toString());
        }
        if (origJobDetail.getJobDataMap().containsKey(RESERVEDMAPKEY_STREAMPROVIDER)) {
            jobParams.put(RESERVEDMAPKEY_STREAMPROVIDER, (Serializable) origJobDetail.getJobDataMap().get(RESERVEDMAPKEY_STREAMPROVIDER));
        }
        if (origJobDetail.getJobDataMap().containsKey(RESERVEDMAPKEY_UIPASSPARAM)) {
            jobParams.put(RESERVEDMAPKEY_UIPASSPARAM, (Serializable) origJobDetail.getJobDataMap().get(RESERVEDMAPKEY_UIPASSPARAM));
        }
        JobDetail jobDetail = createJobDetails(jobKey, jobParams);
        scheduler.addJob(jobDetail, true);
        if (triggerCalendar != null) {
            scheduler.addCalendar(jobId.toString(), triggerCalendar, true, true);
            quartzTrigger.setCalendarName(jobId.toString());
        }
        scheduler.rescheduleJob(jobId, jobKey.getUserName(), quartzTrigger);
        // if (triggerState != Trigger.STATE_PAUSED) {
        // scheduler.resumeTrigger(jobId, jobKey.getUserName());
        // }
        logger.debug(MessageFormat.format("Scheduling job {0} with trigger {1} and job parameters [ {2} ]", jobId.toString(), trigger, // $NON-NLS-1$
        prettyPrintMap(jobParams)));
    } catch (org.quartz.SchedulerException e) {
        throw new SchedulerException(Messages.getInstance().getString("QuartzScheduler.ERROR_0001_FAILED_TO_SCHEDULE_JOB", jobKey.getJobName()), // $NON-NLS-1$
        e);
    }
}
Also used : CronTrigger(org.quartz.CronTrigger) JobDetail(org.quartz.JobDetail) CronTrigger(org.quartz.CronTrigger) SimpleJobTrigger(org.pentaho.platform.api.scheduler2.SimpleJobTrigger) JobTrigger(org.pentaho.platform.api.scheduler2.JobTrigger) ComplexJobTrigger(org.pentaho.platform.api.scheduler2.ComplexJobTrigger) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) IJobTrigger(org.pentaho.platform.api.scheduler2.IJobTrigger) SchedulerException(org.pentaho.platform.api.scheduler2.SchedulerException) ComplexJobTrigger(org.pentaho.platform.api.scheduler2.ComplexJobTrigger) Scheduler(org.quartz.Scheduler) IScheduler(org.pentaho.platform.api.scheduler2.IScheduler) Calendar(org.quartz.Calendar)

Aggregations

ComplexJobTrigger (org.pentaho.platform.api.scheduler2.ComplexJobTrigger)28 Test (org.junit.Test)21 IJobTrigger (org.pentaho.platform.api.scheduler2.IJobTrigger)12 Job (org.pentaho.platform.api.scheduler2.Job)12 Calendar (java.util.Calendar)8 SimpleJobTrigger (org.pentaho.platform.api.scheduler2.SimpleJobTrigger)8 ITimeRecurrence (org.pentaho.platform.api.scheduler2.recur.ITimeRecurrence)7 RecurrenceList (org.pentaho.platform.scheduler2.recur.RecurrenceList)6 IncrementalRecurrence (org.pentaho.platform.scheduler2.recur.IncrementalRecurrence)5 Serializable (java.io.Serializable)4 Date (java.util.Date)4 GregorianCalendar (java.util.GregorianCalendar)4 QualifiedDayOfWeek (org.pentaho.platform.scheduler2.recur.QualifiedDayOfWeek)4 HashMap (java.util.HashMap)3 JobTrigger (org.pentaho.platform.api.scheduler2.JobTrigger)3 Calendar (org.quartz.Calendar)3 CronTrigger (org.quartz.CronTrigger)3 SimpleTrigger (org.quartz.SimpleTrigger)3 CronJobTrigger (org.pentaho.platform.api.scheduler2.CronJobTrigger)2 IBackgroundExecutionStreamProvider (org.pentaho.platform.api.scheduler2.IBackgroundExecutionStreamProvider)2