cancel
Showing results for 
Search instead for 
Did you mean: 

Accessing Process Variable in external Groovy Script launched from Java Service Task

sankarts
Champ in-the-making
Champ in-the-making
My requirement is to launch external Groovy script from Java Service Task (which implements JavaDelegate).

In the Groovy script, I need to access the "Process Variable" through Execution Context i.e. DelegateExecution.


        def myArray = [] as ArrayList
   myArray  = (ArrayList)execution.getVariable("myGetVar");
   execution.setVariable("mySetVar", myArray.get(myArray.size()-1) );

Just wondering, what could be the preferred way to pass "execution" context from Java class to external Groovy script file?

Thanks in advance.

7 REPLIES 7

sankarts
Champ in-the-making
Champ in-the-making
Any inputs / comments on this please?

martin_grofcik
Confirmed Champ
Confirmed Champ
Hi,
I would prefer to pass variable values to the external script as an arguments.

Regards
Martin

sankarts
Champ in-the-making
Champ in-the-making
Martin,

Thanks for the response.

I do understand passing the variable as argument is the preferred option.

<java>
public class AutoRouteSelection implements JavaDelegate {
….
public void execute(DelegateExecution execution) throws Exception {
            GroovyScriptEngine gse = new GroovyScriptEngine( "." );
     Binding binding = new Binding();
     binding.setVariable("delegateExec", execution);
     gse.run(strGroovyScriptName, binding);
        }
}
</java>

The catch is that how to access "DelegateExecution execution" in Groovy script, which is passed as argument from Java Service Task.

jbarrez
Star Contributor
Star Contributor
What's wrong with using the script task? Which can be used with Groovy and where the binding is already implemented by us?

sankarts
Champ in-the-making
Champ in-the-making
The Script Task works fine with accessing the process variables.

If Groovy Script involves complex logic, it is better to keep it as external file, rather than inline within the BPMN spec.

PLease refer
<bbcode>
http://forums.activiti.org/content/script-task-using-external-files-source
</bbcode>

and
<bbcode>
http://jira.codehaus.org/browse/ACT-2178
</bbcode>

In this backdrop, how can I pass "DelegateExecution execution", so as to access Process Varibales from external Groovy Script launched from "Service Task"? Thanks.

sankarts
Champ in-the-making
Champ in-the-making
Joram,

Objective: To access Process variables in external Groovy Script file

Can you comment on how we can pass "DelegateExecution execution" context so as to access Process Variables from external Groovy Script? Thanks.


jbarrez
Star Contributor
Star Contributor
In m opinion, it would be better to expand the ScriptTask to be able to use external files. That way other people who have the same use case would benefit from the effort.

>In this backdrop, how can I pass "DelegateExecution execution", so as to access Process Varibales from external Groovy Script launched from "Service Task"? Thanks.

You would need to create Bindings, like we do : https://github.com/Activiti/Activiti/blob/master/modules/activiti-engine/src/main/java/org/activiti/...