【弄nèng - Activiti6】Activiti6入门篇(十)—— 脚本任务

文章目录

    • 1. 脚本任务
      • 1.1 流程设计
      • 1.2 测试类
      • 1.5 运行
        • 1.5.1 启动任务
    • 源码地址
    • 项目推荐

Activiti中脚本任务一般是用在当前的监听器或者监听服务类都不能满足的情形下面,或者说后期系统维护,突然在不想改动系统的情况下需要对流程做一些适当的改变。仅仅是几个变量或者仅仅是一个计算公式等等。这个时候可以使用脚本任务。一般有三种使用方式,Javascript, Juel, Groovy脚本。以下讲述avascript, Juel。

*后台服务基于Springboot2 + Activiti6,整合文章请参考:https://blog.csdn.net/yy756127197/article/details/101211510 不需要流程设计器就排除3,4步骤 *

1. 脚本任务

1.1 流程设计

javascript节点设置
【弄nèng - Activiti6】Activiti6入门篇(十)—— 脚本任务_第1张图片
Script内容:

var myVar = "sima";
execution.setVariable("user",  myVar);
for(var i=0; i<10; i++){
print(i)
}

execution是内置 对象

juel
【弄nèng - Activiti6】Activiti6入门篇(十)—— 脚本任务_第2张图片
Script内容:

${myBean.print(execution)}

myBean是变量,启动流程时候传递的

流程图
【弄nèng - Activiti6】Activiti6入门篇(十)—— 脚本任务_第3张图片

流程文件bpmn


<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="scriptTask" name="scriptTask" isExecutable="true">
    <documentation>scriptTaskdocumentation>
    <startEvent id="startEvent1">startEvent>
    <scriptTask id="sid-CF08BBA7-509F-4246-B4BC-939236D90185" name="javascript任务" scriptFormat="javascript" activiti:autoStoreVariables="false">
      <script><![CDATA[var myVar = "sima";
execution.setVariable("user",  myVar);
for(var i=0; i<10; i++){
print(i)
}]]>script>
    scriptTask>
    <sequenceFlow id="sid-EB8225D9-E9B8-4E6D-ADDB-59BFDA5F3DCB" sourceRef="startEvent1" targetRef="sid-CF08BBA7-509F-4246-B4BC-939236D90185">sequenceFlow>
    <scriptTask id="sid-07E57300-6B1F-47E3-ACFD-5210B7CA4847" name="juel脚本" scriptFormat="juel" activiti:autoStoreVariables="false">
      <script><![CDATA[${myBean.print(execution)}]]>script>
    scriptTask>
    <sequenceFlow id="sid-A920AD82-A34C-4EBD-9943-8BFB9BC41B5C" sourceRef="sid-CF08BBA7-509F-4246-B4BC-939236D90185" targetRef="sid-07E57300-6B1F-47E3-ACFD-5210B7CA4847">sequenceFlow>
    <endEvent id="sid-F670C82B-26AA-4DA3-9692-CCAFA6ECE1C4">endEvent>
    <sequenceFlow id="sid-6DF92EFE-7A7E-48B9-9EC4-60D00DDDCE08" sourceRef="sid-07E57300-6B1F-47E3-ACFD-5210B7CA4847" targetRef="sid-F670C82B-26AA-4DA3-9692-CCAFA6ECE1C4">sequenceFlow>
  process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_scriptTask">
    <bpmndi:BPMNPlane bpmnElement="scriptTask" id="BPMNPlane_scriptTask">
      <bpmndi:BPMNShape bpmnElement="startEvent1" id="BPMNShape_startEvent1">
        <omgdc:Bounds height="30.0" width="30.0" x="135.0" y="163.0">omgdc:Bounds>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-CF08BBA7-509F-4246-B4BC-939236D90185" id="BPMNShape_sid-CF08BBA7-509F-4246-B4BC-939236D90185">
        <omgdc:Bounds height="80.0" width="100.0" x="270.0" y="138.0">omgdc:Bounds>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-07E57300-6B1F-47E3-ACFD-5210B7CA4847" id="BPMNShape_sid-07E57300-6B1F-47E3-ACFD-5210B7CA4847">
        <omgdc:Bounds height="80.0" width="100.0" x="480.0" y="140.0">omgdc:Bounds>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-F670C82B-26AA-4DA3-9692-CCAFA6ECE1C4" id="BPMNShape_sid-F670C82B-26AA-4DA3-9692-CCAFA6ECE1C4">
        <omgdc:Bounds height="28.0" width="28.0" x="675.0" y="166.0">omgdc:Bounds>
      bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sid-A920AD82-A34C-4EBD-9943-8BFB9BC41B5C" id="BPMNEdge_sid-A920AD82-A34C-4EBD-9943-8BFB9BC41B5C">
        <omgdi:waypoint x="370.0" y="178.0">omgdi:waypoint>
        <omgdi:waypoint x="480.0" y="178.0">omgdi:waypoint>
      bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-6DF92EFE-7A7E-48B9-9EC4-60D00DDDCE08" id="BPMNEdge_sid-6DF92EFE-7A7E-48B9-9EC4-60D00DDDCE08">
        <omgdi:waypoint x="580.0" y="180.0">omgdi:waypoint>
        <omgdi:waypoint x="675.0" y="180.0">omgdi:waypoint>
      bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-EB8225D9-E9B8-4E6D-ADDB-59BFDA5F3DCB" id="BPMNEdge_sid-EB8225D9-E9B8-4E6D-ADDB-59BFDA5F3DCB">
        <omgdi:waypoint x="165.0" y="178.0">omgdi:waypoint>
        <omgdi:waypoint x="270.0" y="178.0">omgdi:waypoint>
      bpmndi:BPMNEdge>
    bpmndi:BPMNPlane>
  bpmndi:BPMNDiagram>
