Search in sources :

Example 71 with CronTrigger

use of org.quartz.CronTrigger in project PublicCMS-preview by sanluan.

the class ScheduledTask method create.

/**
 * 创建任务计划
 *
 * @param site
 * @param id
 * @param cronExpression
 */
public void create(SysSite site, Integer id, String cronExpression) {
    if (CommonUtils.notEmpty(id) && CommonUtils.notEmpty(cronExpression)) {
        Date startTime = CommonUtils.getDate();
        String taskName = getTaskName(id);
        TriggerKey triggerKey = TriggerKey.triggerKey(taskName);
        try {
            CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(site.getId() % 60 + BLANK_SPACE + cronExpression);
            if (null == trigger) {
                JobDetail jobDetail = JobBuilder.newJob(ScheduledJob.class).withIdentity(taskName).build();
                jobDetail.getJobDataMap().put(ID, id);
                trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).withSchedule(scheduleBuilder).startNow().build();
                scheduler.scheduleJob(jobDetail, trigger);
            } else {
                trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).startNow().build();
                scheduler.rescheduleJob(triggerKey, trigger);
            }
        } catch (SchedulerException e) {
            sysTaskService.updateStatus(id, TASK_STATUS_ERROR);
            logTaskService.save(new LogTask(site.getId(), id, startTime, CommonUtils.getDate(), false, e.getMessage()));
        }
    }
}
Also used : TriggerKey(org.quartz.TriggerKey) CronTrigger(org.quartz.CronTrigger) CronScheduleBuilder(org.quartz.CronScheduleBuilder) JobDetail(org.quartz.JobDetail) SchedulerException(org.quartz.SchedulerException) Date(java.util.Date) LogTask(com.publiccms.entities.log.LogTask)

Example 72 with CronTrigger

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

the class StatisticsAdminController method refreshUIState.

private void refreshUIState() {
    log_.info("refreshUIState: schedulerFactoryBean found");
    boolean enabled = false;
    String cronExpression = "";
    Trigger.TriggerState triggerState;
    try {
        TriggerKey triggerKey = new TriggerKey("updateStatisticsTrigger", null);
        triggerState = scheduler.getTriggerState(triggerKey);
        enabled = triggerState != Trigger.TriggerState.NONE && triggerState != Trigger.TriggerState.ERROR;
        Trigger trigger = scheduler.getTrigger(triggerKey);
        if (trigger == null) {
            enabled &= false;
        } else {
            enabled &= trigger.getJobKey().getName().equals("org.olat.statistics.job.enabled");
            if (trigger instanceof CronTrigger) {
                log_.info("refreshUIState: org.olat.statistics.job.enabled check, enabled now: " + enabled);
                cronExpression = ((CronTrigger) trigger).getCronExpression();
            }
        }
        log_.info("refreshUIState: updateStatisticsTrigger state was " + triggerState + ", enabled now: " + enabled);
    } catch (SchedulerException e) {
        log_.warn("refreshUIState: Got a SchedulerException while asking for the updateStatisticsTrigger's state", e);
    }
    StatisticUpdateManager statisticUpdateManager = getStatisticUpdateManager();
    if (statisticUpdateManager == null) {
        log_.info("refreshUIState: statisticUpdateManager not configured");
        enabled = false;
    } else {
        enabled &= statisticUpdateManager.isEnabled();
        log_.info("refreshUIState: statisticUpdateManager configured, enabled now: " + enabled);
    }
    if (enabled) {
        content.contextPut("status", getTranslator().translate("statistics.status.enabled", new String[] { cronExpression }));
    } else {
        content.contextPut("status", getTranslator().translate("statistics.status.disabled"));
    }
    content.contextPut("statisticEnabled", enabled);
    recalcLastUpdated();
    updateStatisticUpdateOngoingFlag();
}
Also used : TriggerKey(org.quartz.TriggerKey) CronTrigger(org.quartz.CronTrigger) Trigger(org.quartz.Trigger) CronTrigger(org.quartz.CronTrigger) SchedulerException(org.quartz.SchedulerException) StatisticUpdateManager(org.olat.course.statistic.StatisticUpdateManager)

Example 73 with CronTrigger

use of org.quartz.CronTrigger in project archiva by apache.

the class DefaultRepositoryArchivaTaskScheduler method scheduleRepositoryJobs.

