Search in sources :

Example 86 with CronTrigger

use of org.quartz.CronTrigger in project spring-boot by spring-projects.

the class QuartzEndpointDocumentationTests method quartzTriggerGroup.

@Test
void quartzTriggerGroup() throws Exception {
    CronTrigger cron = cronTrigger.getTriggerBuilder().startAt(fromUtc("2020-11-30T17:00:00Z")).endAt(fromUtc("2020-12-30T03:00:00Z")).withIdentity("3am-week", "tests").build();
    setPreviousNextFireTime(cron, "2020-12-04T03:00:00Z", "2020-12-07T03:00:00Z");
    SimpleTrigger simple = simpleTrigger.getTriggerBuilder().withIdentity("every-day", "tests").build();
    setPreviousNextFireTime(simple, null, "2020-12-04T12:00:00Z");
    CalendarIntervalTrigger calendarInterval = calendarIntervalTrigger.getTriggerBuilder().withIdentity("once-a-week", "tests").startAt(fromUtc("2019-07-10T14:00:00Z")).endAt(fromUtc("2023-01-01T12:00:00Z")).build();
    setPreviousNextFireTime(calendarInterval, "2020-12-02T14:00:00Z", "2020-12-08T14:00:00Z");
    DailyTimeIntervalTrigger tueThuTrigger = dailyTimeIntervalTrigger.getTriggerBuilder().withIdentity("tue-thu", "tests").build();
    Trigger customTrigger = mock(Trigger.class);
    given(customTrigger.getKey()).willReturn(TriggerKey.triggerKey("once-a-year-custom", "tests"));
    given(customTrigger.toString()).willReturn("com.example.CustomTrigger@fdsfsd");
    given(customTrigger.getPriority()).willReturn(10);
    given(customTrigger.getPreviousFireTime()).willReturn(fromUtc("2020-07-14T16:00:00Z"));
    given(customTrigger.getNextFireTime()).willReturn(fromUtc("2021-07-14T16:00:00Z"));
    mockTriggers(cron, simple, calendarInterval, tueThuTrigger, customTrigger);
    this.mockMvc.perform(get("/actuator/quartz/triggers/tests")).andExpect(status().isOk()).andDo(document("quartz/trigger-group", responseFields(fieldWithPath("group").description("Name of the group."), fieldWithPath("paused").description("Whether the group is paused."), fieldWithPath("triggers.cron").description("Cron triggers keyed by name, if any."), fieldWithPath("triggers.simple").description("Simple triggers keyed by name, if any."), fieldWithPath("triggers.dailyTimeInterval").description("Daily time interval triggers keyed by name, if any."), fieldWithPath("triggers.calendarInterval").description("Calendar interval triggers keyed by name, if any."), fieldWithPath("triggers.custom").description("Any other triggers keyed by name, if any.")).andWithPrefix("triggers.cron.*.", concat(triggerSummary, cronTriggerSummary)).andWithPrefix("triggers.simple.*.", concat(triggerSummary, simpleTriggerSummary)).andWithPrefix("triggers.dailyTimeInterval.*.", concat(triggerSummary, dailyTimeIntervalTriggerSummary)).andWithPrefix("triggers.calendarInterval.*.", concat(triggerSummary, calendarIntervalTriggerSummary)).andWithPrefix("triggers.custom.*.", concat(triggerSummary, customTriggerSummary))));
}
Also used : CronTrigger(org.quartz.CronTrigger) CalendarIntervalTrigger(org.quartz.CalendarIntervalTrigger) CronTrigger(org.quartz.CronTrigger) DailyTimeIntervalTrigger(org.quartz.DailyTimeIntervalTrigger) OperableTrigger(org.quartz.spi.OperableTrigger) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) SimpleTrigger(org.quartz.SimpleTrigger) DailyTimeIntervalTrigger(org.quartz.DailyTimeIntervalTrigger) CalendarIntervalTrigger(org.quartz.CalendarIntervalTrigger) Test(org.junit.jupiter.api.Test)

Example 87 with CronTrigger

use of org.quartz.CronTrigger in project CzechIdMng by bcvsolutions.

the class DefaultSchedulerManager method getTask.

