Activiti脚本任务(ScriptTask)

Activiti脚本任务(ScriptTask

作者:Jesai

你一直问为什么到不了远方,请停下数数你的脚步,是不是还没迈开腿

对于没有接触过groovy脚本语言的人来说,可能比较难使用

应用场景:

Activiti脚本任务比较少用,脚本任务一般是用在当前的监听器或者监听服务类都不能满足的情形下面,或者说后期系统维护,突然在不想改动系统的情况下需要对流程做一些适当的改变。仅仅是几个变量或者仅仅是一个计算公式等等。这个时候可以使用脚本任务。至于还用其他的作用,我暂时没去多了解。

 

官方解释:

Script Task(脚本服务)

A script task is an automatic activity. When a process execution arrives at the script task, the corresponding script is executed.

脚本任务是一个自动化活动。当一个流程执行到达脚本任务时,执行相应的脚本。

Graphical Notation(图形)

 

A script task is visualized as a typical BPMN 2.0 task (rounded rectangle), with a small 'script' icon in the top-left corner of the rectangle.

d的的脚本任务可视化为一个典型的BPMN 2.0 任务(圆角矩形),在矩形的左上角带有一个小的‘脚本’图标。

 

A script task is defined by specifying the script and the scriptFormat.

通过指定脚本(script )和脚本格式(scriptFormat)定义一个脚本任务。

 

<scriptTask id="theScriptTask" name="Execute script" scriptFormat="groovy">

  <script>

    sum = 0

    for ( i in inputArray ) {

      sum += i

    }

  </script>

</scriptTask>

 

The value of the scriptFormat attribute must be a name that is compatible with the JSR-223 (scripting for the Java platform). The Groovy jar is shipped by default with the Activiti distribution. If you want to use another (JSR-223 compatible) scripting engine, it is sufficient to add the corresponding jar to the classpath and use the appropriate name.

scriptFormat属性的值必须是一个和JSR-223 兼容的名称(Java平台上的脚本系统)。发布包缺省带有Groovy jar包。如果你想使用另外的脚本引擎,将相应的jar包加入classpath里。

Variables in scripts(脚本里的变量)

 

All process variables that are accessible through the execution that arrives in the script task, can be used within the script. In the example, the script variable 'inputArray' is in fact a process variable (an array of integers).

通过到达在脚本任务里的执行,所有可以访问的流程变量也能够在脚本里面使用。在本例中,脚本变量'inputArray'事实上是一个流程变量(一个整数数组)。

 

<script>

    sum = 0

    for ( i in inputArray ) {

      sum += i

    }

</script>

 

It's also possible to set process variables in a script, simply by using an assignment statement. In the example above, the 'sum' variable will be stored as a process variable after the script task has been executed. To avoid this behavior, script-local variables can be used. In Groovy, the keyword 'def' must then be used: 'def sum = 0'. In that case, no process variable will be stored.

简单地通过使用一个赋值语句,也可能在一个脚本里设置流程变量。在上例,在脚本任务已经执行之后, 'sum'变量将保存为一个流程变量。为了避免这个行为,能够使用脚本局部变量。在Groovy里面,必须使用关键字 'def' : 'def sum = 0'。在那种情况下,将不保存任何流程变量。

 

An alternative is to set variables through the current execution, which is available as a reserved variable called 'execution'.

通过当前执行,有另外设置变量的可选方式,作为叫做 'execution'的保留变量可以获得。

 

<script>

    def scriptVar = "test123"

    execution.setVariable("myVar", scriptVar)

</script>

 

Note: the following names are reserved and cannot be used as variable names: out, out:print, lang:import, context, elcontext.

注意:下列名称将要保留并且不能作为变量名被使用(cannot be used):out, out:print, lang:import, context, elcontext。

Script results(脚本结果)

 

The return value of a script task can be assigned to an already existing or to a new process variable by specifying the process variable name as a literal value for the'activiti:resultVariableName' attribute of a script task definition. Any existing value for a specific process variable will be overwritten by the result value of the script execution. When not specifying a result variable name, the script result value gets ignored.

为了一个脚本任务定义的属性 'activiti:resultVariableName' ,通过指定流程变量名称为一个字面值,脚本任务的返回值能够分配给已经存在的或者一个新的流程变量。对于一个特定的流程变量的任何存在的值将被脚本执行的结果值复写。当没有指定一个结果变量值,脚本结果值被忽视。

 

<scriptTask id="theScriptTask" name="Execute script" scriptFormat="juel" activiti:resultVariableName="myVar">

  <script>#{echo}</script>

</scriptTask>

 

In the above example, the result of the script execution (the value of the resolved expression '#{echo}') is set to the process variable named 'myVar' after the script completes.

的在上例里,在脚本完成之后,脚本执行(解析变量 '#{echo}'的值)的结果被设置为名叫'myVar'的流程变量。

 

我们将会使用Groovy脚本来实现脚本任务。

 

什么是Groovy脚本?

Groovy是一种基于JVM(Java虚拟机)的敏捷开发语言,它结合了Python、Ruby和Smalltalk的许多强大的特性,Groovy 代码能够与 Java 代码很好地结合,也能用于扩展现有代码。由于其运行在 JVM 上的特性,Groovy 可以使用其他 Java 语言编写的库。

 

需要的jar包:

groovy-all-2.4.3.jar

 

ScriptTask实现:

 

流程图设计:

 

 

制定脚本的语言scriptformat=”groovy”

 

 

 

 

流程设计的代码:

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/processdef">
 3   <process id="process" isExecutable="true">
 4     <startEvent id="sid-42267A2F-38D1-4A65-A383-6E74430FACC1" activiti:initiator="admin"></startEvent>
 5     <scriptTask id="sid-2E17D24E-A933-48E8-8A86-7A05DD363386" name="脚本任务" scriptFormat="groovy" activiti:autoStoreVariables="false">
 6       <script><![CDATA[import  light.mvc.workflow.scriptTask.ScriptTask;
 7 public class GPerson {
 8     public void say(String name){
 9         println "Hello, $name! ";
10     }
11     def foo(){
12         ScriptTask p = new ScriptTask();
13         p.ScriptTaskWriteToConsole();
14     }
15     static void main(args) {
16         GPerson gp = new GPerson();
17                 gp.say("jesai!");
18         gp.foo();
19     }
20 }
21 
22 def gp = new GPerson()
23 gp.say("jesai!")
24 gp.foo()]]></script>
25     </scriptTask>
26     <endEvent id="sid-315AF00C-DE1A-4390-89B9-F33640B62AAF"></endEvent>
27     <sequenceFlow id="sid-19DCED8B-09E1-4E5C-B81C-4B02B09ED6E9" sourceRef="sid-2E17D24E-A933-48E8-8A86-7A05DD363386" targetRef="sid-315AF00C-DE1A-4390-89B9-F33640B62AAF"></sequenceFlow>
28     <sequenceFlow id="sid-A0230878-472D-4D2B-8FF9-8D7CC9DC5621" sourceRef="sid-42267A2F-38D1-4A65-A383-6E74430FACC1" targetRef="sid-2E17D24E-A933-48E8-8A86-7A05DD363386"></sequenceFlow>
29   </process>
30   <bpmndi:BPMNDiagram id="BPMNDiagram_process">
31     <bpmndi:BPMNPlane bpmnElement="process" id="BPMNPlane_process">
32       <bpmndi:BPMNShape bpmnElement="sid-42267A2F-38D1-4A65-A383-6E74430FACC1" id="BPMNShape_sid-42267A2F-38D1-4A65-A383-6E74430FACC1">
33         <omgdc:Bounds height="30.0" width="30.0" x="106.75" y="97.0"></omgdc:Bounds>
34       </bpmndi:BPMNShape>
35       <bpmndi:BPMNShape bpmnElement="sid-2E17D24E-A933-48E8-8A86-7A05DD363386" id="BPMNShape_sid-2E17D24E-A933-48E8-8A86-7A05DD363386">
36         <omgdc:Bounds height="80.0" width="100.0" x="252.75" y="72.0"></omgdc:Bounds>
37       </bpmndi:BPMNShape>
38       <bpmndi:BPMNShape bpmnElement="sid-315AF00C-DE1A-4390-89B9-F33640B62AAF" id="BPMNShape_sid-315AF00C-DE1A-4390-89B9-F33640B62AAF">
39         <omgdc:Bounds height="28.0" width="28.0" x="405.0" y="98.0"></omgdc:Bounds>
40       </bpmndi:BPMNShape>
41       <bpmndi:BPMNEdge bpmnElement="sid-19DCED8B-09E1-4E5C-B81C-4B02B09ED6E9" id="BPMNEdge_sid-19DCED8B-09E1-4E5C-B81C-4B02B09ED6E9">
42         <omgdi:waypoint x="352.75" y="112.0"></omgdi:waypoint>
43         <omgdi:waypoint x="405.0" y="112.0"></omgdi:waypoint>
44       </bpmndi:BPMNEdge>
45       <bpmndi:BPMNEdge bpmnElement="sid-A0230878-472D-4D2B-8FF9-8D7CC9DC5621" id="BPMNEdge_sid-A0230878-472D-4D2B-8FF9-8D7CC9DC5621">
46         <omgdi:waypoint x="136.75" y="112.0"></omgdi:waypoint>
47         <omgdi:waypoint x="252.75" y="112.0"></omgdi:waypoint>
48       </bpmndi:BPMNEdge>
49     </bpmndi:BPMNPlane>
50   </bpmndi:BPMNDiagram>
51 </definitions>

 

 

 

脚本语言的编写:

import  light.mvc.workflow.scriptTask.ScriptTask;

public class GPerson {

public void say(String name){

println "Hello, $name! ";

}

def foo(){

ScriptTask p = new ScriptTask();

p.ScriptTaskWriteToConsole();

}

static void main(args) {

GPerson gp = new GPerson();

                gp.say("jesai!");

gp.foo();

}

}

 

def gp = new GPerson()

gp.say("jesai!")

gp.foo()

 

 

 

 

这是一段groovy的脚本语言,也许有得人很多人并没有接触过groovy这个脚本。

 

需要调用的java方法:

 

 1 package light.mvc.workflow.scriptTask;
 2 
 3  
 4 
 5 /**  
 6 
 7  *   
 8 
 9  * 项目名称:lightmvc  
10 
11  * 类名称:ScriptTask  
12 
13  * 类描述:  
14 
15  * 创建人:邓家海  
16 
17  * 创建时间:2017年6月4日 下午8:02:29  
18 
19  * 修改人:deng  
20 
21  * 修改时间:2017年6月4日 下午8:02:29  
22 
23  * 修改备注:  
24 
25  * @version   
26 
27  *   
28 
29  */
30 
31  
32 
33 public class ScriptTask {
34 
35  
36 
37   public void ScriptTaskWriteToConsole(){
38 
39   System.out.println("hellow,It is ScriptTask Running!test success!");
40 
41   }
42 
43  
44 
45 }

 

 

最后部署流程,运行:

  Activiti交流QQ群:634320089

 

 

 

posted @ 2017-06-04 22:56  Jesai  阅读(12178)  评论(0编辑  收藏  举报