本文主要是介绍BPMN2.0脚本任务,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. 脚本任务
-
JUEL脚本(默认)(Java Unified Expression Lanaguage Java统一表达语言)
-
Groovy脚本(依赖groovy-all.jar)
-
JavaScript脚本
<scriptTask id="scripttask" name="Script Task"><script>${bean.invoke()}</script>
</scriptTask>
2. 脚本任务(Script Task)内置变量
添加依赖:
<dependency><groupId>org.codehaus.groovy</groupId><artifactId>groovy-all</artifactId>
</dependency>
内置变量 execution, xxxService, processEngineConfiguration
保留关键字,不能用作变量使用:out, out:print, lang:Import, context, elcontext
3. 脚本任务(Script Task)设置返回值
JUEL:
JavaScript:
(1)使用groovy脚本
测试代码
my-process-scripttask1.bpmn20.xml中部分代码:
<process id="my-process"><startEvent id="start" />
<sequenceFlow id="flow1" sourceRef="start" targetRef="someTask" /><scriptTask id="someTask" name="Script Task"
scriptFormat="groovy"><script>def myValue = "value123"execution.setVariable("myKey", myValue)</script>
</scriptTask>
<sequenceFlow id="flow2" sourceRef="someTask" targetRef="end" /><endEvent id="end" />
</process>
测试代码:
public class ScriptTaskTest {
private static final Logger LOGGER = LoggerFactory.getLogger(ScriptTaskTest.class);@Rule
public ActivitiRule activitiRule = new ActivitiRule();@Test
@Deployment(resources = {"my-process-scripttask1.bpmn20.xml"})
public void testUserTask(){ProcessInstance processInstance = activitiRule.getRuntimeService().startProcessInstanceByKey("my-process");HistoryService historyService = activitiRule.getHistoryService();List<HistoricVariableInstance> historicVariableInstances = historyService.createHistoricVariableInstanceQuery().processInstanceId(processInstance.getId()).orderByVariableName().asc().listPage(0, 100);for(HistoricVariableInstance historicVariableInstance : historicVariableInstances){LOGGER.info("variable = {}", historicVariableInstance);}LOGGER.info("variables.size = {}", historicVariableInstances.size());}
}
测试输出:
(2)使用JUEL脚本(设置返回值):
<process id="my-process"><startEvent id="start" />
<sequenceFlow id="flow1" sourceRef="start" targetRef="someTask" /><scriptTask id="someTask" name="Script Task" scriptFormat="juel" activiti:resultVariable="mySum"><script>#{key1 + key2}</script>
</scriptTask>
<sequenceFlow id="flow2" sourceRef="someTask" targetRef="end" /><endEvent id="end" />
</process>
测试代码:
@Test
@Deployment(resources = {"my-process-scripttask2.bpmn20.xml"})
public void testScriptTask2(){Map<String, Object> variables = Maps.newHashMap();variables.put("key1", 3);variables.put("key2", 5);ProcessInstance processInstance = activitiRule.getRuntimeService().startProcessInstanceByKey("my-process", variables);HistoryService historyService = activitiRule.getHistoryService();List<HistoricVariableInstance> historicVariableInstances = historyService.createHistoricVariableInstanceQuery().processInstanceId(processInstance.getId()).orderByVariableName().asc().listPage(0, 100);for(HistoricVariableInstance historicVariableInstance : historicVariableInstances){LOGGER.info("variable = {}", historicVariableInstance);}LOGGER.info("variables.size = {}", historicVariableInstances.size());}
测试输出:
(3)使用JavaScript脚本
只需要将(2)中流程定义文件中的脚本类型改为javascript,脚本内容改为key1 + key2
<scriptTask id="someTask" name="Script Task"
scriptFormat="javascript" activiti:resultVariable="mySum"><script>key1 + key2</script>
</scriptTask>
测试输出:
可以用脚本实现的就定义在脚本定义文件里面,避免修改java代码。
这篇关于BPMN2.0脚本任务的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!