Resolution
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:
- RunSync():Run a task synchronously, bypassing the Quartz scheduler and
returning the result to the same thread.
- TaskResult runSync (String TaskDefinitionName, Map<String,Object> args)
- TaskResult runSync(TaskDefinition def, Map<String,Object> args)
- TaskResult runSync(TaskSchedule schedule, Map<String,Object> schedArgs)
- RunNow():Force immediate execution of a previously scheduled task. The
task remains scheduled.
- Void runNow(TaskSchedule sched)
- Run():Run a task immediately (creates a TaskSchedule object that is
deleted automatically when execution completes).
- void run(String TaskDefinitionName, Attributes<String,Object> args)
- TaskSchedule run(TaskDefinition def, Map<String,Object> args)
- RunWithResult:Launch task immediately, pre-creating a TaskResult; used
if need to store the result ID somewhere (typically in a WorkItem) to facilitate monitoring of the
task execution.
- TaskResult runWithResult(TaskDefinition def, Map<String,Object> args)
Examples:
- 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); }
- 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);
- 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); } }
- 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); }