Activity脚本任务(ScriptTask)

Activity脚本任务(ScriptTask)

作者:邓家海

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

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

应用场景:

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

官方解释:

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 }

 

最后部署流程,运行:

Activity交流QQ群:634320089

 

时间: 2024-07-30 23:58:08

Activity脚本任务(ScriptTask)的相关文章

开启真机的View Server引入HierarchyViewer/By写monkeyrunner自动化测试脚本

其实相关文章网上也有不少了,不过在真机上开启View Server的中文文章好像只有一篇,前段时间按照这篇文章的内容,并结合英文源文去hack我的Nexus S(4.1.2)也走了一点弯路.现在总结一下我的步骤(其实有相当一部分拷贝了这篇,衷心感谢原文作者).并写点在开启View Server之后monkeyrunner的脚本. 先交待一下背景,monkeyrunner作为自动化测试Android系统工具在某些情况下还是比Robotium易用一些,不过monkeryrunner判断测试结果是否正

Android中Activity启动模式全面解析

在Android应用中, Activity是最核心的组件, 如何生成一个Activity实例, 可以选择不同的启动模式, 即LaunchMode. 启动模式主要包括: standard, singleTop, singleTask, singleInstance. 标准模式在每次启动时, 都会创建实例; 三种单例模式, 会根据情况选择创建还是复用实例. 在Activity启动中, 创建实例的生命周期: onCreate -> onStart -> onResume; 重用实例的生命周期: on

第2章2节《MonkeyRunner源码剖析》了解你的测试对象: NotePad窗口Activity之NotesList简介(原创)

天地会珠海分舵注:本来这一系列是准备出一本书的,详情请见早前博文"寻求合作伙伴编写<深入理解 MonkeyRunner>书籍".但因为诸多原因,没有如愿.所以这里把草稿分享出来,所以错误在所难免.有需要的就参考下吧,转发的话还请保留每篇文章结尾的出处等信息. NotePad窗口Activity之NotesList简介 上一节我们简要描述了测试对象NotePad的主要功能模块,那么这一节开始我们就会对每个模块进行相应的阐述,这样读者就算没有真正去安装和玩过这个应用也不会影响大

第2章3节《MonkeyRunner源码剖析》了解你的测试对象: NotePad窗口Activity之NoteEditor简介(原创)

天地会珠海分舵注:本来这一系列是准备出一本书的,详情请见早前博文"寻求合作伙伴编写<深入理解 MonkeyRunner>书籍".但因为诸多原因,没有如愿.所以这里把草稿分享出来,所以错误在所难免.有需要的就参考下吧,转发的话还请保留每篇文章结尾的出处等信息. 我们在增加和编辑一个日记的时候会从NotesList这个Activity进入到NoteEditor这个Activity.增加和编辑的时候的控件都是一样的,只是他们的标题内容会有区别. 下面我们先来通过hierarchy

Android Monkey 脚本编写与检查内存泄露

一.Monkey脚本编写 1.Monkey脚本格式 脚本优势: 简单快捷,不需要接触任何工具,只需要一个记事本文件 脚本缺点: 实现坐标.按键等基本操作的相应步骤,顺序脚本无逻辑性 脚本源码: \development\cmds\monkey\src\com\android\commands\monkey\MonkeySourceScrip.java #头文件.控制monkey发送消息的参数 type=raw events count=10 speed=1.0 #以下为monkey命令 star

MonkeyRunner 之如何获取APP的Package Name和Activity Name

MonkeyRunner 之如何获取APP的Package Name和Activity Name   最近尝试学习使用monkeyrunner进行Android自动化测试,一开始均是使用点击屏幕坐标的方法来运行应用程序,可是点击屏幕坐标的方法比较麻烦,且通用性差.通过度娘我知道可以使用device.startActivity("package name /activity")这个函数来直接启用安装在手机中的app,可是如何获得activity却成了关键,现将获取activity的方法总

Gradle Android最新自动化编译脚本教程

转自:http://blog.csdn.net/changemyself/article/details/39927381 一.前言 Gradle 是以 Groovy 语言为基础,面向Java应用为主.基于DSL(领域特定语言)语法的自动化构建工具. 上面这句话我觉得写得很官方,大家只需知道Gradle可以用来android开发中进行多个项目依赖的自动化编译脚本,知道这点也就知道我们使用它的目的: 为什么不使用Ant做自动化编译脚本,因为ant上手快,但是维护起来太不方便了,有了Gradle你可

Android APP压力测试(二)之Monkey信息自动收集脚本【转】

前言: 上一篇Monkey介绍基本搬抄官方介绍,主要是为了自己查阅方便.本文重点介绍我在进行Monkey时如何自动收集相关信息,主要收集Monkey测试日志.手机日志.手机屏幕截图.测试手机信息,自动按次按时间点保存信息.只需轻轻一点,腾出手腾出脑想干吗干吗,执行结束应该有信息的都有收集,一定程序提升了效率,节约了时间.可以偷空看看美图.聊天扯淡...哦不,是学习提高审美观,沟通交流增进同事情感... 转载请注明出处:Findyou   http://www.cnblogs.com/findyo

Android APP压力测试(二)之Monkey信息自动收集脚本

Android APP压力测试(二) 之Monkey信息自动收集脚本 前言: 上一篇Monkey介绍基本搬抄官方介绍,主要是为了自己查阅方便.本文重点介绍我在进行Monkey时如何自动收集相关信息,主要收集Monkey测试日志.手机日志.手机屏幕截图.测试手机信息,自动按次按时间点保存信息.只需轻轻一点,腾出手腾出脑想干吗干吗,执行结束应该有信息的都有收集,一定程序提升了效率,节约了时间.可以偷空看看美图.聊天扯淡...哦不,是学习提高审美观,沟通交流增进同事情感... 转载请注明出处:Find