Search in sources :

Example 1 with Calendar

use of org.quartz.Calendar in project camel by apache.

the class QuartzCustomCalendarFireTest method testQuartzCustomCronRouteNoFire.

@Test
public void testQuartzCustomCronRouteNoFire() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMinimumMessageCount(0);
    QuartzComponent component = context.getComponent("quartz2", QuartzComponent.class);
    Scheduler scheduler = component.getScheduler();
    Calendar c = scheduler.getCalendar(QuartzConstants.QUARTZ_CAMEL_CUSTOM_CALENDAR);
    Date now = new Date();
    java.util.Calendar tomorrow = java.util.Calendar.getInstance();
    tomorrow.setTime(now);
    tomorrow.add(java.util.Calendar.DAY_OF_MONTH, 1);
    assertEquals(false, c.isTimeIncluded(tomorrow.getTimeInMillis()));
    assertEquals(true, c.isTimeIncluded(now.getTime()));
    assertMockEndpointsSatisfied();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Scheduler(org.quartz.Scheduler) Calendar(org.quartz.Calendar) HolidayCalendar(org.quartz.impl.calendar.HolidayCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 2 with Calendar

use of org.quartz.Calendar in project spring-framework by spring-projects.

the class SchedulerAccessor method registerJobsAndTriggers.

/**
 * Register jobs and triggers (within a transaction, if possible).
 */
protected void registerJobsAndTriggers() throws SchedulerException {
    TransactionStatus transactionStatus = null;
    if (this.transactionManager != null) {
        transactionStatus = this.transactionManager.getTransaction(TransactionDefinition.withDefaults());
    }
    try {
        if (this.jobSchedulingDataLocations != null) {
            ClassLoadHelper clh = new ResourceLoaderClassLoadHelper(this.resourceLoader);
            clh.initialize();
            XMLSchedulingDataProcessor dataProcessor = new XMLSchedulingDataProcessor(clh);
            for (String location : this.jobSchedulingDataLocations) {
                dataProcessor.processFileAndScheduleJobs(location, getScheduler());
            }
        }
        // Register JobDetails.
        if (this.jobDetails != null) {
            for (JobDetail jobDetail : this.jobDetails) {
                addJobToScheduler(jobDetail);
            }
        } else {
            // Create empty list for easier checks when registering triggers.
            this.jobDetails = new ArrayList<>();
        }
        // Register Calendars.
        if (this.calendars != null) {
            for (String calendarName : this.calendars.keySet()) {
                Calendar calendar = this.calendars.get(calendarName);
                getScheduler().addCalendar(calendarName, calendar, true, true);
            }
        }
        // Register Triggers.
        if (this.triggers != null) {
            for (Trigger trigger : this.triggers) {
                addTriggerToScheduler(trigger);
            }
        }
    } catch (Throwable ex) {
        if (transactionStatus != null) {
            try {
                this.transactionManager.rollback(transactionStatus);
            } catch (TransactionException tex) {
                logger.error("Job registration exception overridden by rollback exception", ex);
                throw tex;
            }
        }
        if (ex instanceof SchedulerException) {
            throw (SchedulerException) ex;
        }
        if (ex instanceof Exception) {
            throw new SchedulerException("Registration of jobs and triggers failed: " + ex.getMessage(), ex);
        }
        throw new SchedulerException("Registration of jobs and triggers failed: " + ex.getMessage());
    }
    if (transactionStatus != null) {
        this.transactionManager.commit(transactionStatus);
    }
}
Also used : SchedulerException(org.quartz.SchedulerException) XMLSchedulingDataProcessor(org.quartz.xml.XMLSchedulingDataProcessor) Calendar(org.quartz.Calendar) TransactionStatus(org.springframework.transaction.TransactionStatus) ClassLoadHelper(org.quartz.spi.ClassLoadHelper) SchedulerException(org.quartz.SchedulerException) ObjectAlreadyExistsException(org.quartz.ObjectAlreadyExistsException) TransactionException(org.springframework.transaction.TransactionException) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) TransactionException(org.springframework.transaction.TransactionException)

Example 3 with Calendar

use of org.quartz.Calendar in project camel by apache.

the class QuartzCustomCalendarNoFireTest method testQuartzCustomCronRouteNoFire.

@Test
public void testQuartzCustomCronRouteNoFire() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMinimumMessageCount(0);
    QuartzComponent component = context.getComponent("quartz2", QuartzComponent.class);
    Scheduler scheduler = component.getScheduler();
    Calendar c = scheduler.getCalendar(QuartzConstants.QUARTZ_CAMEL_CUSTOM_CALENDAR);
    Date now = new Date();
    java.util.Calendar tomorrow = java.util.Calendar.getInstance();
    tomorrow.setTime(now);
    tomorrow.add(java.util.Calendar.DAY_OF_MONTH, 1);
    assertEquals(true, c.isTimeIncluded(tomorrow.getTimeInMillis()));
    assertEquals(false, c.isTimeIncluded(now.getTime()));
    assertMockEndpointsSatisfied();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Scheduler(org.quartz.Scheduler) Calendar(org.quartz.Calendar) HolidayCalendar(org.quartz.impl.calendar.HolidayCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 4 with Calendar

use of org.quartz.Calendar 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 5 with Calendar

use of org.quartz.Calendar 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

Calendar (org.quartz.Calendar)6 Scheduler (org.quartz.Scheduler)4 ComplexJobTrigger (org.pentaho.platform.api.scheduler2.ComplexJobTrigger)3 SimpleJobTrigger (org.pentaho.platform.api.scheduler2.SimpleJobTrigger)3 CronTrigger (org.quartz.CronTrigger)3 JobDetail (org.quartz.JobDetail)3 SimpleTrigger (org.quartz.SimpleTrigger)3 Trigger (org.quartz.Trigger)3 Date (java.util.Date)2 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)2 Test (org.junit.Test)2 IJobTrigger (org.pentaho.platform.api.scheduler2.IJobTrigger)2 IScheduler (org.pentaho.platform.api.scheduler2.IScheduler)2 JobTrigger (org.pentaho.platform.api.scheduler2.JobTrigger)2 SchedulerException (org.pentaho.platform.api.scheduler2.SchedulerException)2 HolidayCalendar (org.quartz.impl.calendar.HolidayCalendar)2 Serializable (java.io.Serializable)1 Job (org.pentaho.platform.api.scheduler2.Job)1 ObjectAlreadyExistsException (org.quartz.ObjectAlreadyExistsException)1 SchedulerException (org.quartz.SchedulerException)1