cancel
Showing results for 
Search instead for 
Did you mean: 

Service task with properties

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

I am an Activiti novice. My company gave me a task of converting their current process engine over to Activiti. Current process engine is executing services (java API calls) in defined order, everything is specified using XML, similar to Activiti.  So I am using service tasks and each service task  contains parameters like service  type, name, various parameter, etc. What a service task is doing is starting services, a receive task is then used to wait for the service to send a response.
This is how it may look like in Activiti:

<serviceTask activiti:delegateExpression="${publisherBean}" activiti:exclusive="false" id="Torpedo_S1" name="Torpedo_S">
<extensionElements>
<activiti:field name="typeOfSystem">
<activiti:string>System_X</activiti:string>
</activiti:field>
<activiti:field name="name">
<activiti:string>Torpedo_S</activiti:string>
</activiti:field>
<activiti:field name="param1">
<activiti:string>execNow</activiti:string>
</activiti:field>
</extensionElements>
</serviceTask>
<receiveTask id="w_Torpedo_S1" name="W4Torpedo_S"/>


We are using Spring today and still want to use it for the new solution. Even before I started converting to Activiti I read about using Spring beans (singelton) and field parameters, and how this may is not be thread safe. Alternative 1 is to make prototype beans or alternative 2; rewrite the Java delegate to use an expression and passing the needed data to the delegate via a method arguments.

Question:
* Are we completly sure no race condition when using alternative 1?
* Is it possible to still have reference to DelegateExecution when using alternative 2?

Kind regards
A.H.
5 REPLIES 5

vasile_dirla
Star Contributor
Star Contributor
case 1: "Scope prototype means that every time you ask spring (getBean or dependency injection) for an instance it will create a new instance and give a reference to that." so I think it's safe.
case 2: I didn't understand what exactly you want to do by "rrewrite the Java delegate to use an expression" can you give an example (some code).

Probably you already know but here you can find some more details about service tasks:
http://www.activiti.org/userguide/#bpmnJavaServiceTask

natedawg
Champ in-the-making
Champ in-the-making
Hi Vasile,

Thanks for a quick reply and sorry for a late answer, I was away from work.

Regarding case 1, I am aware of how prototype works but I see you also stated that you "think it's safe". Does it means there are some level of uncertainty?
Regarding case 2: "rewrite the Java delegate to use an expression and passing the needed data to the delegate via a method arguments" is a sentence literally taken from Activity documentation . I am wondering if my Spring bean can implement JavaDelegate so I can write a method that allows me to specify all needed parameters, ex. startApp(String appType, String appName, String appParam), while still have access to DelegateExecution-object (I need to store processInstanceId temporarly so I can reuse it later to correlate a response-message in the process instance). If this is doable, case 2 sounds lot more like a bulletproof solution than case 1.

As it is today, there are times where up to hundred of processes can be started simultaneously so in my case thread-safety is critical.

Kind regards
A.H.

natedawg
Champ in-the-making
Champ in-the-making
Hi Vasile,

Thx againg for a very quick response.
Your answer regarding case 2 sounds like a dream, I will try to change my code during this day and tomorrow, and test a bit.
Thx a lotSmiley Happy

Kind regards
A.H.

vasile_dirla
Star Contributor
Star Contributor
case 1. "I think it's safe" i said that because I really think it's safe. Smiley Happy (nobody is perfect so there is a chance for me to be wrong, but I still think it's safe)
now let's take it seriously.. yes it's safe to use prototype.

As the documentation said:
"using field injection is not thread-safe"
because by default you'll get the same bean instance and you can change the value of the field from another thread.

by using prototype is safe because on another call you'll get another instance and will change the field of another bean instance. (so it's safe 100%)

case 2: yes you can create a spring bean which implements the JavaDelegate interface.
this way you'll have to implement a method execute(DelegateExecution)

but you can also create a spring bean which does not implements JavaDelegate but still have access to the DelegateExecution

<code>
<serviceTask id="javaService"
             name="Start App Service Task"
             activiti:expression="#{myBean.startApp(execution, appType , appName, appParam)}" />
</code>

"The first parameter passed is the DelegateExecution, which is available in the expression context by default available as execution"

appType , appName, appParam: should also be Expressions inside your bean.

vasile_dirla
Star Contributor
Star Contributor
don't forget, the community is waiting your result. Smiley Happy