1. Build the project structure

  1. To use IDEA, you need to install the actiBPM plug-in to draw flowcharts
  2. Create an empty Java project and add a folder like this:

    Lib: Store the jar package you need to use. You can download it from the link below and decompress it. Remember to add the package to the project.Jar package extraction code: C0zq

    Resource: Stores resource files, such as BPMN flow files

  3. Create a new process first.bpmn
  4. Add the log4j.properties file to configure logging
log4j.rootLogger=INFO, stdout

# Console Appender
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern= %d{hh:mm:ss,SSS} [%t] %-5p %c %x - %m%n

# Custom tweaks
log4j.logger.com.codahale.metrics=WARN
log4j.logger.com.ryantenney=WARN
log4j.logger.com.zaxxer=WARN
log4j.logger.org.apache=WARN
log4j.logger.org.hibernate=WARN
log4j.logger.org.hibernate.engine.internal=WARN
log4j.logger.org.hibernate.validator=WARN
log4j.logger.org.springframework=WARN
log4j.logger.org.springframework.web=WARN
log4j.logger.org.springframework.security=WARN
Copy the code
  1. The imported activiti configuration file activiti.cfg. XML can be copied from the official website: activiti.cfg. XML modified as follows:
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">

	    <! Jar = mysql-connector-java-8.0.16.jar = mysql-connector-java-8.0.16.jar = mysql-connector-java-8.0.16.jar = mysql-connector-java-8.0.16.jar = mysql-connector-java-8.0.16.jar
        <! --<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti? serverTimezone=UTC" />-->
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti" />
        <property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUsername" value="root" />
        <property name="jdbcPassword" value="123456" />

        <property name="databaseSchemaUpdate" value="true" />

        <! -->
<! -- <property name="asyncExecutorActivate" value="false" />-->

<! -- <property name="mailServerHost" value="mail.my-corp.com" />-->
<! -- <property name="mailServerPort" value="5025" />-->
    </bean>

</beans>
Copy the code
  1. Create the corresponding database

Second, the coding

  1. Create a class ActivitiTest in the corresponding directory SRC, coded as follows
package com.xjf.c3;

/* Import the Activiti6.0 package */
import org.activiti.engine.*;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

/ * * *@Auther: XuJiaFei
 * @Date: 2019/11/8 probably *@Description: * /
public class ActivitiTest {

    public static void main(String[] args) {
        // Get the process engine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        // Storage service
        RepositoryService repositoryService = processEngine.getRepositoryService();
        // Run the service
        RuntimeService runtimeService = processEngine.getRuntimeService();
        // Task service
        TaskService taskService = processEngine.getTaskService();

        // Release process
        repositoryService.createDeployment().addClasspathResource("first.bpmn").deploy();

        // Create a leave process to get an instance of the process. The process ID here is the PROCESS ID of first.bpmn
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("myProcess_1");

        // Employees complete the task of asking for leave
        Task task = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult();
        System.out.println("Current process node:" + task.getName());
        taskService.complete(task.getId());

        // The manager completes the approval task
        task = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult();
        System.out.println("Current process node:" + task.getName());
        taskService.complete(task.getId());

        // The task will be null when the process is finished
        task = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult();
        System.out.println("Process execution is complete:" + task);

        // Stop the engine
        processEngine.close();
        / / exit
        System.exit(0); }}Copy the code

Note:

  1. RuntimeService. StartProcessInstanceByKey (” myProcess_1 “), the corresponding key, is the first in the BPMN, in the heart of the idea first. BPMN renamed first. The XML, can see the corresponding XML

    Be sure to remove XMLNS =” “from process, otherwise it will fail to start

  2. After running, the database will generate 28 tables

Third, the results