springboot整合Camunda实现业务

1.bean实现 业务

1.画流程图

系统任务,实现方式

2.定义bean

java 复制代码
package com.jmj.camunda7test.process.config;

import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.camunda.bpm.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.awt.*;
import java.net.URI;
import java.util.Map;

@Configuration
@Slf4j
public class ProcessConfiguration {

    @Autowired
    TaskService taskService;



    @Bean("baidu")
    public JavaDelegate baidu() {
        return execution -> {
            String processDefinitionId = execution.getProcessDefinitionId();
            log.info("processDefinitionId:{}", processDefinitionId);
            Map<String, Object> variables = execution.getVariables();
            log.info("approved:{}", variables.get("approved"));
            try {
                System.out.println("来访问百度了");
            } catch (Exception e) {
                e.printStackTrace();
            }
        };
    }


    @Bean("hao123")
    public JavaDelegate hao123() {
        return execution -> {
            String processDefinitionId = execution.getProcessDefinitionId();
            log.info("processDefinitionId:{}", processDefinitionId);
            Map<String, Object> variables = execution.getVariables();
            log.info("approved:{}", variables.get("approved"));
            try {
                System.out.println("来访问hao123了");
            } catch (Exception e) {
                e.printStackTrace();
            }

        };
    }


}

3.会对应Bean去执行

java 复制代码
package com.jmj.camunda7test.controller;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.repository.Deployment;
import org.camunda.bpm.engine.repository.ProcessDefinition;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.task.Task;
import org.camunda.bpm.engine.task.TaskQuery;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
class ResServiceTest {
    @Autowired
    private ResService resService;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;


    @Test
    //部署
    void deploy() {
        Deployment deploy = repositoryService.createDeployment().name("测试bean")
                .addClasspathResource("bpmn/process.bpmn")//绑定需要部署的流程文件
                .enableDuplicateFiltering(true)
                .deploy();
        System.out.println(deploy.getId() + ":" + deploy.getName());

    }

    @Test
    //运行
    void run() {

        ProcessInstance processInstance = runtimeService.startProcessInstanceById("my-project-process:5:21b5c0c8-399e-11ef-b3eb-005056c00008");
        String processDefinitionId = processInstance.getProcessDefinitionId();
        String businessKey = processInstance.getBusinessKey();
        String processInstanceId = processInstance.getProcessInstanceId();
        System.out.println(processDefinitionId);
        System.out.println(businessKey);
        System.out.println(processInstanceId);

    }

    public static final String Approved = "approved";

    @Test
    //运行
    void run1() {
        Map<String, Object> map = new HashMap<>();
        map.put(Approved, true);
        map.put("user", new DataB("testId","TestName"));
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("my-project-process", map);
        System.out.println("流程启动成功: 流程实例ID=" + processInstance.getProcessInstanceId());
    }

