cancel
Showing results for 
Search instead for 
Did you mean: 

Activiti event for process instance creation?

cdeneux
Champ in-the-making
Champ in-the-making
Hi all,

I try to inject dedicated processing on a process instance creation, completion and deletion (for example adding a log trace), with the following constraints:
  • The processing must be applied on all my process definitions deployed
  • The processing must not impact the process definition because it's a technical processing
I found a solution for process instance completion and deletion: adding event listeners on the events PROCESS_COMPLETED and PROCESS_CANCELLED.

But it seems to me that no event PROCESS_CREATED or PROCESS_INSTANCE_CREATED exists ? Can I use event ENTITY_CREATED or ACTIVITY_STARTED ? Or should I investigate another solution ? Or can I add a support of such an event ?

Note: the solution mentioned in [1] can not be applied in my use case because process definitions are impacted.

Thanks for your recommendations,

Regards,
Christophe DENEUX

[1] http://stackoverflow.com/questions/25428147/activiti-event-for-process-instance-creation
10 REPLIES 10

trademak
Star Contributor
Star Contributor
Hi,

You can listen to ENTITY_CREATED. That event is fired when a process instance is started.

Best regards,

cdeneux
Champ in-the-making
Champ in-the-making
Thanks Tijs,

Is event ENTITY CREATED fired only when a process instance is started ?

In the associated event listener, I try to get variables passed to [font=Courier]RuntimeService.startProcessInstanceById()[/font] used to create the process instance. But I'm not able to retrieve the process instance through a [font=Courier]ProcessInstanceQuery[/font], my query result [font=Courier]processResult[/font] is [font=Courier]null[/font]:
<code>
final ProcessInstanceQuery processQuery = event.getEngineServices().getRuntimeService()
        .createProcessInstanceQuery()
                .processInstanceId(processInstanceId)
                .includeProcessVariables();
final ProcessInstance processResult = processQuery.singleResult();
</code>

How can I retrieve the process instance created when receiving the event ENTITY_CREATED ?

Thanks for your help

Regards,
Christophe DENEUX

cdeneux
Champ in-the-making
Champ in-the-making
Hi all,

According to the current source code, it seems to me that a process instance is created through the method [font=Courier]org.activiti.engine.impl.cmd.StartProcessInstanceCmd.execute()[/font]:
<code>
   …
1 // Start the process instance
2 ExecutionEntity processInstance = processDefinition.createProcessInstance(businessKey);
3
4 // now set the variables passed into the start command
5 if (variables != null) {
6   processInstance.setVariables(variables);
7 }
8  
9 // now set processInstance name
10 if (processInstanceName != null) {
11   processInstance.setName(processInstanceName);
12 }
13
14 processInstance.start();
15
16 return processInstance;
</code>

The event ENTITY_CREATED is fired at line 1 by [font=Courier]processDefinition.createProcessInstance(businessKey)[/font], but variables are set into the process instance after the process instance creation.

After other investigations in source, the start of the process instance at line 14 performs the operation [font=Courier]org.activiti.engine.impl.pvm.runtime.AtomicOperation.PROCESS_START[/font] that is implemented by [font=Courier]org.activiti.engine.impl.pvm.runtime.AtomicOperationProcessStart[/font]. This implementation fires the event ENTITY_INITIALIZED.

Moreover, the events ENTITY_CREATED and ENTITY_INITIALIZED are not fired only for on a process instance creation. I receive them on an identify link add.
So, I must filter events in the event listener as following:
<code>
final ActivitiEntityWithVariablesEvent eventImpl = (ActivitiEntityWithVariablesEvent)event;
if (eventImpl.getEntity() instanceof ExecutionEntity) {
    …           
}
</code>

Is it correct ?

For performance reason and to avoid filtering, it seems to me that an event PROCESS_STARTED should be introduced and fired in the same location than the event ENTITY_INITIALIZED in [font=Courier]AtomicOperationProcessStart[/font]. What do you think ? If you are ok, I can contribute it.

Thanks for your help,

Regards,
Christophe DENEUX

jbarrez
Star Contributor
Star Contributor
I agree that a process start event would make a lot of sense. A contribution would certainly be appreciated!

cdeneux
Champ in-the-making
Champ in-the-making
Hi,

I've started a contribution [1] waiting to create a pull request. And I've just created the JIRA issue [2].

What do you think about a call activity with the new event ? I think that the event PROCESS_STARTED should be fired also when starting the new process instance created through the call activity.

Regards,
Christophe DENEUX

[1]: https://github.com/cdeneux/Activiti/tree/event_PROCESS_INSTANCE_STARTED
[2]: https://activiti.atlassian.net/browse/ACT-3994

jbarrez
Star Contributor
Star Contributor
> I think that the event PROCESS_STARTED should be fired also when starting the new process instance created through the call activity.

I agree. But if possible, would be good if that information could be contained in the event too.

cdeneux
Champ in-the-making
Champ in-the-making
I have added to the event the following information to track the process in which a call activity starts another process:
  • process instance id of the nested process,
  • process definition if of the nested process.
These attributes are set to null for a main process (not invoked through a call activity)

If you think that is right, I could create a pull request.

Regards,
Christophe DENEUX

jbarrez
Star Contributor
Star Contributor
Wouldn't it make more sense to have the parent information in there (instead of the nested)?

Feel free to push a Pull Request, we can surely discuss further there too, with some real code 🙂

cdeneux
Champ in-the-making
Champ in-the-making
Joram,

I've just pushed the pull request: https://github.com/Activiti/Activiti/pull/570.

You're right, the two attributes of the event mentioned below should be renamed (from 'nestedProcess…Id' to 'parentProcess…Id') . They hold the parent process instance identifiers for a nested process, or null for a main process.


Regards,
Christophe
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.