springboot2.1.3+activiti7整合

1.actiBPM 下载

在 IDEA的 File 菜单中找到子菜单”Settings”,后面我们再选择左侧的“plugins”菜单,如下图所示:

springBoot Activiti6 初始化表_xml

2.Activiti 服务架构图

springBoot Activiti6 初始化表_System_02

3.activiti 的引擎配置文件

activiti 的引擎配置文件:activiti.cfg.xml 放在resource目录下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
						http://www.springframework.org/schema/contex http://www.springframework.org/schema/context/spring-context.xsd
						http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
        <property name="jdbcDriver" value="oracle.jdbc.driver.OracleDriver"></property>
        <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe"></property>
        <property name="jdbcUsername" value="system"></property>
        <property name="jdbcPassword" value="123456"></property>

        <!-- 如果表不存在  自动创建 -->
        <property name="databaseSchemaUpdate" value="true"></property>
    </bean>
</beans>

4.新建bpmn文件

绘制流程图:resource目录下新建diagram文件夹,在文件夹中右击新建bpmn文件

springBoot Activiti6 初始化表_System_03

5.bpmn文件转化为流程图片

转化流程图(此步骤可跳过):选择已完成的bpmn文件,rename后缀bpmn为xml,然后右击选择Diagrams/show bpmn 2.0 Designer...,新窗口中点击Export to file按钮,导出图片;完成后将图片放到与bpmn文件同目录下,将.xml后缀改回.bpmn。

springBoot Activiti6 初始化表_spring_04

6.部署流程定义

代码:

/**
     * 流程部署
     */
    @RequestMapping(value = "/send_to_BPM", method = RequestMethod.POST)
    @ResponseBody
    @PreAuthorize("hasRole('OPRMGT')")
    public HashMap sendToBPM(@RequestBody HashMap<String, Object> data) {
        HashMap<String, Object> ret = new HashMap<>();
        // 检测人员
        SysUser user = authService.currentUser();
        //1.获取processEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取repositoryService对象
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //3.部署
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("diagram/holiday4.bpmn")
            	.addClasspathResource("diagram/holiday4.png")
                .name("holiday04流程")
                .deploy();
        System.out.println("流程部署id:" +deploy.getId());
        System.out.println("流程部署名称:" +deploy.getName());

        ret.put("status", true);
        return ret;
    }

7.启动流程实例

代码:

/**
     * 流程启动
     */
    @RequestMapping(value = "/BPM_start", method = RequestMethod.POST)
    @ResponseBody
    @PreAuthorize("hasRole('OPRMGT')")
    public HashMap BPMStart(@RequestBody HashMap<String, Object> data) {
        HashMap<String, Object> ret = new HashMap<>();
        //1.获取processEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取RunTimeService对象
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //3.流程定义的Key,流程图中的key myProcess_1
        String key = "myProcess_1";
        Map<String,Object> map = new HashMap<String,Object>();
        //设置全局变量
        holiday holiday = new holiday();
        holiday.setNum(1F);
        map.put("holiday",holiday);
        //4.启动流程实例,并设置流程变量的值 业务关联Id businessKey:"202004090936"
        ProcessInstance processInstance = runtimeService
            .startProcessInstanceByKey(key, "202004090936",map);

        System.out.println(processInstance.getName());
        System.out.println(processInstance.getProcessInstanceId());

        ret.put("status", true);
        return ret;
    }

8.个人任务查询

代码:

// 查询当前个人待执行的任务
@Test
public void findPersonalTaskList() {
    // 任务负责人
    String assignee = "zhangsan";
    // 创建TaskService
    TaskService taskService = processEngine.getTaskService();
    List<Task> list = taskService.createTaskQuery()//
    							 .processDefinitionKey("myProcess_1")//
    							 .taskAssignee(assignee)//只查询该任务负责人的任务
    							 .list();
    for (Task task : list) {
    	System.out.println(" 流 程 实 例 id : " +task.getProcessInstanceId());
        System.out.println("任务id:" + task.getId());
        System.out.println("任务负责人:" + task.getAssignee());
        System.out.println("任务名称:" + task.getName());
    }
}

9.流程完成

代码:

/**
     * 流程完成
     */
    @RequestMapping(value = "/BPM_complete", method = RequestMethod.POST)
    @ResponseBody
    @PreAuthorize("hasRole('OPRMGT')")
    public HashMap BPMComplete(@RequestBody HashMap<String, Object> data) {
        HashMap<String, Object> ret = new HashMap<>();
        //1.获取processEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取TaskService对象
        TaskService taskService = processEngine.getTaskService();
        //3.查询当前用户是否有任务
        String key = "myProcess_1";
        //按顺序执行
//        Task task = taskService.createTaskQuery()
//                .processDefinitionKey(key)
//                .taskAssignee("zhangsan")
//                .singleResult();
//        Task task = taskService.createTaskQuery()
//                .processDefinitionKey(key)
//                .taskAssignee("lisi")
//                .singleResult();
//        Task task = taskService.createTaskQuery()
//                .processDefinitionKey(key)
//                .taskAssignee("zhaoliu")
//                .singleResult();
        Task task = taskService.createTaskQuery()
                .processDefinitionKey(key)
                .taskAssignee("zhaoliu")
                .singleResult();
        //4.判断task !=null 说明当前有任务
        if(task != null){
            taskService.complete(task.getId());
            System.out.println("任务执行完成");
        }
        ret.put("status", true);
        return ret;
    }

10.图片中文乱码解决

  1. 打开 IDEA安装路径,找到如下的安装目录

springBoot Activiti6 初始化表_xml_05

  1. 根据自己所安装的版本来决定,我使用的是 64 位的 idea,所以在 idea64.exe.vmoptions 文件的最后
    一行追加一条命令: -Dfile.encoding=UTF-8

springBoot Activiti6 初始化表_spring_06

  1. 一定注意,不要有空格,否则重启 IDEA 时会打不开,然后 重启 IDEA,把原来的 png 图片删掉,
    再重新生成,即可解决乱码问题