Create an Activiti project

This step, based on SpringBoot and mysql, will create many Activiti tables in mysql. These tables are activiti’s way of controlling the flow. In fact, if we use Activiti to do a business on our own, It also creates various tables for the process over time, and Activiti provides a unified summary and formatting

If you want to understand the concept of the Activiti project you can just look down at what is Activiti?

1. Import maven coordinates

Create a SpringBoot project and import the coordinates

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring-boot-starter-basic</artifactId>
            <version>6.0.0</version>
        </dependency>
        <! - mybatis - plus depend on -- -- >
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1 track</version>
        </dependency>
        <! - mysql driver - >
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.22</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
Copy the code

2. Configuration file

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url:  jdbc:mysql:///activiti? useUnicode=true&nullCatalogMeansCurrent=true&serverTimezone=GMT%2B8&characterEncoding=utf-8
    username: root
    password: 123456
	# jdbC-URL: error if jDBC-URL is not used
    jdbc-url: jdbc:mysql:///activiti? useUnicode=true&nullCatalogMeansCurrent=true&serverTimezone=GMT%2B8&characterEncoding=utf-8
     Check automatically created tables at each startup
  activiti:
    history-level: full
    check-process-definitions: false
Copy the code

3. Modify the comments on the startup class

@SpringBootApplication(exclude = {LiquibaseAutoConfiguration.class, org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class, SecurityAutoConfiguration.class})
@EnableTransactionManagement
public class DemoApplication {

    public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}Copy the code

4. Add the configuration file registration bean

@Configuration
public class ActivitiConfig extends AbstractProcessEngineAutoConfiguration {

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource activitiDataSource(a) {
        return DataSourceBuilder.create().build();
    }

    public SpringProcessEngineConfiguration springProcessEngineConfiguration(PlatformTransactionManager transactionManager, SpringAsyncExecutor executor) throws IOException {

        returnbaseSpringProcessEngineConfiguration(activitiDataSource(),transactionManager,executor); }}Copy the code

5. Start the project

A total of 28 tables are automatically created:

  • ACT_GE generic class
  • ACT_HI history class
  • ACT_ID User information class
  • ACT_RE process instance class
  • ACT_RU runtime class

6. Install the ActiBPM plug-in

Prevent tool Chinese garbled characters

Last line add: -dfile. encoding=UTF-8

7. The eclipse plugin

Because the ActiBPM plug-in hasn’t been updated for a long time, too high a version of IDEA can cause all sorts of weird errors, so you can use the Eclipse plug-in for convenienceSpecific installation tutorials can be baidu

About the drawing

What is Activiti?

What is Activiti? What is Activiti? What is Activiti? This problem, do not make too much expansion, god can skip.

At the beginning of learning Activiti, Baidu read a lot of articles (each article index will be given at the end), the core idea is that Activiti is a tool to manage workflow, or a workflow engine, in popular terms, it is the Activiti engine. We just need to follow its existing configuration to carry out the corresponding existing business. It automatically helps us solve previously uncontrollable process problems.

However, I do not know whether you are as confused as me after watching these. Now I have built a set of demo of leave process, which helps me learn and understand some concepts of Activiti. I want to understand Activiti from the actual project.

Let me summarize what is required to complete the Activiti process project:

Process definition -> Start the process instance -> each user node completes its task in the process sequence -> End the process

The following code is not recommended; it is intended to be easy to understand

1. Process definition

First set a small goal, do a leave process demo, drawing:Copy the code

This is a very simple flow chart, in simple terms is the start of the process -> students apply for leave -> teacher approval leave -> end of the process. So what's the use of drawing this picture? This flow chart is actually a description of the function you want to achieve in the whole project, that is, the flow definition. It can be considered that we have a flow of the project, and our program can create multiple flow instances according to this flow definition.Copy the code