/**
 * Returns task by given key
 *
 * @param jobKey
 * @return task dto
 */
@SuppressWarnings("unchecked")
private Task getTask(JobKey jobKey) {
    try {
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null) {
            // job does not exists
            return null;
        }
        Task task = new Task();
        // task setting
        task.setId(jobKey.getName());
        // app context is needed here
        SchedulableTaskExecutor<?> taskExecutor = (SchedulableTaskExecutor<?>) context.getAutowireCapableBeanFactory().createBean(jobDetail.getJobClass());
        task.setTaskType((Class<? extends SchedulableTaskExecutor<?>>) AutowireHelper.getTargetClass(taskExecutor));
        task.setDescription(jobDetail.getDescription());
        JobDataMap jobDataMap = jobDetail.getJobDataMap();
        task.setInstanceId(jobDataMap.getString(SchedulableTaskExecutor.PARAMETER_INSTANCE_ID));
        task.setModified((ZonedDateTime) jobDataMap.get(SchedulableTaskExecutor.PARAMETER_MODIFIED));
        task.setTriggers(new ArrayList<>());
        // task properties
        for (Entry<String, Object> entry : jobDataMap.entrySet()) {
            task.getParameters().put(entry.getKey(), entry.getValue() == null ? null : entry.getValue().toString());
        }
        task.setDisabled(taskExecutor.isDisabled());
        if (!task.isDisabled()) {
            task.setSupportsDryRun(taskExecutor.supportsDryRun());
            task.setFormDefinition(taskExecutor.getFormDefinition());
            task.setRecoverable(taskExecutor.isRecoverable());
        } else {
            LOG.warn("Task [{}] is disabled and cannot be executed, remove schedule for this task to hide this warning.", task.getTaskType().getSimpleName());
        }
        // scheduled triggers - native
        for (Trigger trigger : scheduler.getTriggersOfJob(jobKey)) {
            TriggerState state = scheduler.getTriggerState(trigger.getKey());
            if (trigger instanceof CronTrigger) {
                task.getTriggers().add(new CronTaskTrigger(task.getId(), (CronTrigger) trigger, TaskTriggerState.convert(state)));
            } else if (trigger instanceof SimpleTrigger) {
                task.getTriggers().add(new SimpleTaskTrigger(task.getId(), (SimpleTrigger) trigger, TaskTriggerState.convert(state)));
            } else {
                LOG.warn("Job '{}' ({}) has registered trigger of unsupported type {}", jobKey, jobDetail.getJobClass(), trigger);
            }
        }
        // dependent tasks
        dependentTaskTriggerRepository.findByDependentTaskId(jobKey.getName()).forEach(dependentTask -> {
            task.getTriggers().add(new DependentTaskTrigger(task.getId(), dependentTask.getId(), dependentTask.getInitiatorTaskId()));
        });
        return task;
    } catch (org.quartz.SchedulerException ex) {
        if (ex.getCause() instanceof ClassNotFoundException) {
            deleteTask(jobKey.getName());
            LOG.warn("Job [{}] inicialization failed, job class was removed, scheduled task is removed.", jobKey, ex);
            return null;
        }
        throw new CoreException(ex);
    } catch (BeansException | IllegalArgumentException ex) {
        deleteTask(jobKey.getName());
        LOG.warn("Job [{}] inicialization failed, scheduled task is removed", jobKey, ex);
        return null;
    }
}
Also used : Task(eu.bcvsolutions.idm.core.scheduler.api.dto.Task) JobDataMap(org.quartz.JobDataMap) CronTrigger(org.quartz.CronTrigger) TaskTriggerState(eu.bcvsolutions.idm.core.scheduler.api.dto.TaskTriggerState) TriggerState(org.quartz.Trigger.TriggerState) JobDetail(org.quartz.JobDetail) DependentTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger) CronTrigger(org.quartz.CronTrigger) AbstractTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.AbstractTaskTrigger) CronTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.CronTaskTrigger) SimpleTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.SimpleTaskTrigger) IdmDependentTaskTrigger(eu.bcvsolutions.idm.core.scheduler.entity.IdmDependentTaskTrigger) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) DependentTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger) IdmDependentTaskTrigger(eu.bcvsolutions.idm.core.scheduler.entity.IdmDependentTaskTrigger) SimpleTrigger(org.quartz.SimpleTrigger) SchedulableTaskExecutor(eu.bcvsolutions.idm.core.scheduler.api.service.SchedulableTaskExecutor) CronTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.CronTaskTrigger) SimpleTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.SimpleTaskTrigger) BeansException(org.springframework.beans.BeansException)

