Camunda development record of workflow -- common API s and processes

Posted by sparks on Fri, 18 Feb 2022 14:09:32 +0100

What is it

        A workflow engine is Activiti I don't care much about the introduction of many other places

Do what?

       What I recorded this time is that only one shell of the workflow engine is used for secondary packaging development 
       The specific process is:
       1,Pass on his own Json Structure. Of course, you need to add back-end customization when necessary
       2,The backend resolves to Camunda Recognizable Json structure
       3,Need to pass in parsing camunda User tasks and service tasks call their own business logic
       4,Publish process definition
       5,Start process instance

Daily API usage and noun introduction

preparation
     1,First of all, I use Springboot+Camunda It can be used in the main class with annotations at startup + @EnableProcessApplication
     Of course, we can't rely less. What I use is Gradle Development dependency is the latest version
     compile group: 'org.camunda.bpm.springboot', name: 'camunda-bpm-spring-boot-starter-webapp', version: '7.14.0'
     use maven Students can find it by themselves. Ha, it's not difficult
     2,If you want to test the backend yourself, you can download an official drawing plug-in Camunda-modeler 
     Address is:
      https://github.com/camunda/camunda-modeler/releases
     3,The drawing plug-in is very simple to use. Just look at the simple drawing
API introduction

#######Process definition Publishing

       1,The above preparations are finished. Start up boot The project is slow for the first time and will automatically generate 47 tables, which I hardly care about, because I use my own business process, just its shell
       2,Let's get a simple one first XML structure:


3. There are several forms of xml that can be used here
I'll just say two
a. File format the file must be in bpmn20 End XML
b. There are not too many string forms. It is required that the name must be the same name in bpmn
After we get the xml structure, we can publish it. The objects we get after publishing are called process definition, which is very important

// If we have annotated the main class before, we can directly inject the RepositoryService object through Spring to use all services related to camunda
// String publishing s1 is an xml format string
Deployment deploy = repositoryService.createDeployment().addString("demo1.bpmn20.xml", s1).deploy();

// File publishing, where param is the path information of the file
Deployment deploy = repositoryService.createDeployment().addInputStream("bpmnProcessId",new FileInputStream(param)).deploy();

Through the above two methods, we can publish successfully, and there will be a prompt if the publishing fails
In the process of my use, I summarized some mistakes I will encounter
1. The id in the bpmn file must start with a letter, not a number
2. In case of file publishing, the file name must be bpmn20 XML. Otherwise, the process definition will not be generated even if it is published successfully. An error will be reported when starting the instance, saying that the definition cannot be found
3. End is necessary but not necessary. Without end, I have experienced the situation that I can't jump out of starting an infinite loop, and I can't find the end

Here we also need to get a post release id

ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery().deploymentId(deploy.getId());
        List<ProcessDefinition> list = processDefinitionQuery.list();
        ProcessDefinition processDefinition = processDefinitionQuery.singleResult();
        System.out.println("=========1" + processDefinition.getId());
        String name = deploy.getName();

In this way, you can get the process definition id. if no error is reported here, the release is basically successful

Start process instance
Map<String, Object> params = new HashMap<>();
        DemoParam demoParam1 = new DemoParam();
        DemoParam demoParam2 = new DemoParam();
        DemoParam demoParam3 = new DemoParam();
        params.put("s1", demoParam1);
        params.put("s2", demoParam2);
        params.put("s3", 2);
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(param, params);
        String id = processInstance.getId();

The startup process can be added with global parameters. Please see my introduction to service tasks for specific parameters.
If there is no error in this step, it will start successfully and get the instance id
The so-called process instance is the thing after the process definition is started. Note that one definition can publish multiple instances
Here we need to mention the task type. The so-called task is a square. The type of tasks that can be executed. The service task is automatically executed, and the user task needs to be approved for execution. Therefore, if the user task is aborted and needs approval, it will continue to be executed

Approval method

Map<String, Object> params = new HashMap<>();
        params.put("x", 1);
        List<Task> list = taskService.createTaskQuery().processInstanceId(param).active().list();
        list.forEach(s-> {
            taskService.complete(s.getId());
        });

Of course, you can continue to add parameters to global variables during the approval process

Global parameters are very important, which is related to how to add parameters and use parameters in our future business processes. Because the workflow is executed automatically, the parameters are set before starting the task process

So far, a basic process is successfully executed
As for specific businesses such as query history, you can query the API

Let's briefly introduce other nouns
Task node: includes user task, service task, sub task, etc
Sequence flow: Line
Gateway: that is, the forked gateway itself has no business significance. It is only used as an identification of condition judgment. Condition expressions can be added to the sequence flow behind the gateway as the basis for judgment

Topics: Java camunda