Search in sources :

Example 1 with CronTaskTrigger

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

the class AbstractScheduledTaskInitializer method getTrigger.

/**
 * Method return {@link AbstractTaskTrigger} from
 * {@link IdmScheduledTaskTriggerType}
 *
 * @param triggerType
 * @return
 * @throws ClassNotFoundException
 */
protected AbstractTaskTrigger getTrigger(IdmScheduledTaskTriggerType triggerType) throws ClassNotFoundException {
    @SuppressWarnings("unchecked") Class<AbstractTaskTrigger> triggerClass = (Class<AbstractTaskTrigger>) Class.forName(triggerType.getType());
    if (triggerClass.isAssignableFrom(SimpleTaskTrigger.class)) {
        SimpleTaskTrigger simpleTrigger = new SimpleTaskTrigger();
        simpleTrigger = (SimpleTaskTrigger) setTriggerDefaultValues(simpleTrigger, triggerType);
        simpleTrigger.setFireTime(new DateTime(triggerType.getFireTime()));
        return simpleTrigger;
    } else if (triggerClass.isAssignableFrom(CronTaskTrigger.class)) {
        CronTaskTrigger cronTrigger = new CronTaskTrigger();
        cronTrigger = (CronTaskTrigger) setTriggerDefaultValues(cronTrigger, triggerType);
        cronTrigger.setCron(triggerType.getCron());
        return cronTrigger;
    } else {
        LOG.error("Trigger type: [{}] is not implemented, please override method getTrigger from AbstractScheduledTaskInitializer. " + "This trigger will be skipped.", triggerType.getType());
        return null;
    }
}
Also used : AbstractTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.AbstractTaskTrigger) DateTime(org.joda.time.DateTime) CronTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.CronTaskTrigger) SimpleTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.SimpleTaskTrigger)

Example 2 with CronTaskTrigger

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

the class DefaultSchedulerManager method createTrigger.

@Override
public AbstractTaskTrigger createTrigger(String taskId, AbstractTaskTrigger trigger, boolean dryRun) {
    Assert.notNull(taskId);
    Assert.notNull(trigger);
    // 
    // task has to support dry run mode
    Task task = getTask(taskId);
    Assert.notNull(task);
    if (dryRun && !task.isSupportsDryRun()) {
        throw new DryRunNotSupportedException(task.getTaskType().getCanonicalName());
    }
    // 
    String triggerId = Key.createUniqueName(taskId);
    trigger.setId(triggerId);
    trigger.setTaskId(taskId);
    // TODO use of visitor pattern may be good
    if (trigger instanceof CronTaskTrigger) {
        createTriggerInternal(taskId, (CronTaskTrigger) trigger, dryRun);
    } else if (trigger instanceof SimpleTaskTrigger) {
        createTriggerInternal(taskId, (SimpleTaskTrigger) trigger, dryRun);
    } else if (trigger instanceof DependentTaskTrigger) {
        createTriggerInternal(taskId, (DependentTaskTrigger) trigger);
    } else {
        throw new SchedulerException(CoreResultCode.SCHEDULER_UNSUPPORTED_TASK_TRIGGER, ImmutableMap.of("trigger", trigger.getClass()));
    }
    return trigger;
}
Also used : Task(eu.bcvsolutions.idm.core.scheduler.api.dto.Task) SchedulerException(eu.bcvsolutions.idm.core.scheduler.exception.SchedulerException) DependentTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger) IdmDependentTaskTrigger(eu.bcvsolutions.idm.core.scheduler.entity.IdmDependentTaskTrigger) DryRunNotSupportedException(eu.bcvsolutions.idm.core.scheduler.api.exception.DryRunNotSupportedException) CronTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.CronTaskTrigger) SimpleTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.SimpleTaskTrigger)

Example 3 with CronTaskTrigger

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

the class AbstractScheduledTaskInitializerTest method testInitTask.

