activiti6中的脚本任务可以通过scriptFormat指定脚本格式来执行对应的脚本,scriptFormat属性的值必须是与JSR-223(Java平台的脚本编写)兼容的名称。
需要执行python脚本需要选择的脚本格式为jython
需要引入的jar:
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter-basic</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>org.python</groupId>
<artifactId>jython</artifactId>
<version>2.7.2</version>
</dependency>
1.创建一个简单的流程如下:
脚本任务中的属性配置为:
整个流程的ID设置为python_test_1
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" id="Definitions_152bf5q" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="3.0.0">
<bpmn:process id="python_test_1" isExecutable="true">
<bpmn:startEvent id="StartEvent_1" name="开始">
<bpmn:outgoing>SequenceFlow_0bdle5t</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:sequenceFlow id="SequenceFlow_0bdle5t" sourceRef="StartEvent_1" targetRef="Task_0nvspvr" />
<bpmn:endEvent id="EndEvent_1rospzp" name="结束">
<bpmn:incoming>SequenceFlow_0h63l8b</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="SequenceFlow_0h63l8b" sourceRef="Task_0nvspvr" targetRef="EndEvent_1rospzp" />
<bpmn:scriptTask id="Task_0nvspvr" scriptFormat="jython">
<bpmn:incoming>SequenceFlow_0bdle5t</bpmn:incoming>
<bpmn:outgoing>SequenceFlow_0h63l8b</bpmn:outgoing>
<bpmn:script>print("hello 2022!!!!")</bpmn:script>
</bpmn:scriptTask>
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="python_test_1">
<bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
<dc:Bounds x="179" y="103" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="186" y="146" width="22" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="SequenceFlow_0bdle5t_di" bpmnElement="SequenceFlow_0bdle5t">
<di:waypoint x="215" y="121" />
<di:waypoint x="265" y="121" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="EndEvent_1rospzp_di" bpmnElement="EndEvent_1rospzp">
<dc:Bounds x="415" y="103" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="422" y="146" width="22" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="SequenceFlow_0h63l8b_di" bpmnElement="SequenceFlow_0h63l8b">
<di:waypoint x="365" y="121" />
<di:waypoint x="415" y="121" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="ScriptTask_0k3ur0y_di" bpmnElement="Task_0nvspvr">
<dc:Bounds x="265" y="81" width="100" height="80" />
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>
2.在Controller中的调用方法如下:
@GetMapping("/api/bpmn/python_test")
public void mysql_test(){
Deployment deployment = repositoryService.createDeployment().name("Python测试任务").addClasspathResource("processes/pythonTest_01.bpmn").deploy();
log.error("部署 ID is:{} name is:{}", deployment.getId(), deployment.getName());
Properties props = new Properties();
props.put("python.home", "path to the Lib folder");
props.put("python.console.encoding", "UTF-8");
props.put("python.security.respectJavaAccessibility", "false");
props.put("python.import.site", "false");
Properties preprops = System.getProperties();
PythonInterpreter.initialize(preprops, props, new String[0]);
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("python_test_1");
log.error("流程实例ID is:{}, \n流程定义ID is:{}, ", processInstance.getId(), processInstance.getProcessDefinitionId());
}
3.运行结果: