use of eu.bcvsolutions.idm.core.scheduler.exception.SchedulerException 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.exception.SchedulerException in project CzechIdMng by bcvsolutions.
the class DefaultSchedulerManager method createTask.
@Override
public Task createTask(Task task) {
Assert.notNull(task);
Assert.notNull(task.getInstanceId());
Assert.notNull(task.getTaskType());
//
try {
// task id
String taskId = Key.createUniqueName(DEFAULT_GROUP_NAME);
// description
String description = task.getDescription();
if (StringUtils.isEmpty(description)) {
description = AutowireHelper.getBeanDescription(task.getTaskType());
}
// job properties
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put(SchedulableTaskExecutor.PARAMETER_INSTANCE_ID, task.getInstanceId());
task.getParameters().entrySet().forEach(entry -> {
jobDataMap.put(entry.getKey(), entry.getValue());
});
// validate init method
try {
LongRunningTaskExecutor<?> taskExecutor = AutowireHelper.createBean(task.getTaskType());
taskExecutor.init(jobDataMap);
} catch (ResultCodeException ex) {
throw ex;
} catch (Exception ex) {
throw new ResultCodeException(CoreResultCode.LONG_RUNNING_TASK_INIT_FAILED, ImmutableMap.of("taskId", taskId, "taskType", task.getTaskType().getSimpleName(), "instanceId", task.getInstanceId()), ex);
}
// create job detail - job definition
JobDetail jobDetail = JobBuilder.newJob().withIdentity(taskId, DEFAULT_GROUP_NAME).withDescription(description).ofType(task.getTaskType()).usingJobData(jobDataMap).storeDurably().build();
// add job
scheduler.addJob(jobDetail, false);
//
LOG.debug("Job '{}' ({}) was created and registered", taskId, task.getTaskType());
//
return getTask(taskId);
} catch (org.quartz.SchedulerException ex) {
throw new SchedulerException(CoreResultCode.SCHEDULER_CREATE_TASK_FAILED, ex);
}
}
use of eu.bcvsolutions.idm.core.scheduler.exception.SchedulerException in project CzechIdMng by bcvsolutions.
the class DefaultIdmScheduledTaskService method findByQuartzTaskName.
@Transactional(readOnly = true)
@Override
public IdmScheduledTaskDto findByQuartzTaskName(String taskName) {
IdmScheduledTaskFilter filter = new IdmScheduledTaskFilter();
filter.setQuartzTaskName(taskName);
Page<IdmScheduledTaskDto> results = find(filter, new PageRequest(0, 1));
if (results.getTotalElements() == 0) {
return null;
} else if (results.getTotalElements() == 1) {
return results.iterator().next();
}
throw new SchedulerException(CoreResultCode.SEARCH_ERROR, ImmutableMap.of("reason", "Multiple tasks found for " + getClass().getSimpleName() + " task."));
}
use of eu.bcvsolutions.idm.core.scheduler.exception.SchedulerException 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);
}
}
use of eu.bcvsolutions.idm.core.scheduler.exception.SchedulerException in project CzechIdMng by bcvsolutions.
the class DefaultSchedulerManager method updateTask.
@Override
public Task updateTask(String taskId, Task newTask) {
Assert.notNull(taskId);
Task task = getTask(taskId);
String description = newTask.getDescription();
if (StringUtils.isEmpty(description)) {
description = AutowireHelper.getBeanDescription(task.getTaskType());
}
try {
// job properties
JobDataMap jobDataMap = new JobDataMap();
newTask.getParameters().entrySet().forEach(entry -> {
jobDataMap.put(entry.getKey(), entry.getValue());
});
jobDataMap.put(SchedulableTaskExecutor.PARAMETER_INSTANCE_ID, newTask.getInstanceId());
// validate init method
try {
LongRunningTaskExecutor<?> taskExecutor = AutowireHelper.createBean(task.getTaskType());
taskExecutor.init(jobDataMap);
} catch (ResultCodeException ex) {
throw ex;
} catch (Exception ex) {
throw new ResultCodeException(CoreResultCode.LONG_RUNNING_TASK_INIT_FAILED, ImmutableMap.of("taskId", taskId, "taskType", task.getTaskType().getSimpleName(), "instanceId", task.getInstanceId()), ex);
}
// create job detail - job definition
JobDetail jobDetail = JobBuilder.newJob().withIdentity(task.getId(), DEFAULT_GROUP_NAME).withDescription(description).ofType(task.getTaskType()).usingJobData(jobDataMap).storeDurably().build();
// add job
scheduler.addJob(jobDetail, true);
//
LOG.debug("Job '{}' ({}) was updated and registered", task.getId(), task.getTaskType());
//
} catch (org.quartz.SchedulerException ex) {
throw new SchedulerException(CoreResultCode.SCHEDULER_CREATE_TASK_FAILED, ex);
}
return getTask(task.getId());
}
Aggregations