@Test
public void testInitTask() {
    List<Task> tasks = schedulerService.getAllTasks();
    // found test task, other will be skip
    for (Task task : tasks) {
        if (task.getTaskType().equals(TestTaskExecutor.class)) {
            assertEquals(TEST_DESCRIPTION, task.getDescription());
            assertEquals(TEST_INSTANCE, task.getInstanceId());
            // assertEquals(InitTestScheduledTask.TEST_MODULE, task.getModule()); // module not working in Task
            Map<String, String> parameters = task.getParameters();
            assertEquals(3, parameters.size());
            String value1 = parameters.get(KEY_1);
            assertEquals(VALUE_1, value1);
            String value2 = parameters.get(KEY_2);
            assertEquals(VALUE_2, value2);
            String value3 = parameters.get(KEY_3);
            assertEquals(VALUE_3, value3);
            List<AbstractTaskTrigger> triggers = task.getTriggers();
            assertEquals(2, triggers.size());
            for (AbstractTaskTrigger trigger : triggers) {
                assertEquals(TaskTriggerState.ACTIVE, trigger.getState());
                if (trigger.getClass().equals(CronTaskTrigger.class)) {
                    CronTaskTrigger cronTrigger = (CronTaskTrigger) trigger;
                    assertEquals(TEST_CRON, cronTrigger.getCron());
                } else if (trigger.getClass().equals(SimpleTaskTrigger.class)) {
                    SimpleTaskTrigger simpleTrigger = (SimpleTaskTrigger) trigger;
                    assertEquals(TEST_FIRE_TIME, simpleTrigger.getFireTime());
                } else {
                    fail();
                }
            }
            // success
            return;
        }
    }
    // test task is not initialized
    fail();
}
Also used : AbstractTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.AbstractTaskTrigger) Task(eu.bcvsolutions.idm.core.scheduler.api.dto.Task) CronTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.CronTaskTrigger) SimpleTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.SimpleTaskTrigger) Test(org.junit.Test) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)

Example 4 with CronTaskTrigger

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

the class DefaultSchedulerManagerIntegrationTest method testCreateInvalidCronTrigger.

@Test(expected = InvalidCronExpressionException.class)
public void testCreateInvalidCronTrigger() {
    Task task = createTask(null);
    // 
    CronTaskTrigger trigger = new CronTaskTrigger();
    trigger.setTaskId(task.getId());
    trigger.setCron("not-valid");
    // 
    manager.createTrigger(task.getId(), trigger);
}
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)

Example 5 with CronTaskTrigger

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

the class DefaultSchedulerManager method createTriggerInternal.

private void createTriggerInternal(String taskId, CronTaskTrigger trigger, boolean dryRun) {
    CronTaskTrigger cronTaskTrigger = (CronTaskTrigger) trigger;
    CronScheduleBuilder cronBuilder;
    try {
        cronBuilder = CronScheduleBuilder.cronSchedule(cronTaskTrigger.getCron()).withMisfireHandlingInstructionDoNothing().inTimeZone(TimeZone.getDefault());
    } catch (RuntimeException ex) {
        throw new InvalidCronExpressionException(cronTaskTrigger.getCron(), ex);
    }
    // 
    try {
        scheduler.scheduleJob(TriggerBuilder.newTrigger().withIdentity(trigger.getId(), taskId).forJob(getKey(taskId)).withDescription(cronTaskTrigger.getDescription()).withSchedule(cronBuilder).usingJobData(SchedulableTaskExecutor.PARAMETER_DRY_RUN, dryRun).startNow().build());
    } catch (org.quartz.SchedulerException ex) {
        throw new SchedulerException(CoreResultCode.SCHEDULER_CREATE_TRIGGER_FAILED, ex);
    }
}
Also used : CronScheduleBuilder(org.quartz.CronScheduleBuilder) SchedulerException(eu.bcvsolutions.idm.core.scheduler.exception.SchedulerException) InvalidCronExpressionException(eu.bcvsolutions.idm.core.scheduler.exception.InvalidCronExpressionException) CronTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.CronTaskTrigger)

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