How to combine variables from parallel streams in Activiti?

I currently have a sub-process that uses fork / join mechanism to create parallel threads. Suppose that there are two flows: A, B . Each of these streams takes a complex CONTEXT object as input variables . Also, each of these threads does some calculations and updates the CONTEXT internally. As a result, each stream returns an updated CONTEXT . The problem here is that at Join point, the last CONTEXT result overrides the previous one. Suppose that the filling of stream A is completed first with the result CONTEXT_1 , and stream B returnsCONTEXT_2 . So the end result will be CONTEXT_2 and all changes from stream A will be lost.

The question here is how to combine the results of the two streams? enter image description here

UPDATE: From my observations, the variable (CONTEXT) is passed from SuperProcess to SubProcess (CONTEXT) and after the subProcess processing is completed, the new value of the passed variable (CONTEXT) will have the original (CONTEXT).

In the example below, I mean all past variables have the same name.

Example:

  • SuperProcess P1 (variable: CONTEXT ) calls SubProcess P2 (variables are passed in copy);
  • (variable: CONTEXT ) creates two parallel threads (tasks) A , B strong> (variables are passed by copy);

  • Task (variable: CONTEXT_1 ) updates the value of the variable, exits, and returns the variable;

    3.1. CONTEXT_1 has a CONTEXT variable , so P2 can only see this new value, since the names of these variables are the same;

  • Meanwhile B Task (variable: CONTEXT_2 ) is still running and after a while updates the variable, exits and returns the variable;

    4.1. CONTEXT_2 has a CONTEXT_1 variable , so P2 can only see this new value, since the names of these variables are the same;

  • SubProcess P2 (variable: CONTEXT_2 ) exits and returns a new version of SuperProcess. Result -> CONTEXT_1 lost.

My target scenario:

  • SuperProcess P1 (variable: CONTEXT ) calls SubProcess P2 (variables are passed in copy);
  • (variable: CONTEXT ) creates two parallel threads (tasks) A , B strong> (variables are passed by copy);

  • Task (variable: CONTEXT_1 ) updates the value of the variable, exits, and returns the variable;

    3.1. CONTEXT_1 and CONTEXT are merged into CONTEXT_M1 , in other words, only new changes to CONTEXT_1 will be applied to CONTEXT .

  • Meanwhile B Task (variable: CONTEXT_2 ) is still running and after a while updates the variable, exits and returns the variable;

    4.1. CONTEXT_2 and CONTEXT_M1 are merged into CONTEXT_M2 , in other words, only new CONTEXT_2 changes will be applied to CONTEXT_M1 , so the previous update will not be lost;

  • SubProcess P2 (variable: CONTEXT_M2 ) exits and returns a new version of SuperProcess. Result -> CONTEXT_M2 . All changes have been saved.
+3


source to share


2 answers


After a few days of research, we found out that copying variables from SuperProcess to SubProcess is the default behavior ( link ):

"You can pass process variables to a subprocess and vice versa. Data is copied to a subprocess when it starts and is copied back to the main process when it ends."



As a solution, we pass the variables to SubProcess under a different name and merge with the original variable after the SubProcess completes: enter image description here

+2


source


When do you talk about merging? What exactly do you mean? What is your desired emerge behavior?

If you want to support both contexts, use a map with an execution id as the key, however I doubt this is what you want.



Greg

+1


source







All Articles