Search in sources :

Example 26 with CronExpression

use of org.quartz.CronExpression in project JSpiderCluster by xiyuan-fengyu.

the class OnTimeTask method updateCron.

public boolean updateCron(String newCron) {
    if (CronExpression.isValidExpression(newCron)) {
        try {
            cron = new CronExpression(newCron);
            cronStr = newCron;
            beforeExcute();
            return true;
        } catch (Exception e) {
            return false;
        }
    } else {
        return false;
    }
}
Also used : CronExpression(org.quartz.CronExpression) ParseException(java.text.ParseException)

Example 27 with CronExpression

use of org.quartz.CronExpression in project plugin-vm by ligoj.

the class VmResource method writeSchedules.

/**
 * Write all schedules.
 */
private void writeSchedules(final OutputStream output, Collection<VmSchedule> schedules, final Map<Integer, VmExecution> lastExecutions) throws IOException {
    final Writer writer = new BufferedWriter(new OutputStreamWriter(output, "cp1252"));
    final FastDateFormat df = FastDateFormat.getInstance("yyyy/MM/dd HH:mm:ss");
    final Date now = DateUtils.newCalendar().getTime();
    writer.write("subscription;project;projectKey;projectName;node;cron;operation;lastDateHMS;lastTimestamp;lastOperation;vm;lastTrigger;lastSucceed;lastStatusText;lastErrorText;nextDateHMS;nextTimestamp");
    for (final VmSchedule schedule : schedules) {
        // The last execution of the related schedule
        final VmExecution execution = lastExecutions.get(schedule.getSubscription().getId());
        writeCommon(writer, schedule.getSubscription());
        writer.write(';');
        writer.write(schedule.getCron());
        writer.write(';');
        writer.write(schedule.getOperation().name());
        if (execution == null) {
            writer.write(";;;;;;;;");
        } else {
            // Last execution
            writeExecutionStatus(writer, execution, df);
        }
        // Next execution
        try {
            final Date next = new CronExpression(schedule.getCron()).getNextValidTimeAfter(now);
            writer.write(';');
            writer.write(df.format(next));
            writer.write(';');
            writer.write(String.valueOf(next.getTime()));
        } catch (final ParseException pe) {
            // Non blocking error
            log.error("Invalid CRON expression {}", schedule.getCron());
            writer.write(";ERROR;ERROR");
        }
    }
    // Ensure buffer is flushed
    writer.flush();
}
Also used : VmExecution(org.ligoj.app.plugin.vm.model.VmExecution) OutputStreamWriter(java.io.OutputStreamWriter) FastDateFormat(org.apache.commons.lang3.time.FastDateFormat) CronExpression(org.quartz.CronExpression) ParseException(java.text.ParseException) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) Date(java.util.Date) VmSchedule(org.ligoj.app.plugin.vm.model.VmSchedule) BufferedWriter(java.io.BufferedWriter)

Example 28 with CronExpression

use of org.quartz.CronExpression in project openolat by klemens.

the class ReminderModuleTest method testCronJob_everyHeightHours.

@Test
public void testCronJob_everyHeightHours() throws ParseException {
    reminderModule.setScheduler("8", "6:30");
    sleep(1000);
    String cron = reminderModule.getCronExpression();
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 5);
    CronExpression cronExpression = new CronExpression(cron);
    Date d1 = cronExpression.getNextValidTimeAfter(cal.getTime());
    Calendar triggerCal = Calendar.getInstance();
    triggerCal.setTime(d1);
    Assert.assertEquals(6, triggerCal.get(Calendar.HOUR_OF_DAY));
    Assert.assertEquals(30, triggerCal.get(Calendar.MINUTE));
    Date d2 = cronExpression.getNextValidTimeAfter(d1);
    triggerCal.setTime(d2);
    Assert.assertEquals(14, triggerCal.get(Calendar.HOUR_OF_DAY));
    Assert.assertEquals(30, triggerCal.get(Calendar.MINUTE));
    Date d3 = cronExpression.getNextValidTimeAfter(d2);
    triggerCal.setTime(d3);
    Assert.assertEquals(22, triggerCal.get(Calendar.HOUR_OF_DAY));
    Assert.assertEquals(30, triggerCal.get(Calendar.MINUTE));
}
Also used : Calendar(java.util.Calendar) CronExpression(org.quartz.CronExpression) Date(java.util.Date) Test(org.junit.Test)

Example 29 with CronExpression

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

the class QuartzScheduler method setTimezone.

/**
 * Update cronTrigger's timezone based on the info from caller
 *
 * @param cronTrigger the cron trigger to update
 * @param timezone the timezone to set
 */
