做者:Jesaijava
你一直問爲何到不了遠方,請停下數數你的腳步,是否是還沒邁開腿express
對於沒有接觸過groovy腳本語言的人來講,可能比較難使用數組
應用場景:mvc
Activiti腳本任務比較少用,腳本任務通常是用在當前的監聽器或者監聽服務類都不能知足的情形下面,或者說後期系統維護,忽然在不想改動系統的狀況下須要對流程作一些適當的改變。僅僅是幾個變量或者僅僅是一個計算公式等等。這個時候可使用腳本任務。至於還用其餘的做用,我暫時沒去多瞭解。app
官方解釋:this
Script Task(腳本服務)spa
A script task is an automatic activity. When a process execution arrives at the script task, the corresponding script is executed.設計
腳本任務是一個自動化活動。當一個流程執行到達腳本任務時,執行相應的腳本。code
Graphical Notation(圖形)orm
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