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博客 可以看看这个

相关推荐
shaoing1 分钟前
MySQL 错误 报错:Table ‘performance_schema.session_variables’ Doesn’t Exist
java·开发语言·数据库
腥臭腐朽的日子熠熠生辉1 小时前
解决maven失效问题(现象:maven中只有jdk的工具包,没有springboot的包)
java·spring boot·maven
ejinxian1 小时前
Spring AI Alibaba 快速开发生成式 Java AI 应用
java·人工智能·spring
杉之1 小时前
SpringBlade 数据库字段的自动填充
java·笔记·学习·spring·tomcat
圈圈编码1 小时前
Spring Task 定时任务
java·前端·spring
俏布斯1 小时前
算法日常记录
java·算法·leetcode
27669582922 小时前
美团民宿 mtgsig 小程序 mtgsig1.2 分析
java·python·小程序·美团·mtgsig·mtgsig1.2·美团民宿
爱的叹息2 小时前
Java 连接 Redis 的驱动(Jedis、Lettuce、Redisson、Spring Data Redis)分类及对比
java·redis·spring
程序猿chen2 小时前
《JVM考古现场(十五):熵火燎原——从量子递归到热寂晶壁的代码涅槃》
java·jvm·git·后端·java-ee·区块链·量子计算
松韬2 小时前
Spring + Redisson:从 0 到 1 搭建高可用分布式缓存系统
java·redis·分布式·spring·缓存