Example 88 with CronTrigger

use of org.quartz.CronTrigger in project engine by craftercms.

the class FolderBasedScriptJobResolverTest method testResolveJobs.

@Test
public void testResolveJobs() throws Exception {
    List<JobContext> jobContexts = resolver.resolveJobs(siteContext);
    assertNotNull(jobContexts);
    assertEquals(1, jobContexts.size());
    JobDetailImpl jobDetail = (JobDetailImpl) jobContexts.get(0).getDetail();
    CronTrigger trigger = (CronTrigger) jobContexts.get(0).getTrigger();
    assertEquals(ScriptJob.class, jobDetail.getJobClass());
    assertEquals("/scripts/jobs/testJob.groovy", jobDetail.getJobDataMap().getString(ScriptJob.SCRIPT_URL_DATA_KEY));
    assertEquals(HOURLY_CRON_EXPRESSION, trigger.getCronExpression());
}
Also used : CronTrigger(org.quartz.CronTrigger) JobDetailImpl(org.quartz.impl.JobDetailImpl) JobContext(org.craftercms.engine.util.quartz.JobContext) Test(org.junit.Test)

Example 89 with CronTrigger

use of org.quartz.CronTrigger in project dubidubi by lzzzz4.

the class Quartz method updateJobTime.

public static void updateJobTime(String jobName, String jobGroupName, String triggerName, String triggerGroupName, String cron) {
    try {
        Scheduler sched = schedulerFactory.getScheduler();
        TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
        CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey);
        if (trigger == null) {
            return;
        }
        String oldTime = trigger.getCronExpression();
        if (!oldTime.equalsIgnoreCase(cron)) {
            /**
             * 方式一 :调用 rescheduleJob 开始
             */
            // 触发器
            TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
            // 触发器名,触发器组
            triggerBuilder.withIdentity(triggerName, triggerGroupName);
            triggerBuilder.startNow();
            // 触发器时间设定
            triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
            // 创建Trigger对象
            trigger = (CronTrigger) triggerBuilder.build();
            // 方式一 :修改一个任务的触发时间
            sched.rescheduleJob(triggerKey, trigger);
        /**
         * 方式一 :调用 rescheduleJob 结束
         */
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : TriggerKey(org.quartz.TriggerKey) CronTrigger(org.quartz.CronTrigger) Trigger(org.quartz.Trigger) CronTrigger(org.quartz.CronTrigger) Scheduler(org.quartz.Scheduler)

Example 90 with CronTrigger

use of org.quartz.CronTrigger in project ballerina by ballerina-lang.

the class AppointmentManager method schedule.

void schedule(String taskId, NativeCallableUnit fn, Class<? extends Job> jobClass, Context balParentContext, FunctionRefCPEntry onTriggerFunction, FunctionRefCPEntry onErrorFunction, String cronExpression) throws SchedulerException {
    JobDataMap jobData = new JobDataMap();
    jobData.put(AppointmentConstants.BALLERINA_FUNCTION, fn);
    jobData.put(AppointmentConstants.BALLERINA_PARENT_CONTEXT, balParentContext);
    jobData.put(AppointmentConstants.BALLERINA_ON_TRIGGER_FUNCTION, onTriggerFunction);
    jobData.put(AppointmentConstants.BALLERINA_ON_ERROR_FUNCTION, onErrorFunction);
    JobDetail job = newJob(jobClass).usingJobData(jobData).withIdentity(taskId).build();
    CronTrigger trigger = newTrigger().withIdentity(taskId).withSchedule(cronSchedule(cronExpression)).build();
    scheduler.scheduleJob(job, trigger);
    quartzJobs.put(taskId, job.getKey());
}
Also used : JobDataMap(org.quartz.JobDataMap) JobDetail(org.quartz.JobDetail) CronTrigger(org.quartz.CronTrigger)

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