definitions>

1.2 测试类

ServiceTaskTest .java

import com.it.cloud.modules.activiti.service.IActReModelService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务任务测试
 */
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class ServiceTaskTest {

    @Autowired
    private IActReModelService actReModelService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private HistoryService historyService;

    /**
     * 部署流程定义
     */
    @Test
    public void deploy() {
        Deployment deployment = repositoryService.createDeployment() // 创建部署
                .addClasspathResource("diagrams/serviceTask.bpmn20.xml") // 加载流程资源文件
                .name("serviceTask流程") // 流程名称
                .deploy(); // 部署
        System.out.println("流程部署ID:" + deployment.getId());
        System.out.println("流程部署Name:" + deployment.getName());
    }

    /**
     * 启动流程实例
     */
    @Test
    public void start() {
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("serviceTask"); // 流程定义表的KEY字段值
        System.out.println("流程实例ID:" + pi.getId());
        System.out.println("流程定义ID:" + pi.getProcessDefinitionId());
    }

    /**
     * 查看任务
     */
    @Test
    public void queryTask() {
        List<Task> taskList = taskService.createTaskQuery() // 创建任务查询
                .taskAssignee("admin") // 指定某个人
                .list();
        for (Task task : taskList) {
            System.out.println("任务ID:" + task.getId());
            System.out.println("任务名称:" + task.getName());
            System.out.println("任务创建时间:" + task.getCreateTime());
            System.out.println("任务委派人:" + task.getAssignee());
            System.out.println("流程实例ID:" + task.getProcessInstanceId());
        }
    }

    /**
     * 完成任务
     */
    @Test
    public void completeTask() {
        taskService.complete("85005");
    }

    /**
     * 完成任务2
     */
    @Test
    public void completeTask2() {
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("days", 7);
        taskService.complete("165005", variables);
    }
}

1.5 运行

运行deploy()

1.5.1 启动任务

运行start()

效果:
【弄nèng - Activiti6】Activiti6入门篇(十)—— 脚本任务_第4张图片
数据表ACT_HI_VARINST
在这里插入图片描述


源码地址

IT-CLOUD-ACTIVITI6
开源项目,持续更新中,喜欢请 Star~

项目推荐

IT-CLOUD :IT服务管理平台,集成基础服务,中间件服务,监控告警服务等。
开源项目,持续更新中,喜欢请 Star~

你可能感兴趣的:(Activiti6,activiti)