Search in sources :

Example 1 with AbstractTaskTrigger

use of eu.bcvsolutions.idm.core.scheduler.api.dto.AbstractTaskTrigger 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 AbstractTaskTrigger

use of eu.bcvsolutions.idm.core.scheduler.api.dto.AbstractTaskTrigger 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 3 with AbstractTaskTrigger

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

the class AbstractScheduledTaskInitializer method createAndSaveTriggers.

/**
 * Create and save trigger from {@link IdmScheduledTaskType}.
 *
 * @param task
 * @param type
 */
private void createAndSaveTriggers(Task task, IdmScheduledTaskType type) {
    Assert.notNull(task);
    Assert.notNull(task.getId());
    // triggers
    if (type.getTriggers() != null && type.getTriggers().getTriggers() != null) {
        for (IdmScheduledTaskTriggerType trigger : type.getTriggers().getTriggers()) {
            AbstractTaskTrigger newTrigger = null;
            try {
                newTrigger = getTrigger(trigger);
            } catch (ClassNotFoundException e) {
                LOG.error("Trigger type: [{}], not exist. Skip init this trigger for task id: [{}]. Error message: [{}]", trigger.getType(), task.getId(), e.getMessage());
                continue;
            }
            if (newTrigger != null) {
                LOG.info("Create new trigger for task id: [{}]. Trigger type: [{}].", task.getId(), trigger.getType());
                schedulerService.createTrigger(task.getId(), newTrigger);
            }
        }
    }
}
Also used : AbstractTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.AbstractTaskTrigger) IdmScheduledTaskTriggerType(eu.bcvsolutions.idm.core.scheduler.jaxb.IdmScheduledTaskTriggerType)

Aggregations

AbstractTaskTrigger (eu.bcvsolutions.idm.core.scheduler.api.dto.AbstractTaskTrigger)3 CronTaskTrigger (eu.bcvsolutions.idm.core.scheduler.api.dto.CronTaskTrigger)2 SimpleTaskTrigger (eu.bcvsolutions.idm.core.scheduler.api.dto.SimpleTaskTrigger)2 Task (eu.bcvsolutions.idm.core.scheduler.api.dto.Task)1 IdmScheduledTaskTriggerType (eu.bcvsolutions.idm.core.scheduler.jaxb.IdmScheduledTaskTriggerType)1 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)1 DateTime (org.joda.time.DateTime)1 Test (org.junit.Test)1