    @Test
    //完成用户任务
    void compelete() {
        TaskQuery taskQuery = taskService.createTaskQuery().processDefinitionKey("my-project-process")
                .orderByProcessInstanceId()
                .desc()
                .active();
        String taskId = taskQuery.list().get(0).getId();
        Map<String, Object> map = new HashMap<>();

        map.put("user", "admin");

        taskService.complete(taskId, map);

    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    //参数
    static class DataB implements Serializable {
        private String id;
        private String name;
    }

    @Test
    //完成用户任务 
    void compelete1() {


        Task task = taskService.createTaskQuery().taskAssignee("admin").list().get(0);
        Map<String, Object> variables = taskService.getVariables(task.getId());
        variables.forEach((k, v) -> {
            System.out.println(k + ":" + v);
        });
        taskService.setVariable(task.getId(), Approved, false);
        taskService.complete(task.getId());


    }

    @Test
    //级联删除所有部署
    void deleteDeploy() {
        for (ProcessDefinition processDefinition : repositoryService.createProcessDefinitionQuery().list()) {
            System.out.println(processDefinition.getId() + ":" + processDefinition.getName());
        }
        repositoryService.createDeploymentQuery().list().forEach(deployment -> {
            repositoryService.deleteDeployment(deployment.getId(), true);

        });
    }

    @Test
    //删除部署
    void delete() {
        repositoryService.deleteDeployment("86cf1536-39a5-11ef-ba9b-005056c00008", true);
    }
}

2.JavaClass:

java 复制代码
package com.jmj.camunda7test.process.config;

import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.camunda.bpm.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class Hao123Delegate implements JavaDelegate {
    @Autowired
    private RepositoryService repositoryService;
    @Override
    public void execute(DelegateExecution execution) throws Exception {
        List<Deployment> list = repositoryService.createDeploymentQuery().list();
        for (Deployment deployment : list) {
            System.out.println(deployment.getId()+":"+deployment.getName());
        }
        log.info("进入Java;类执行 hao123");
    }
}

3.Expression

下面两种可使用spring的配置

EL表达式,调用java类的方法 ,规范:

expression="#{monitorExecution.execution(execution)}"

直接调用容器中对象的方法

java 复制代码
package com.jmj.camunda7test.getStarted.chargecard;

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.springframework.stereotype.Component;

@Component("testA")
public class TestA {
    public void add(DelegateExecution delegateExecution) {
        String processDefinitionId = delegateExecution.getProcessDefinitionId();
        System.out.println("testA:"+processDefinitionId);
    }
}

4.创建用户 组

java 复制代码
package com.jmj.camunda7test.controller;

import org.camunda.bpm.engine.IdentityService;
import org.camunda.bpm.engine.identity.Group;
import org.camunda.bpm.engine.identity.GroupQuery;
import org.camunda.bpm.engine.identity.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class IdentityActivity {

    @Autowired
    IdentityService identityService;

    @Test
    void createUser() {
        List<User> list = identityService.createUserQuery().list();
        for (User user : list) {
            System.out.println(user.getId());
            System.out.println(user.getFirstName());
            System.out.println(user.getEmail());
            System.out.println(user.getPassword());
            System.out.println(user.getLastName());
        }
    }

    @Test
    void selectUserGroup() {
        List<Group> list = identityService.createGroupQuery().groupId("camunda-admin").list();
        System.out.println(list.get(0).getName());
    }

    @Test
    void getCurrentAuth() {
        identityService.setAuthenticatedUserId("admin");
        String userId = identityService.getCurrentAuthentication().getUserId();
        System.out.println(userId);
    }

    @Test
    void createUserID() {
        User jmj = identityService.newUser("jmj");
        jmj.setFirstName("mingji");
        jmj.setLastName("jiang");
        jmj.setPassword("123456");
        jmj.setEmail("123@qq");
        identityService.saveUser(jmj);
    }

    @Test
    void bindGroup() {
        identityService.createMembership("jmj","camunda-admin");



    }
}

官网Spring Boot Version Compatibility | docs.camunda.org

5.获取候选人用户

候选人或者主要审批人或者候选组的所有用户都会有审批的权限,谁审批了,就过去了

java 复制代码
   @Test
    //完成用户任务
    void compelete1() {

        identityService.setAuthenticatedUserId("jmj");

        Authentication currentAuthentication = identityService.getCurrentAuthentication();
        List<String> groupIds = currentAuthentication.getGroupIds();
        List<String> tenantIds = currentAuthentication.getTenantIds();
        System.out.println(groupIds);
        System.out.println(tenantIds);

        String userId = currentAuthentication.getUserId();
        System.out.println(userId);


        Task task = taskService.createTaskQuery().taskAssignee("admin").list().get(0);

        List<IdentityLink> identityLinksForTask = taskService.getIdentityLinksForTask(task.getId());
        for (IdentityLink identityLink : identityLinksForTask) {
            String type = identityLink.getType();
            System.out.println(type+":"+identityLink.getUserId());
        }
        Map<String, Object> variables = taskService.getVariables(task.getId());
        variables.forEach((k, v) -> {
            System.out.println(k + ":" + v);
        });
//        taskService.setVariable(task.getId(), Approved, false);
//        taskService.complete(task.getId());

        taskService.complete(task.getId());
    }

6.监听器

java 复制代码
package com.jmj.camunda7test.process.config;

import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.ExecutionListener;
import org.camunda.bpm.engine.delegate.TaskListener;
import org.camunda.bpm.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;

@Configuration
public class EvenListenerConfig {

   public String inject;

    @Autowired
    private TaskService taskService;
    @Bean("start")
    public ExecutionListener start() {
        return execution -> {
            String processInstanceId = execution.getProcessInstanceId();
            System.out.println(processInstanceId+"开始执行了");
        };
    }

    @Bean("end")
    public ExecutionListener end() {
        return execution -> {
            String processInstanceId = execution.getProcessInstanceId();
            System.out.println(processInstanceId+"执行结束了");
        };
    }

}

Camunda工作流集成SpringBoot(三)_camunda springboot 监听-CSDN博客 可以看看这个

相关推荐
郑祎亦21 分钟前
Spring Boot 项目 myblog 整理
spring boot·后端·java-ee·maven·mybatis
不是二师兄的八戒21 分钟前
本地 PHP 和 Java 开发环境 Docker 化与配置开机自启
java·docker·php
爱编程的小生33 分钟前
Easyexcel(2-文件读取)
java·excel
带多刺的玫瑰1 小时前
Leecode刷题C语言之统计不是特殊数字的数字数量
java·c语言·算法
请叫我雷轰1 小时前
百度主动推送可以提升抓取,它能提升索引量吗?
百度·dubbo
计算机毕设指导62 小时前
基于 SpringBoot 的作业管理系统【附源码】
java·vue.js·spring boot·后端·mysql·spring·intellij-idea
Gu Gu Study2 小时前
枚举与lambda表达式,枚举实现单例模式为什么是安全的,lambda表达式与函数式接口的小九九~
java·开发语言
Chris _data2 小时前
二叉树oj题解析
java·数据结构
牙牙7052 小时前
Centos7安装Jenkins脚本一键部署
java·servlet·jenkins
paopaokaka_luck2 小时前
[371]基于springboot的高校实习管理系统
java·spring boot·后端