A Task can be initiated from code in any rule (including workflow rules) by instantiating a TaskManager and calling one of its methods for running a task. Methods to run a task include:
Examples
1. Using TaskResult runSync (String TaskDefinitionName, Map<String,Object> args)
try {
TaskManager tm = new TaskManager(context);
TaskResult result = tm.runSync("Identity Refresh", new HashMap());
} catch (Exception e) {
log.error("Exception processing request for Identity Refresh run from rule");
throw new GeneralException(e);
}
2. Using void runNow(TaskSchedule sched)
TaskDefinition task = context.getObjectByName(TaskDefinition.class,"Identity Refresh");
if (null != task) {
try {
// Task Schedule Setup
TaskSchedule taskSchedule = new TaskSchedule();
taskSchedule.setName("Schedule for" + task.getName());
taskSchedule.setDeleteOnFinish(true);
taskSchedule.setDescription("Job Scheduled from Rule");
taskSchedule.setTaskDefinition(task);
taskSchedule.setLauncher("spadmin");
Date now = new Date();
taskSchedule.setNextExecution(now);
TaskManager tm = new TaskManager(context);
tm.runNow(taskSchedule);
} catch (Exception e) {
log.error("Exception processing request for "+task.getName());
throw new GeneralException(e);
}
// Include this if you want to wait for the results from the task:
// TaskResult tr = tm.awaitTask(taskSchedule, 1800);
}
3. Using TaskSchedule run(TaskDefinition def, Map<String,Object> args)
TaskDefinition task = context.getObjectByName(TaskDefinition.class,"Identity Refresh");
if (null != task) {
try {
Date now = new Date();
TaskManager tm = new TaskManager(context);
tm.run(task,null);
} catch (Exception e) {
log.error("Exception processing request for "+task.getName());
throw new GeneralException(e);
}
}
4. Using void run(String TaskDefinitionName, Attributes <String, Object> args)
try {
TaskManager tm = new TaskManager(context);
tm.run ("Identity Refresh", new Attributes());
} catch (Exception e) {
log.error("Exception processing request for Identity Refresh run from rule");
throw new GeneralException(e);
}