private synchronized void scheduleRepositoryJobs(ManagedRepositoryConfiguration repoConfig) throws SchedulerException {
    if (repoConfig.getRefreshCronExpression() == null) {
        log.warn("Skipping job, no cron expression for {}", repoConfig.getId());
        return;
    }
    if (!repoConfig.isScanned()) {
        log.warn("Skipping job, repository scannable has been disabled for {}", repoConfig.getId());
        return;
    }
    // get the cron string for these database scanning jobs
    String cronString = repoConfig.getRefreshCronExpression();
    if (!cronValidator.validate(cronString)) {
        log.warn("Cron expression [{}] for repository [{}] is invalid.  Defaulting to hourly.", cronString, repoConfig.getId());
        cronString = CRON_HOURLY;
    }
    JobDataMap jobDataMap = new JobDataMap();
    jobDataMap.put(TASK_QUEUE, repositoryScanningQueue);
    jobDataMap.put(TASK_REPOSITORY, repoConfig.getId());
    // setup the unprocessed artifact job
    JobDetail repositoryJob = JobBuilder.newJob(RepositoryTaskJob.class).withIdentity(REPOSITORY_JOB + ":" + repoConfig.getId(), REPOSITORY_SCAN_GROUP).setJobData(jobDataMap).build();
    try {
        CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(REPOSITORY_JOB_TRIGGER + ":" + repoConfig.getId(), REPOSITORY_SCAN_GROUP).withSchedule(CronScheduleBuilder.cronSchedule(cronString)).build();
        jobs.add(REPOSITORY_JOB + ":" + repoConfig.getId());
        scheduler.scheduleJob(repositoryJob, trigger);
    } catch (RuntimeException e) {
        log.error("ParseException in repository scanning cron expression, disabling repository scanning for '{}': {}", repoConfig.getId(), e.getMessage());
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) JobDetail(org.quartz.JobDetail) CronTrigger(org.quartz.CronTrigger)

Example 74 with CronTrigger

use of org.quartz.CronTrigger in project jforum2 by rafaelsteil.

the class SummaryScheduler method startJob.

/**
 * Starts the summary Job. Conditions to start: Is not started yet and is enabled on the file
 * SystemGlobasl.properties. The to enable it is "summary.enabled"
 * (ConfigKeys.SUMMARY_IS_ENABLED).
 *
 * @throws SchedulerException
 * @throws IOException
 */
public static void startJob() throws SchedulerException {
    boolean isEnabled = SystemGlobals.getBoolValue(ConfigKeys.SUMMARY_IS_ENABLED);
    if (!isStarted && isEnabled) {
        String filename = SystemGlobals.getValue(ConfigKeys.QUARTZ_CONFIG);
        String cronExpression = SystemGlobals.getValue("org.quartz.context.summary.cron.expression");
        scheduler = new StdSchedulerFactory(filename).getScheduler();
        Trigger trigger = null;
        try {
            trigger = new CronTrigger(SummaryJob.class.getName(), "summaryJob", cronExpression);
            logger.info("Starting quartz summary expression " + cronExpression);
            scheduler.scheduleJob(new JobDetail(SummaryJob.class.getName(), "summaryJob", SummaryJob.class), trigger);
            scheduler.start();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    isStarted = true;
}
Also used : StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) CronTrigger(org.quartz.CronTrigger) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) CronTrigger(org.quartz.CronTrigger) ParseException(java.text.ParseException)

Example 75 with CronTrigger

use of org.quartz.CronTrigger in project opencast by opencast.

the class AbstractScanner method schedule.

/**
 * Schedule the scanning job to execute according to the cron schedule.
 */
public void schedule() {
    if (!isEnabled()) {
        logger.info(getScannerName() + " is disabled");
        return;
    }
    if (quartz == null) {
        logger.warn("No quartz scheduler available to schedule scanner.");
        return;
    }
    logger.info("Schedule " + getScannerName() + " as a cron job ({})", getCronExpression());
    try {
        final CronTrigger trigger = new CronTrigger();
        trigger.setCronExpression(getCronExpression());
        trigger.setName(getTriggerName());
        trigger.setGroup(getTriggerGroupName());
        trigger.setJobName(getJobName());
        trigger.setJobGroup(getJobGroup());
        if (getQuartz().getTriggersOfJob(getJobName(), getJobGroup()).length == 0) {
            getQuartz().scheduleJob(trigger);
        } else {
            getQuartz().rescheduleJob(getTriggerName(), getTriggerGroupName(), trigger);
        }
    } catch (ParseException e) {
        logger.error("Error scheduling " + getScannerName() + ", the cron expression '{}' could not be parsed: {}", getCronExpression(), e.getMessage());
    } catch (Exception e) {
        logger.error("Error scheduling " + getScannerName(), e);
    }
}
Also used : CronTrigger(org.quartz.CronTrigger) ParseException(java.text.ParseException) JobExecutionException(org.quartz.JobExecutionException) SchedulerException(org.quartz.SchedulerException) ParseException(java.text.ParseException)

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