Search in sources :

Example 96 with CronTrigger

use of org.quartz.CronTrigger in project pentaho-platform by pentaho.

the class QuartzSchedulerIT method createQuartzTriggerComplexTriggerTest.

@Test
public void createQuartzTriggerComplexTriggerTest() throws Exception {
    final Trigger quartzTrigger = QuartzScheduler.createQuartzTrigger(getComplexJobTrigger(), getJobKey());
    assertNotNull(quartzTrigger);
    assertTrue(quartzTrigger instanceof CronTrigger);
    assertEquals(SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW, quartzTrigger.getMisfireInstruction());
    assertEquals(CRON_EXPRESSION, ((CronTrigger) quartzTrigger).getCronExpression());
    assertEquals(USER_NAME, quartzTrigger.getGroup());
}
Also used : CronTrigger(org.quartz.CronTrigger) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) IJobTrigger(org.pentaho.platform.api.scheduler2.IJobTrigger) CronTrigger(org.quartz.CronTrigger) SimpleJobTrigger(org.pentaho.platform.api.scheduler2.SimpleJobTrigger) ComplexJobTrigger(org.pentaho.platform.api.scheduler2.ComplexJobTrigger) Test(org.junit.Test)

Example 97 with CronTrigger

use of org.quartz.CronTrigger in project pentaho-platform by pentaho.

the class QuartzScheduler method triggerNow.

/**
 * {@inheritDoc}
 */
public void triggerNow(String jobId) throws SchedulerException {
    try {
        QuartzJobKey jobKey = QuartzJobKey.parse(jobId);
        Scheduler scheduler = getQuartzScheduler();
        String groupName = jobKey.getUserName();
        for (Trigger trigger : scheduler.getTriggersOfJob(jobId, groupName)) {
            if ("MANUAL_TRIGGER".equals(trigger.getGroup())) {
                continue;
            }
            if (trigger instanceof SimpleTrigger) {
                ((SimpleTrigger) trigger).setPreviousFireTime(new Date());
            } else if (trigger instanceof CronTrigger) {
                ((CronTrigger) trigger).setPreviousFireTime(new Date());
            }
            // force the trigger to be updated with the previous fire time
            scheduler.rescheduleJob(jobId, jobKey.getUserName(), trigger);
        }
        scheduler.triggerJob(jobId, jobKey.getUserName());
    } catch (org.quartz.SchedulerException e) {
        throw new SchedulerException(Messages.getInstance().getString("QuartzScheduler.ERROR_0007_FAILED_TO_GET_JOB", jobId), // $NON-NLS-1$
        e);
    }
}
Also used : CronTrigger(org.quartz.CronTrigger) 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) Scheduler(org.quartz.Scheduler) IScheduler(org.pentaho.platform.api.scheduler2.IScheduler) SimpleTrigger(org.quartz.SimpleTrigger) Date(java.util.Date)

Example 98 with CronTrigger

use of org.quartz.CronTrigger in project pentaho-platform by pentaho.

the class QuartzScheduler method createQuartzTrigger.

public static Trigger createQuartzTrigger(IJobTrigger jobTrigger, QuartzJobKey jobId) throws SchedulerException {
    Trigger quartzTrigger = null;
    if (jobTrigger instanceof ComplexJobTrigger) {
        try {
            quartzTrigger = new CronTrigger(jobId.toString(), jobId.getUserName(), jobTrigger.getCronString() != null ? jobTrigger.getCronString() : QuartzCronStringFactory.createCronString((ComplexJobTrigger) jobTrigger));
        } catch (ParseException e) {
            throw new SchedulerException(Messages.getInstance().getString("QuartzScheduler.ERROR_0001_FAILED_TO_SCHEDULE_JOB", jobId.getJobName()), // $NON-NLS-1$
            e);
        }
    } else if (jobTrigger instanceof SimpleJobTrigger) {
        SimpleJobTrigger simpleTrigger = (SimpleJobTrigger) jobTrigger;
        long interval = simpleTrigger.getRepeatInterval();
        if (interval > 0) {
            interval *= 1000;
        }
        int repeatCount = simpleTrigger.getRepeatCount() < 0 ? SimpleTrigger.REPEAT_INDEFINITELY : simpleTrigger.getRepeatCount();
        quartzTrigger = new SimpleTrigger(jobId.toString(), jobId.getUserName(), simpleTrigger.getStartTime(), simpleTrigger.getEndTime(), repeatCount, interval);
    } else {
        // $NON-NLS-1$
        throw new SchedulerException(Messages.getInstance().getString("QuartzScheduler.ERROR_0002_TRIGGER_WRONG_TYPE"));
    }
    quartzTrigger.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW);
    if (quartzTrigger instanceof SimpleTrigger) {
        if (((SimpleTrigger) quartzTrigger).getRepeatCount() != 0) {
            quartzTrigger.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT);
        }
    }
    return quartzTrigger;
}
Also used : SimpleJobTrigger(org.pentaho.platform.api.scheduler2.SimpleJobTrigger) CronTrigger(org.quartz.CronTrigger) 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) ParseException(java.text.ParseException) SimpleTrigger(org.quartz.SimpleTrigger)

Example 99 with CronTrigger

use of org.quartz.CronTrigger 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 100 with CronTrigger

use of org.quartz.CronTrigger 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());
        }
        if (quartzTrigger instanceof CronTrigger) {
            Serializable timezone = jobParams.get("timezone");
            if (timezone != null) {
                setTimezone((CronTrigger) quartzTrigger, timezone.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) Serializable(java.io.Serializable) 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

CronTrigger (org.quartz.CronTrigger)103 JobDetail (org.quartz.JobDetail)42 Trigger (org.quartz.Trigger)36 SimpleTrigger (org.quartz.SimpleTrigger)29 SchedulerException (org.quartz.SchedulerException)28 Test (org.junit.Test)24 Scheduler (org.quartz.Scheduler)19 TriggerKey (org.quartz.TriggerKey)19 Date (java.util.Date)14 JobKey (org.quartz.JobKey)12 JobDataMap (org.quartz.JobDataMap)11 Test (org.junit.jupiter.api.Test)9 ArrayList (java.util.ArrayList)8 CronScheduleBuilder (org.quartz.CronScheduleBuilder)8 ParseException (java.text.ParseException)7 HashMap (java.util.HashMap)7 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 JobExecutionException (org.quartz.JobExecutionException)6 ComplexJobTrigger (org.pentaho.platform.api.scheduler2.ComplexJobTrigger)5 SimpleJobTrigger (org.pentaho.platform.api.scheduler2.SimpleJobTrigger)5