Activiti7 + SpringBoot 2.0 - basic use of new features (Integrated Development)

0. New features of activiti7 on the official website

Because activiti7 has fewer Chinese documents, you can consider reading English documents. There are also examples, which are quite detailed.

GitHub document Click to enter

This article shares the basic operation of using activiti7 new api in the whole process

1. Test engineering construction

1.1 pom file

Here are the coordinates for testing only

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.0.RELEASE</version>
    <relativePath/>
</parent>

<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.activiti/activiti-spring-boot-starter -->
    <dependency>
        <groupId>org.activiti</groupId>
        <artifactId>activiti-spring-boot-starter</artifactId>
        <version>7.0.0.Beta2</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

1.2 application.yml file

Some profiles are interpreted from the Internet. Not in person.

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/activiti?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
  activiti:
    database-schema-update: true
    #Indicates that the history table is used. If it is not configured, the database can be checked after the project is started. Only 17 tables have been established, but the history table has not been established
    db-history-used: true
    #The history level can be configured as none, activity, audit, full
    history-level: full
    #Verify the process file. By default, verify the process file in the processes folder under resources
    #If false is set, the bpmn file under resources/processes will not be automatically deployed
    check-process-definitions: true

1.3 preparation of initialization documents

1.4 bpmn file

1.5 special attention

  • Placing in this directory automatically deploys the process. The official documents are shown in the second picture

2. Test code

  • Empty test code
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class TextActiviti {

    @Autowired
    private SecurityUtil securityUtil;

    @Autowired
    private ProcessRuntime processRuntime;

    @Autowired
    private TaskRuntime taskRuntime;
}

2.2 query process definition information

/**
* Query process definition
*/
@Test
public void testQueryProcessDefinition() {

   //1. Simulate login, because activiti7 is strongly coupled with spring security
   securityUtil.logInAs("system");

   //2. query
   Page<ProcessDefinition> processDefinitionPage = processRuntime.processDefinitions(Pageable.of(0, 10));
   for (ProcessDefinition processDefinition : processDefinitionPage.getContent()) {

       System.out.println(processDefinition.getName());
       System.out.println(processDefinition.getKey());
   }
}

2.3 start a process

Code, official document I refer to

/**
 * Start a process instance
 */
@Test
public void testStartProcess() {

    //1. Simulate login, because activiti7 is strongly coupled with spring security
    //  Parameters are available in the DemoApplicationConfiguration.java file. Because this is the test. There is no actual combination of spring security
    securityUtil.logInAs("erdemedeiros");

    ProcessInstance processInstance = processRuntime
            .start(ProcessPayloadBuilder
                    .start()
                    .withProcessDefinitionKey("testProcess")
                    // . withVariable("content", content) can set process variables
                    .build());

    //Output basic information
    System.out.println(processInstance.getId());
    System.out.println(processInstance.getName());
}

2.4 querying and picking up tasks

Code, official document I refer to

/**
* Test queries and pick up tasks
*/
@Test
public void testQueryAndClaimTask() {

   //1. Simulate login, because activiti7 is strongly coupled with spring security
   //  Parameters are available in the DemoApplicationConfiguration.java file. Because this is the test. There is no actual combination of spring security
   securityUtil.logInAs("salaboy");

   Page<Task> tasks = taskRuntime.tasks(Pageable.of(0, 10));
   List<Task> tasksContent = tasks.getContent();

   for (Task task : tasksContent) {

       // Print task basic information
       System.out.println(task.getName());
       System.out.println(task.getAssignee());
       //Pick tasks.
       taskRuntime.claim(TaskPayloadBuilder.claim().withTaskId(task.getId()).build());
       System.out.println("Successfully picked up!");
   }

}
  • At this time, you can watch that the assignee field in the database table act Ru task has been updated. When other users in this user group are querying, this task will not be queried by others. There's no test here.

2.5 return task