void setTimezone(CronTrigger cronTrigger, String timezone) throws SchedulerException {
    try {
        CronExpression cronEx = new CronExpression(cronTrigger.getCronExpression());
        cronEx.setTimeZone(TimeZone.getTimeZone(timezone));
        cronTrigger.setCronExpression(cronEx);
    } catch (ParseException e) {
        throw new SchedulerException(Messages.getInstance().getString("ComplexJobTrigger.ERROR_0001_InvalidCronExpression"), // $NON-NLS-1$
        e);
    }
}
Also used : SchedulerException(org.pentaho.platform.api.scheduler2.SchedulerException) CronExpression(org.quartz.CronExpression) ParseException(java.text.ParseException)

Example 30 with CronExpression

use of org.quartz.CronExpression in project alfresco-repository by Alfresco.

the class LocalTransformServiceRegistryConfigTest method testAdditionAndRemovalOfTEngines.

@Test
public void testAdditionAndRemovalOfTEngines() throws Exception {
    CronExpression origCronExpression = registry.getCronExpression();
    CronExpression origInitialAndOnErrorCronExpression = registry.getInitialAndOnErrorCronExpression();
    String origPipelineConfigDir = registry.getPipelineConfigDir();
    try {
        readConfigCount = 0;
        // every 2 seconds rather than 10 seconds
        registry.setInitialAndOnErrorCronExpression(new CronExpression(("0/2 * * ? * * *")));
        // every 4 seconds rather than every hour
        registry.setCronExpression(new CronExpression(("0/4 * * ? * * *")));
        // Sleep until a 6 second boundary, in order to make testing clearer.
        // It avoids having to work out schedule offsets and extra quick runs that can otherwise take place.
        Thread.sleep(4000 - System.currentTimeMillis() % 4000);
        startMs = System.currentTimeMillis();
        registry.setMockSuccessReadingConfig(false);
        registry.afterPropertiesSet();
        TransformServiceRegistryImpl.Data data = registry.getData();
        // 1 seconds
        Thread.sleep(1000);
        data = registry.assertDataChanged(data, "There should have been a read after a few milliseconds that fails");
        // 3 seconds
        Thread.sleep(2000);
        data = registry.assertDataChanged(data, "There should have been a read after 2 seconds that fails");
        // 5 seconds
        Thread.sleep(2000);
        data = registry.assertDataChanged(data, "There should have been a read after 4 seconds that fails");
        // 7 seconds
        Thread.sleep(2000);
        data = registry.assertDataChanged(data, "There should have been a read after 6 seconds that fails");
        // Should switch to normal 4s schedule after the next read, so the read at 12 seconds will be on that schedule.
        // It is always possible that another quick one gets scheduled almost straight away after the next read.
        registry.setMockSuccessReadingConfig(true);
        // 9 seconds
        Thread.sleep(2000);
        data = registry.assertDataChanged(data, "There should have been a read after 8 seconds that succeeds");
        // 11 seconds
        Thread.sleep(2000);
        data = registry.assertDataUnchanged(data, "There really should not have been a read until 12 seconds");
        // 13 seconds
        Thread.sleep(2000);
        data = registry.assertDataChanged(data, "There should have been a read after 12 seconds that succeeds");
        // Should switch back to initial/error schedule after failure
        registry.setMockSuccessReadingConfig(false);
        // 17 seconds
        Thread.sleep(4000);
        data = registry.assertDataChanged(data, "There should have been a read after 16 seconds that fails");
        // 19 seconds
        Thread.sleep(2000);
        data = registry.assertDataChanged(data, "There should have been a read after 18 seconds");
    } finally {
        registry.setMockSuccessReadingConfig(true);
        registry.setCronExpression(origCronExpression);
        registry.setInitialAndOnErrorCronExpression(origInitialAndOnErrorCronExpression);
        registry.setPipelineConfigDir(origPipelineConfigDir);
    }
}
Also used : CronExpression(org.quartz.CronExpression) Test(org.junit.Test)

Aggregations

CronExpression (org.quartz.CronExpression)30 Date (java.util.Date)13 ParseException (java.text.ParseException)12 Test (org.junit.Test)10 ArrayList (java.util.ArrayList)6 Calendar (java.util.Calendar)5 DateTime (org.joda.time.DateTime)5 FeedOnTimeArrivalMetric (com.thinkbiganalytics.metadata.sla.api.core.FeedOnTimeArrivalMetric)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 RFC5545Schedule (com.hubspot.singularity.helpers.RFC5545Schedule)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Matcher (java.util.regex.Matcher)2 ProcessException (org.apache.nifi.processor.exception.ProcessException)2 InvalidRecurrenceRuleException (org.dmfs.rfc5545.recur.InvalidRecurrenceRuleException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SingularityDeployStatistics (com.hubspot.singularity.SingularityDeployStatistics)1 PendingType (com.hubspot.singularity.SingularityPendingRequest.PendingType)1 AbstractJob (com.jeesuite.scheduler.AbstractJob)1