Search in sources :

Example 6 with CronTaskTrigger

use of eu.bcvsolutions.idm.core.scheduler.api.dto.CronTaskTrigger in project CzechIdMng by bcvsolutions.

the class DefaultSchedulerManager method getTask.

/**
 * Returns task by given key
 *
 * @param jobKey
 * @return
 */
@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());
        // AutowireHelper is not needed here
        SchedulableTaskExecutor<?> taskExecutor = (SchedulableTaskExecutor<?>) jobDetail.getJobClass().newInstance();
        task.setTaskType((Class<? extends SchedulableTaskExecutor<?>>) taskExecutor.getClass());
        task.setSupportsDryRun(taskExecutor.supportsDryRun());
        task.setDescription(jobDetail.getDescription());
        task.setInstanceId(jobDetail.getJobDataMap().getString(SchedulableTaskExecutor.PARAMETER_INSTANCE_ID));
        task.setTriggers(new ArrayList<>());
        // task properties
        for (Entry<String, Object> entry : jobDetail.getJobDataMap().entrySet()) {
            task.getParameters().put(entry.getKey(), entry.getValue() == null ? null : entry.getValue().toString());
        }
        // 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 (InstantiationException | IllegalAccessException | 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) CronTrigger(org.quartz.CronTrigger) TriggerState(org.quartz.Trigger.TriggerState) TaskTriggerState(eu.bcvsolutions.idm.core.scheduler.api.dto.TaskTriggerState) JobDetail(org.quartz.JobDetail) DependentTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger) 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) CronTrigger(org.quartz.CronTrigger) AbstractTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.AbstractTaskTrigger) CronTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.CronTaskTrigger) 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)

Example 7 with CronTaskTrigger

use of eu.bcvsolutions.idm.core.scheduler.api.dto.CronTaskTrigger in project CzechIdMng by bcvsolutions.

the class DefaultSchedulerManagerIntegrationTest method testCreateAndDeleteCronTrigger.

@Test
public void testCreateAndDeleteCronTrigger() {
    Task task = createTask(null);
    // 
    CronTaskTrigger trigger = new CronTaskTrigger();
    trigger.setTaskId(task.getId());
    trigger.setCron("5 * * * * ?");
    // 
    manager.createTrigger(task.getId(), trigger);
    // 
    task = manager.getTask(task.getId());
    // 
    assertEquals(1, task.getTriggers().size());
    assertEquals(CronTaskTrigger.class, task.getTriggers().get(0).getClass());
    assertEquals(task.getId(), task.getTriggers().get(0).getTaskId());
    // 
    manager.deleteTrigger(task.getId(), task.getTriggers().get(0).getId());
    // 
    task = manager.getTask(task.getId());
    assertEquals(0, task.getTriggers().size());
}
Also used : FutureTask(java.util.concurrent.FutureTask) Task(eu.bcvsolutions.idm.core.scheduler.api.dto.Task) LongRunningFutureTask(eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask) CronTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.CronTaskTrigger) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Aggregations

CronTaskTrigger (eu.bcvsolutions.idm.core.scheduler.api.dto.CronTaskTrigger)7 Task (eu.bcvsolutions.idm.core.scheduler.api.dto.Task)5 SimpleTaskTrigger (eu.bcvsolutions.idm.core.scheduler.api.dto.SimpleTaskTrigger)4 AbstractTaskTrigger (eu.bcvsolutions.idm.core.scheduler.api.dto.AbstractTaskTrigger)3 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)3 Test (org.junit.Test)3 DependentTaskTrigger (eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger)2 LongRunningFutureTask (eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask)2 IdmDependentTaskTrigger (eu.bcvsolutions.idm.core.scheduler.entity.IdmDependentTaskTrigger)2 SchedulerException (eu.bcvsolutions.idm.core.scheduler.exception.SchedulerException)2 FutureTask (java.util.concurrent.FutureTask)2 CoreException (eu.bcvsolutions.idm.core.api.exception.CoreException)1 TaskTriggerState (eu.bcvsolutions.idm.core.scheduler.api.dto.TaskTriggerState)1 DryRunNotSupportedException (eu.bcvsolutions.idm.core.scheduler.api.exception.DryRunNotSupportedException)1 SchedulableTaskExecutor (eu.bcvsolutions.idm.core.scheduler.api.service.SchedulableTaskExecutor)1 InvalidCronExpressionException (eu.bcvsolutions.idm.core.scheduler.exception.InvalidCronExpressionException)1 DateTime (org.joda.time.DateTime)1 CronScheduleBuilder (org.quartz.CronScheduleBuilder)1 CronTrigger (org.quartz.CronTrigger)1 JobDetail (org.quartz.JobDetail)1