AnsweredAssumed Answered

Competing for tasks

Question asked by ritchieannand on Oct 22, 2015
Latest reply on Nov 3, 2015 by jbarrez
If you're writing something that uses the Activiti API to automatically handle something, but there could be multiple copies of it, is there a good way to have it work with the workflow and handle the inevitable collisions?

I was thinking about having the workflow split into a "queued" user/manual task and a "processing" user/manual task:

[ Queued ] —> [ Processing ] —>

And the automated process would do something like this:

boolean success = false;
while (!success)
    List<Task> queuedTasks = getProcessEngine().getTaskService().createTaskQuery().orderByDueDateNullsFirst().asc().taskId("queued").list();
    if (queuedTasks.size() > 0)
        Task claimableTask = queuedTasks.get(0);
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("claimed", myProcessId); // There would be no other way to tell which task we claimed, would there?
            getProcessEngine().getTaskService().complete(claimableTask.getId(), variables);
            success = true;
        catch (ActivitiObjectNotFoundException e)
            // Something else claimed it? We'll try again.

I prefer a "pull" model like this (hence the UserTask instead of a ServiceTask) in case it needs to get spun up on multiple VMs.

Will this cause problems? Are there better ways of accomplishing this?


– Ritchie Annand