cancel
Showing results for 
Search instead for 
Did you mean: 

No process variables if exception is thrown?

clstokes
Champ in-the-making
Champ in-the-making
If an exception is thrown from a service (or script) task, then none of the process variables added during that process are available after it completes. Is this expected?


<?xml version="1.0" encoding="UTF-8" ?>
<definitions id="definitions"
             targetNamespace="http://activiti.org/bpmn20"
             xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:activiti="http://activiti.org/bpmn">

  <process id="ResultAndExceptionTest">

    <startEvent id="start"/>

    <sequenceFlow id="_1" sourceRef="start" targetRef="putValue"/>
    <sequenceFlow id="_2" sourceRef="putValue" targetRef="printValue"/>
    <sequenceFlow id="_end" sourceRef="printValue" targetRef="end"/>

    <scriptTask id="putValue"
                scriptFormat="groovy"
                activiti:async="true">
      <script>
        def map = execution.getVariable( 'references' )
        map.put( 'myValue1', 'asdf' )
        execution.setVariable( 'references', map )
      </script>
    </scriptTask>

    <scriptTask id="printValue"
                scriptFormat="groovy">
      <script>
        def map = execution.getVariable( 'references' )
        map.put( 'myValue2', 'asdf' )
        execution.setVariable( 'references', map )

        throw new IllegalArgumentException("whoops")
      </script>
    </scriptTask>

    <endEvent id="end"/>

  </process>
</definitions>


In this example, neither myValue1 or myValue2 are available via a HistoricProcessInstanceQuery call.

I'm trying to get the process variables with this code below which works fine for a successful completion.

List<HistoricProcessInstance> results = historyService.createHistoricProcessInstanceQuery()
      .includeProcessVariables()
      .processInstanceBusinessKey( context.jobId )
      .list()


Also, what's the best way via the HistoryService (or another service) to determine if a process has failed? endTime is always null for a failed process so I can't tell if it's failed or still going without doing JobQuery to check for an exception message.

Thanks for the help.
5 REPLIES 5

martin_grofcik
Confirmed Champ
Confirmed Champ
I would expect that 2 script tasks are executed in one transaction. I do not know what you want to achieve. But you can run tasks with async='true'.

http://www.activiti.org/userguide/#bpmnConcurrencyAndTransactions

clstokes
Champ in-the-making
Champ in-the-making
I want to be able to retrieve process variables from a process if it failed. Do I need to be doing something specifically with transactions?

What's the best way via the HistoryService (or another service) to determine if a process has failed?

martin_grofcik
Confirmed Champ
Confirmed Champ
What's the best way via the HistoryService (or another service) to determine if a process has failed?

I would say to use error boundary event. Error boundary event allows you to specify what to do after the error. And you can search easily for processes which has failed.
http://www.activiti.org/userguide/#bpmnBoundaryErrorEvent

I want to be able to retrieve process variables from a process if it failed.
HistoryService, RuntimeService?

clstokes
Champ in-the-making
Champ in-the-making
Is there a particular field or query I can do for failed processes? Or do I need to set my own process variable and query for that?

trademak
Star Contributor
Star Contributor
When you have a default service and script task and one of these fail, the transaction is rolled back and the process instance isn't even created and the startProcessInstance call gets back an exception. If you don't want this behavior then you can make a task async and then the transaction is only rolled back to the previous async task. Because then the job mechanism is used, this will be retried 3 times by default and then the job will be in a failed state. Now you can query on these jobs using the JobQuery with the method withException. This will give back all jobs with an exception.

Best regards,