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