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”
流程设计的代码:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<process id="process" isExecutable="true">
<startEvent id="sid-42267A2F-38D1-4A65-A383-6E74430FACC1" activiti:initiator="admin"></startEvent>
<scriptTask id="sid-2E17D24E-A933-48E8-8A86-7A05DD363386" name="脚本任务" scriptFormat="groovy" activiti:autoStoreVariables="false">
<script><![CDATA[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()]]></script>
</scriptTask>
<endEvent id="sid-315AF00C-DE1A-4390-89B9-F33640B62AAF"></endEvent>
<sequenceFlow id="sid-19DCED8B-09E1-4E5C-B81C-4B02B09ED6E9" sourceRef="sid-2E17D24E-A933-48E8-8A86-7A05DD363386" targetRef="sid-315AF00C-DE1A-4390-89B9-F33640B62AAF"></sequenceFlow>
<sequenceFlow id="sid-A0230878-472D-4D2B-8FF9-8D7CC9DC5621" sourceRef="sid-42267A2F-38D1-4A65-A383-6E74430FACC1" targetRef="sid-2E17D24E-A933-48E8-8A86-7A05DD363386"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_process">
<bpmndi:BPMNPlane bpmnElement="process" id="BPMNPlane_process">
<bpmndi:BPMNShape bpmnElement="sid-42267A2F-38D1-4A65-A383-6E74430FACC1" id="BPMNShape_sid-42267A2F-38D1-4A65-A383-6E74430FACC1">
<omgdc:Bounds height="30.0" width="30.0" x="106.75" y="97.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-2E17D24E-A933-48E8-8A86-7A05DD363386" id="BPMNShape_sid-2E17D24E-A933-48E8-8A86-7A05DD363386">
<omgdc:Bounds height="80.0" width="100.0" x="252.75" y="72.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-315AF00C-DE1A-4390-89B9-F33640B62AAF" id="BPMNShape_sid-315AF00C-DE1A-4390-89B9-F33640B62AAF">
<omgdc:Bounds height="28.0" width="28.0" x="405.0" y="98.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="sid-19DCED8B-09E1-4E5C-B81C-4B02B09ED6E9" id="BPMNEdge_sid-19DCED8B-09E1-4E5C-B81C-4B02B09ED6E9">
<omgdi:waypoint x="352.75" y="112.0"></omgdi:waypoint>
<omgdi:waypoint x="405.0" y="112.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-A0230878-472D-4D2B-8FF9-8D7CC9DC5621" id="BPMNEdge_sid-A0230878-472D-4D2B-8FF9-8D7CC9DC5621">
<omgdi:waypoint x="136.75" y="112.0"></omgdi:waypoint>
<omgdi:waypoint x="252.75" y="112.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</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方法:
package light.mvc.workflow.scriptTask; /** * * 项目名称:lightmvc * 类名称:ScriptTask * 类描述: * 创建人:邓家海 * 创建时间:2017年6月4日 下午8:02:29 * 修改人:deng * 修改时间:2017年6月4日 下午8:02:29 * 修改备注: * @version * */ public class ScriptTask { public void ScriptTaskWriteToConsole(){ System.out.println("hellow,It is ScriptTask Running!test success!"); } }
最后部署流程,运行:
Activiti交流QQ群:634320089
Activiti脚本任务(ScriptTask)的更多相关文章
- activiti 清库脚本(转)
在使用activiti 的时候会经常遇到需要清空数据库中的数据,因此本文重点讲解如何解决该问题. 再删除数据的时候,需要注意有主外键约束的问题?下面罗列的DDL可以结合自身的业务需求进行灵活改造. D ...
- activiti学习总结
Activiti界面元素的使用总结 一.图形设计中元素的使用 1.SequenceFlow:连接线,可以连接两个任务,来管理流程实例的流向 -----General -----id:流程的id,用与程 ...
- Activiti工作流学习笔记
先从工作流的启动开始讲,Activiti提供了四种工作流的启动方式 1.空启动事件 2.定时启动事件 3.异常启动事件 4.消息启动事件 空启动事件中标签内没有任何其他元素的定义 <startE ...
- 最近学习工作流 推荐一个activiti 的教程文档
全文地址:http://www.mossle.com/docs/activiti/ Activiti 5.15 用户手册 Table of Contents 1. 简介 协议 下载 源码 必要的软件 ...
- Activiti实战04_简单流程
在Activiti实战03_Hello World中我们介绍了一个中间没有任何任务的流程,实现了流程的部署与查阅,而在本章中,将会为流程添加任务节点,是流程能够像个流程,变得更加丰满起来. 在上一节的 ...
- 【Activiti工作流引擎】官方快速入门demo
Activiti官方快速入门demo 地址: https://www.activiti.org/quick-start 0. 版本 activiti 5.22.0 JDK 1.8 1. 介绍 这个快速 ...
- Activiti Exploer工作流控制台使用指南!使用Activiti Explorer定义部署执行工作流
Activiti Explorer简介 Activiti Explorer: Activiti控制台,是一个web应用程序 从Activiti的官方网站下载Activiti的压缩zip文件时,Acti ...
- activiti入门
一.Activiti简介 Activiti 是一个针对商务人士. 开发人员和系统管理员的轻量级的工作流和业务流程管理 (BPM) 平台.它的核心是Java的高速和可靠的 BPMN 2 流程引擎.它是开 ...
- Activiti 学习笔记记录
官方在线用户手册(英文版):http://activiti.org/userguide/index.html 中文用户手册:http://www.mossle.com/docs/activiti/in ...
随机推荐
- Springboot学习笔记(一)—— 安装
springboot越来越流行了,相比较于springMVC,springboot采用了一种约定大于配置的理念,可以一键安装,一键运行,一键部署,内置tomcat,省去了一大堆配置的时间,并且,spr ...
- react 父组件与子组件双向绑定
在项目中我们可能会遇到类似这样的场景,也就是父子组件的双向数据绑定 首先,先把在head中引入react.js.react-dom.js和可选择的babel.js(这里需要注意引用的顺序,react. ...
- 应用八:Vue之在nginx下的部署实践
最近有时间研究了下前端项目如何在nginx服务器下进行部署,折腾了两天总算有所收获,汗~~ 所以就想着写篇文章来总结一下,主要包括以下三个方面: 1.打包好的vue项目如何进行部署. 2.如何反向代理 ...
- CentOS 7 修改root密码
1.开机,在启动菜单上选择CentOS Linux (3.10**.**.x86**) 7 (Core) 按下e,进入编辑模式2.将光标一直移动到 LANG=en_US.UTF-8 后面(如果找不到, ...
- CP防火墙升级和打补丁
CP防火墙的升级和打补丁可以在命令行下操作,也可以在web ui下进行,CP的升级首先得升级Deployment Agent软件 Step1:升级Deployment Agent ========== ...
- 第二阶段:2.商业需求分析及BRD:7.商业需求文档3
BRD模版 阐述需求来源以及调研分析情况 百度指数工具.定量的数据.发展趋势,是否与公司的战略冲突.环境政策:比如做内容的运营. 决策层看重的! 第二大块. 通过什么方式解决这个需求. 规划能力.类似 ...
- callback、promise和async、await的使用方法
callback 回调是一个函数被作为一个参数传递到另一个函数里,在那个函数执行完后再执行.通俗的讲就是 B函数被作为参数传递到A函数里,在A函数执行完后再执行B. promise Promise 是 ...
- 选择合适的最短路--hdu3499
[题目链接](http://acm.hdu.edu.cn/showproblem.php?pid=3499) 刚看见题目,哦?不就是个最短路么,来,跑一下dijkstra记录最长路除个二就完事了 ,但 ...
- ELK学习实验007:Nginx的日志分析系统之Metribeat配置
一 Metricbeat 简介 1.1 系统级监控,更简洁将 Metricbeat 部署到您的所有 Linux.Windows 和 Mac 主机,并将它连接到 Elasticsearch 就大功告成了 ...
- windows10 powershell上切换至cmd
前言 在windows10 上是遇到了坑,因为出现了这样的情况!不要说什么盗版,公司买的正版呢. 上图是powershell,下图是 cmd,然后我同样使用powershell 与 cmd,查询nod ...