Camunda实战:camunda-springboot整合 部署流程,发起流程

上一篇我们讲了:Camunda 实战练习:Demo搭建(四)
这一篇我们再 上一篇的基础上来说一说如何使用代码去部署一个流程,然后发布一个流程,之后再去研究其他的功能,慢慢的就会进入camunda的开发之旅。

在这里插入图片描述
这是我以前写的一个demo,这里贴出来,我写了一个 DemoController
下面直接贴出代码:

package com.example.workflow.demo;

import net.minidev.json.JSONArray;
import org.camunda.bpm.engine.IdentityService;
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.runtime.ProcessInstance;
import org.camunda.bpm.engine.task.Task;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 
 * @Desc
 * @Date 2022-04-27 22:01
 **/
@RestController
@RequestMapping("/demo")
public class DemoController {

    @Resource
    private RepositoryService repositoryService;

    @Resource
    private RuntimeService runtimeService;

    @Resource
    private IdentityService identityService;

    @Resource
    private TaskService taskService;


    @GetMapping("/deploy")
    public String deploy(){
        System.out.println("deploy-start");
        Deployment deployment = repositoryService.createDeployment()
                .name("价格审批2")
                .addClasspathResource("价格审批2.bpmn")
                .deploy();
        System.out.println("流程id:"+deployment.getId());
        System.out.println("流程name:"+deployment.getName());
        return "deploy-start";
    }

    @GetMapping("/deploy2")
    public String deploy2(){
        System.out.println("deploy-start");
        Deployment deployment = repositoryService.createDeployment()
                .name("价格审批2")
                .addClasspathResource("价格审批2.bpmn")
                .deploy();
        System.out.println("流程id:"+deployment.getId());
        System.out.println("流程name:"+deployment.getName());
        return "deploy-start";
    }

    @GetMapping("/getDeploy")
    public String getDeploy(@RequestParam(value = "deploymentId") String deploymentId){
        System.out.println("deploymentId:"+deploymentId);
        Deployment deployment = repositoryService.createDeploymentQuery().deploymentId(deploymentId).singleResult();
        System.out.println("deployment:"+deployment);
        return "getDeploy end";
    }


    @GetMapping("/start")
    public String startInstance(){
        System.out.println("startInstance-start");
        Map<String, Object> map = new HashMap<String, Object>(3);
//        map.put("tony", "tony");
//        map.put("joe", "joe");
//        map.put("book", "book");
        map.put("sale", "zhangsan");
        identityService.setAuthenticatedUserId("tony");
        String processDefinitionKey = "Process_0cfza8o";
        String businessKey = "价格审批2";
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey, map);

        System.out.println("processInstance:"+processInstance);
        return "startInstance-end";
    }

    @GetMapping("/todo")
    public String queryMyToDo(@RequestParam(value = "userId") String userId){
        System.out.println("startInstance-start");
        List<Task> taskList = new ArrayList<>();
        try {
            taskList = taskService.createTaskQuery().taskAssignee(userId).list();
            taskList.forEach(task-> System.out.println(task.toString()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String json = JSONArray.toJSONString(taskList);
        return json;
    }

    @GetMapping("/taskPass")
    public String taskPass(@RequestParam(value = "taskId") String taskId){
        String id = "17546712-c63b-11ec-a6c5-327a20e038c0";
        taskService.complete(taskId);
        return "审批通过";
    }



}

其中有一个 deploy 方法,是将 resource 下面画好的一个 .bpmn文件(流程图)部署到 camunda引擎中,部署成功之后,在这个Cockpit 就可以看出:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
这个DemoController 里面的代码都是最简单的一些代码功能,学习这些可以让我们初步认识camunda,之后再慢慢研究别的功能。加油。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