In the actual code, the process definition can be done using the API provided by Activiti and the code file we drew:

    @Autowired
 	  private ProcessEngine processEngine;
  
  
      // Services associated with process definition and deployment objects
      Deployment deployment = processEngine.getRepositoryService()// Get the repository
          .createDeployment()// Create a deployment object
          .name("Process Definition")// Add the deployment name
          .addClasspathResource("LeaveDemo.bpmn")// Only one file can be loaded at a time
          .addClasspathResource("LeaveDemo.png")// Only one file can be loaded at a time
          .deploy();// The deployment is complete

      System.out.println("Deployment ID:" + deployment.getId());
      System.out.println("Deployment Name:" + deployment.getName());
Copy the code

When this step is complete, we tell the application that the process is already in the process library, but it has not started executing it.

2. Start the process instance

In summary: A process definition can have multiple process instances, and because Activiti records each step in the database, the process instances, process definitions, and execution steps in Activiti will not disappear even if the application is not running as long as the database is not changed

Or, I prefer to think of Activiti as an entire Java program, where the process definition is a thread template, the process instance is a thread created from the thread template, and these thread templates and threads do not disappear even when the program is running

    /** * Start the process instance */
  @Test
  public void startProcessInstance(a){
      // the key defined by the process is used to start the process instance
      String processDefinitionKey = "LeaveDemo";
      //2. Services associated with the executing process instance and execution object
      / / startProcessInstanceByKey method can also set up other parameters, such as process variables.
      ProcessInstance pi = processEngine.getRuntimeService()
          .startProcessInstanceByKey(processDefinitionKey,variables);// Start the process instance with the key defined by the process. The key corresponds to the id attribute value in the helloWorld.bpmn file. Start the process instance with the key value
      System.out.println("Process instance ID:"+pi.getId());// Process instance ID
      System.out.println("Process definition ID:"+pi.getProcessDefinitionId());// Process definition ID
      // Process instance ID:62501
      // Process definition ID:LeaveDemo: 2:55,004
  }
Copy the code

At this point, our process has started to execute and the first step has been executed. The circle represents the start, and the application for leave is entered to wait for students to submit the application. Each square in the figure represents a task, and each task has its ID.

3. Finish the task

    /** * Completes the task for the corresponding ID */
  @Test
  public void completePersonalTask(a) {
      // Task ID, obtained from the previous query.
      String taskId = "57509";

      processEngine.getTaskService()// Services related to the task being executed
          .complete(taskId);
      System.out.println("Completed Task: Task ID:" + taskId);
  }
Copy the code

The above code is used to complete the corresponding task, take our distance, when students after completion of the task of the filing will enter the teacher approval task of waiting, when the teacher after the completion of the examination and approval, activiti found behind the left over, shows that tasks are completed, the end of the process instance, or can understand into finished thread execution. While threads are live data (stored in memory), activiti is dead data (stored in the database)

4. To summarize

This is the process that uses Activiti in its entirety. It is important to note that there is only one process definition per process definition, and the version of the process definition will be updated if the definition is repeated. Activiti’s process definitions are mostly for the design and implementation of a project’s overall flow, but the detailed calls need to be supplemented by the developers themselves.

5. About the API

    /**
   *  运行时相关表
   */
  @Autowired
  private RuntimeService runtimeService;

  /** * ACT_RU_ ACT_HI_ */
  @Autowired
  private TaskService taskService;

  /** * user info table */
  @Autowired
  private IdentityService identityService;

  /** * generic class table */
  @Autowired
  private RepositoryService repositoryService;

  /** * the facade interface, which can be used to create all services */
  @Autowired
  private ProcessEngine processEngine;

  /** ** */
  @Autowired
  private HistoryService historyService;
Copy the code

If you want to see the specific use of API, may refer to: zhuanlan.zhihu.com/p/77289502

To learn more about each service API, see juejin.cn/post/684490…

Reference article:

Juejin. Cn/post / 684490…

Juejin. Cn/post / 684490…

Juejin. Cn/post / 684490…

www.cnblogs.com/SIHAIloveYA…