Search in sources :

Example 6 with CronExpression

use of org.quartz.CronExpression in project javautils by jiadongpo.

the class Utils method parseCronExpression.

/**
 * @param cronExpression: A cron expression is a string separated by white space, to provide a
 * parser and evaluator for Quartz cron expressions.
 * @return : org.quartz.CronExpression object.
 *
 * TODO: Currently, we have to transform Joda Timezone to Java Timezone due to CronExpression.
 * Since Java8 enhanced Time functionalities, We consider transform all Jodatime to Java Time in
 * future.
 */
public static CronExpression parseCronExpression(final String cronExpression, final DateTimeZone timezone) {
    if (cronExpression != null) {
        try {
            final CronExpression ce = new CronExpression(cronExpression);
            ce.setTimeZone(TimeZone.getTimeZone(timezone.getID()));
            return ce;
        } catch (final ParseException pe) {
            logger.error("this cron expression {" + cronExpression + "} can not be parsed. " + "Please Check Quartz Cron Syntax.");
        }
        return null;
    } else {
        return null;
    }
}
Also used : CronExpression(org.quartz.CronExpression) ParseException(java.text.ParseException)

Example 7 with CronExpression

use of org.quartz.CronExpression in project OpenOLAT by OpenOLAT.

the class ReminderModuleTest method testCronJob_everyTwoHours.

@Test
public void testCronJob_everyTwoHours() throws ParseException {
    reminderModule.setScheduler("2", "9:30");
    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 cal1 = Calendar.getInstance();
    cal1.setTime(d1);
    Assert.assertEquals(1, cal1.get(Calendar.HOUR_OF_DAY));
    Assert.assertEquals(30, cal1.get(Calendar.MINUTE));
}
Also used : Calendar(java.util.Calendar) CronExpression(org.quartz.CronExpression) Date(java.util.Date) Test(org.junit.Test)

Example 8 with CronExpression

use of org.quartz.CronExpression in project yyl_example by Relucent.

the class CronExpressionExample method main.

public static void main(String[] args) throws Throwable {
    DateFormat format = new SimpleDateFormat("HH:mm:ss");
    CronExpression expression = new CronExpression("0 */1 * * * ?");
    // 任意一个时间
    Date from = new Date();
    // 获得下一次满足条件的时间
    Date after = expression.getNextValidTimeAfter(from);
    System.out.println(format.format(from));
    System.out.println(format.format(after));
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) CronExpression(org.quartz.CronExpression) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 9 with CronExpression

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

the class ReminderModuleTest method testCronJob_everyTwoHours.

@Test
public void testCronJob_everyTwoHours() throws ParseException {
    reminderModule.setScheduler("2", "9:30");
    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 cal1 = Calendar.getInstance();
    cal1.setTime(d1);
    Assert.assertEquals(1, cal1.get(Calendar.HOUR_OF_DAY));
    Assert.assertEquals(30, cal1.get(Calendar.MINUTE));
}
Also used : Calendar(java.util.Calendar) CronExpression(org.quartz.CronExpression) Date(java.util.Date) Test(org.junit.Test)

Example 10 with CronExpression

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

the class VmScheduleResource method findAll.

/**
 * Return all schedules related to given subscription.
 *
 * @param subscription
 *            The subscription identifier.
 * @return All schedules related to given subscription.
 * @throws ParseException
 *             When CRON cannot be parsed.
 */
@org.springframework.transaction.annotation.Transactional(readOnly = true)
public List<VmScheduleVo> findAll(final int subscription) throws ParseException {
    final List<VmScheduleVo> schedules = new ArrayList<>();
    final Date now = DateUtils.newCalendar().getTime();
    for (final VmSchedule schedule : repository.findBySubscription(subscription)) {
        // Copy basic attributes
        final VmScheduleVo vo = new VmScheduleVo();
        vo.setCron(schedule.getCron());
        vo.setOperation(schedule.getOperation());
        vo.setId(schedule.getId());
        vo.setNext(new CronExpression(schedule.getCron()).getNextValidTimeAfter(now));
        schedules.add(vo);
    }
    return schedules;
}
Also used : ArrayList(java.util.ArrayList) CronExpression(org.quartz.CronExpression) Date(java.util.Date) VmSchedule(org.ligoj.app.plugin.vm.model.VmSchedule)

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