/**
*Test return task
*/
@Test
public void testReleaseTask() {

   //1. Simulate login, because activiti7 is strongly coupled with spring security
   //  Parameters are available in the DemoApplicationConfiguration.java file. Because this is the test. There is no actual combination of spring security
   securityUtil.logInAs("salaboy");

   Page<Task> tasks = taskRuntime.tasks(Pageable.of(0, 10));
   List<Task> tasksContent = tasks.getContent();

   for (Task task : tasksContent) {

       // Picked by "salaboy"
       if ("salaboy".equals(task.getAssignee())) {
           // Print task basic information
           System.out.println(task.getName());
           System.out.println(task.getAssignee());
           //Return task
           taskRuntime.release(TaskPayloadBuilder.release().withTaskId(task.getId()).build());
       }
   }
}
  • At this time, you can watch that the assignee field in the database table act Ru task is null. When other users in this user group are querying, this task can be queried by other users in the user group, and there is no test here.

2.6 set global / local process variables by task Id

/**
* Set process variables through taskId
*  Note is the same as when not integrated with springboot
*/
@Test
public void testSetVar() {

   //1. Simulate login, because activiti7 is strongly coupled with spring security
   //  Parameters are available in the DemoApplicationConfiguration.java file. Because this is the test. There is no actual combination of spring security
   securityUtil.logInAs("erdemedeiros");

   String taskId = "78d0ee10-47f1-11ea-b90b-005056c00008";

   Map<String, Object> varMap = new HashMap<>();
   varMap.put("name", "immort");
   varMap.put("age", 18);
   // If the local variable is set: add. localOnly()
   taskRuntime.setVariables(TaskPayloadBuilder.setVariables().withTaskId(taskId).withVariables(varMap).build());
}
  • You can see that it has been set in the database table.

2.7 obtaining process variables through task id

/**
* Test get process variables
*  Considerations are the same as when not integrating with springBoot
*/
@Test
public void testGetVar() {
   //1. Simulate login, because activiti7 is strongly coupled with spring security
   //  Parameters are available in the DemoApplicationConfiguration.java file. Because this is the test. There is no actual combination of spring security
   securityUtil.logInAs("erdemedeiros");

   String taskId = "78d0ee10-47f1-11ea-b90b-005056c00008";
   
   //If you want to get local variables, add. localOnly()
   List<VariableInstance> variables = taskRuntime.variables(TaskPayloadBuilder.variables().withTaskId(taskId).build());
   for (VariableInstance variable : variables) {
       System.out.println(variable.getName() + " : " + variable.getValue());
   }
}

2.8 complete the task

/**
 *Test completion task
 */
@Test
public void testCompleteTask() {
    //1. Simulate login, because activiti7 is strongly coupled with spring security
    //  Parameters are available in the DemoApplicationConfiguration.java file. Because this is the test. There is no actual combination of spring security
    securityUtil.logInAs("erdemedeiros");

    Page<Task> tasks = taskRuntime.tasks(Pageable.of(0, 10));
    List<Task> tasksContent = tasks.getContent();

    for (Task task : tasksContent) {
        // Print task basic information
        System.out.println(task.getName());
        System.out.println(task.getAssignee());
        //Pick tasks.
        taskRuntime.claim(TaskPayloadBuilder.claim().withTaskId(task.getId()).build());
        System.out.println("Successfully picked up!");
        //Finish the task
        taskRuntime.complete(TaskPayloadBuilder.complete().withTaskId(task.getId()).build());
        System.out.println("Finish the task!");
    }
}

3. summary

The above are just some basic operations that I can see from the document. Because for the time being, activiti7 has fewer Chinese documents. So I can only read English. If there is anything wrong, please point out. Study together, thank you.

Published 5 original articles, praised 0, visited 75
Private letter follow

Tags: Spring Java Database MySQL

Posted on Wed, 05 Feb 2020 05:32:19 -0500 by AlexRodr