cancel
Showing results for 
Search instead for 
Did you mean: 

Scope on Message within Subprocess

esseti
Champ in-the-making
Champ in-the-making
Hello all.
As far as i understood setVariable() creates global variable, while seVariableLocal() creates a variable with the scope that is the partent object, so if it's within a subprocess it's not accessible from outside.

Now, the receive message task (or a catch message event) seems to use setVariable when it receives a message, am i correct?

i made this test https://www.dropbox.com/s/3kg6h2kepq8p4zh/Screenshot%202015-02-16%2011.02.54.png?dl=0
the script tasks prints the variable "mydata" (first appends "t1", second "t2", third "t3")
and i've a java class to execute the process that has this piece of code:


List<Execution> list = runtimeService.createExecutionQuery().processDefinitionId(processInstance.getProcessDefinitionId()).messageEventSubscriptionName("message").list();
   int i=0;
   for (Execution execution : list) {
      Map<String, Object> data = new HashMap<String, Object>();
      i++;
      System.out.println("sending "+i);
      data.put("mydata", "loop " +i);
      runtimeService.messageEventReceived("message",execution.getId(),data);
   }


what i have as output is:


sending 1
t1: loop 1
sending 2
t1: loop 2
t2: loop 2
t2: loop 2
t3: loop 2


as you can see the second receive in the first subprocess loop (the first t2) "prints 2", while it's the subprocess that received "loop 1". my understanding is that the variable is global, then it's overwritten by the second t1 receive.

Now: How can i set the receive message to store the variable local for the subprocess, thus the output will be smt like:


sending 1
t1: loop 1
sending 2
t1: loop 2
t2: loop 1
t2: loop 2
t3: <error or null or dunno>

6 REPLIES 6

jbarrez
Star Contributor
Star Contributor
I'm not sure where you got that messages are stored as variables?

Also, not sure what you are trying to do here. Where are you calling execution.setVariableLocal() yourself?

esseti
Champ in-the-making
Champ in-the-making
It was just my understanding.

rephrasing: what happens when the catch message event, or the receive message task, receive a message? do they persist as variable or what?

for the second part:
what i'm trying to do is to check if parallel subprocess can receive messages and do computation on the value they get without having problems of writing on the same variable.

the script task (process is shown in the png of the first post) are like this
<code>
println "t1: " + execution.getVariable("mydata")
</code>
<code>
println "t2: " + execution.getVariable("mydata")
</code>
<code>
println "t3: " + execution.getVariable("mydata")
</code>

and the execution of the process is done via the Java u see in the first post.

now:
the subprocess has 2 parallel instances, i send a message (containing a variable name "mydata") to the first loop with value "loop 1", and to the second instance with value "loop 2", the first script task prints the values correctly "t1:loop 1" for first instance and "t1:loop 2" for the second.
Till here everything is fine.

Then after 10 seconds the process calls the second script task which, to me, should print "t2:loop 1" for the first instance, since the first instance of the parallel process received "loop 1",  and "t2: loop 2" for the second instance.
Instead, it prints "t2: loop 2" for both, which seem that the catch event stored a global variable named "mydata" as global and accessible by both subprocess. in fact, the third script task prints "loop 2". instead it should have no access to that.

to sum, the ouput is like this
<code>
t1: loop 1
t1: loop 2

t2: loop 2
t2: loop 2

t3: loop 2
</code>
this is why to me seemed that the message that arrives is stored as a setVariable(). but probably i miss some part.

What i need is: how can i have model/set the process in a way that the message received by the subprocess can be accessed only by the subprocess? thus have an output like this

<code>
t1: loop 1
t1: loop 2

t2: loop 1
t2: loop 2

t3: <nothing/error>
</code>



jbarrez
Star Contributor
Star Contributor
> rephrasing: what happens when the catch message event, or the receive message task, receive a message? do they persist as variable or what?

No. They are catched on the element, and that's it. All side effects like storing something will need to be done by yourself.

For your second part: variables are stored by default globally. If you need to store data locally, you need to use the execution.setVariableLocal() methods.

esseti
Champ in-the-making
Champ in-the-making
I see.
so what's the best (easiest) way to store a variable? with a ScripTask? or what? can i create an extension of a receive task that automatically stores the variable?
Is there a tutorial/blog/guide that you can point me at, to see how things should made.

thanks

jbarrez
Star Contributor
Star Contributor
You can use a script task for that, or an execution listener. Both are covered in the user guide.

royqh1979
Champ in-the-making
Champ in-the-making
In javadoc for messageEventReceived(), it said:
<code>
Notifies the process engine that a message event with the name 'messageName' has been received and has been correlated to an execution with id 'executionId'. The waiting execution is notified synchronously.
Variables are set for the scope of the execution of the message event subscribed to the message name. For example:

The scope for an intermediate message event in the main process is that of the process instance
The scope for an intermediate message event in a subprocess is that of the subprocess
The scope for a boundary message event is that of the execution for the Activity the event is attached to
</code>
But in fact , the implementation code uses execution.setVariables() to set variables, so all are gone to the global scope.
I think it's a bug.