Lookup Timer Due date for a Boundary event from Task Create Listener

Hey folks,

I have a User task with a Boundary Timer event and i am trying to use a Task Listener or Execution Listener to retrieve the due date of the timer. In my listener i am saving the taskid and duedate in my database in a non Camunda table to save having to use the task query api at a later stage. The taskid and due date are added to a large complex table which is the basis my my GUI, here each item can poentially be tied to a specific camunda user task.

diagram_1

The Timer uses a duration of Minutes like “PT10M”

Currrently i am trying to use the management service to lookup the timer but unfortunately it seems that at the point of task creation (in my listener) the boundary timer is not created and so i recieve null.

public class TaskCreator implements JavaDelegate {

    @Override
public void execute(DelegateExecution execution) throws Exception {

    Map<String, Object> vars = execution.getVariables();

    Job timerJob = execution.getProcessEngineServices().getManagementService().createJobQuery()
        .processInstanceId(execution.getProcessInstanceId()).timers().singleResult();

}

}

The query itself still needs refining but as a rudementary check i tried to retrieve all timers using a simlified query of

createJobQuery().timers().list()

Inside my listener this returns nothing but if i use this later (ie in debugger before completion of my task ) the the timer is found and thats why i know that the timer im looking for is not available at the point of task creation.

Does anybody have any ideas how i can hook into another event that will allow me to retrieve ethe timer due date before the task is assigned completed etc

Thanks

Hi @patrick.corbett,

the query can’t find the timer because it isn’t committed yet. The timer is created in the same transaction as the listener is invoked. You can use a transaction listener to resolve the issue:

public class MyTaskListener implements TaskListener
{
    public void notify(final DelegateTask delegateTask)
    {
        TransactionContext transactionContext = Context.getCommandContext().getTransactionContext();
        transactionContext.addTransactionListener(TransactionState.COMMITTED, new TransactionListener()
        {
            public void execute(CommandContext commandContext)
            {
                ManagementService managementService = delegateTask.getProcessEngineServices().getManagementService();

                List<Job> timers = managementService.createJobQuery().timers().list();

                for (Job timer : timers)
                {
                    Date duedate = timer.getDuedate();
                    // ...
                }

            }
        });
    }
}

Does this help you?

Best regards,
Philipp

1 Like

@Philipp_Ossler, Yes thanks, your solution works as expected.

I had solved the problem by parsing the ISO 8601 Duration which was available in my process variables

 // parse the timer "ISO 8601" string to retrieve the duration in seconds
    Calendar createDateCalendar = new GregorianCalendar();
    createDateCalendar.setTime(pStartDate);
    Duration d = Duration.parse(pISO8601Duration);
    Long seconds = d.getSeconds();

    // Add seconds to the creation date and retrieve due date
    createDateCalendar.add(Calendar.SECOND, seconds.intValue());
    return createDateCalendar.getTime();

Your implementation is however what i originally wanted to achieve.

Many thanks