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;
}
}
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;
}
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();
}
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);
}
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);
}
}
Aggregations