3四、Spring Cloud 2.x之整合工做流Activiti

工做流在項目中很是經常使用,這裏先來看兩張圖:html

第一張:
3四、Spring Cloud 2.x之整合工做流Activiti
第二張:
3四、Spring Cloud 2.x之整合工做流Activiti
對以上兩張圖進行說明:java

假設這兩張圖就是華誼兄弟的請假流程圖mysql

圖的組成部分: web

人物:范冰冰、馮小剛、王中軍spring

事件(動做):請假、批准、不批准sql

3四、Spring Cloud 2.x之整合工做流Activiti

工做流(Workflow):就是「業務過程的部分或總體在計算機應用環境下的自動化」,它主要解決的是「使在多個參與者之間按照某種預約義的規則傳遞文檔、信息或任務的過程自動進行,從而實現某個預期的業務目標,或者促使此目標的實現」。目前市面上經常使用的工做流引擎包括jbpm、activiti和OSWorkflowOSWorkflow。數據庫

一、新建項目sc-activiti,對應的pom.xml文件以下express

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>

        <groupId>com.sc</groupId>
        <artifactId>sc-activiti</artifactId>
        <version>0.0.1-SNAPSHOT</version>

        <name>sc-activiti-drools</name>
        <!-- FIXME change it to the project's website -->
        <url>http://www.example.com</url>

        <properties>
                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
        </properties>

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

        <dependencyManagement>
                <dependencies>
                        <dependency>
                                <groupId>org.springframework.cloud</groupId>
                                <artifactId>spring-cloud-dependencies</artifactId>
                                <version>Finchley.RELEASE</version>
                                <type>pom</type>
                                <scope>import</scope>
                        </dependency>

                </dependencies>
        </dependencyManagement>

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

                <dependency>
                        <groupId>org.activiti</groupId>
                        <artifactId>activiti-spring-boot-starter-basic</artifactId>
                        <version>5.22.0</version>
                </dependency>

                <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-data-jpa</artifactId>
                </dependency>

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

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

                <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>

        </dependencies>

        <build>
                <pluginManagement><!-- lock down plugins versions to avoid using Maven 
                                defaults (may be moved to parent pom) -->
                        <plugins>
                                <plugin>
                                        <artifactId>maven-clean-plugin</artifactId>
                                        <version>3.0.0</version>
                                </plugin>
                                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                                <plugin>
                                        <artifactId>maven-resources-plugin</artifactId>
                                        <version>3.0.2</version>
                                </plugin>
                                <plugin>
                                        <artifactId>maven-compiler-plugin</artifactId>
                                        <version>3.7.0</version>
                                </plugin>
                                <plugin>
                                        <artifactId>maven-surefire-plugin</artifactId>
                                        <version>2.20.1</version>
                                </plugin>
                                <plugin>
                                        <artifactId>maven-jar-plugin</artifactId>
                                        <version>3.0.2</version>
                                </plugin>
                                <plugin>
                                        <artifactId>maven-install-plugin</artifactId>
                                        <version>2.5.2</version>
                                </plugin>
                                <plugin>
                                        <artifactId>maven-deploy-plugin</artifactId>
                                        <version>2.8.2</version>
                                </plugin>
                        </plugins>
                </pluginManagement>
        </build>
</project>

二、新建配置文件application.ymlapache

spring:
    datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/sc?characterEncoding=utf8&useSSL=true
        username: root
        password: root
    activiti:
        check-process-definitions: false
    jpa:
        properties:
            hibernate:
                hbm2ddl:
                    auto: update
    show-sql: true

server:
    port: 8081
    context-path: /
    session:
        timeout: 10
    tomcat:
        uri-encoding: UTF-8

三、新建請假流程文件holiday.bpmntomcat

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
    <process id="demo2" name="My process" isExecutable="true">
        <startEvent id="startevent1" name="Start"></startEvent>
        <userTask id="usertask1" name="請假"></userTask>
        <userTask id="usertask2" name="審批"></userTask>
        <endEvent id="endevent1" name="End"></endEvent>
        <sequenceFlow id="flow1" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>
        <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow>
        <sequenceFlow id="flow3" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
    </process>
    <bpmndi:BPMNDiagram id="BPMNDiagram_myProcess">
        <bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess">
            <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
                <omgdc:Bounds height="35.0" width="35.0" x="130.0" y="180.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
                <omgdc:Bounds height="55.0" width="105.0" x="271.0" y="170.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
                <omgdc:Bounds height="55.0" width="105.0" x="471.0" y="170.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
                <omgdc:Bounds height="35.0" width="35.0" x="690.0" y="180.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
                <omgdi:waypoint x="576.0" y="197.0"></omgdi:waypoint>
                <omgdi:waypoint x="690.0" y="197.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
                <omgdi:waypoint x="376.0" y="197.0"></omgdi:waypoint>
                <omgdi:waypoint x="471.0" y="197.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
                <omgdi:waypoint x="165.0" y="197.0"></omgdi:waypoint>
                <omgdi:waypoint x="271.0" y="197.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
        </bpmndi:BPMNPlane>
    </bpmndi:BPMNDiagram>
</definitions>

對應的bpmn文件的圖形以下
3四、Spring Cloud 2.x之整合工做流Activiti

四、新建springboot啓動類

package sc.ad;

import org.activiti.spring.boot.SecurityAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
public class ActivitiApp {
        public static void main(String[] args)
        {
                SpringApplication.run(ActivitiApp.class, args);
        }

}

備註:必定要忽略SecurityAutoConfiguration.class,不然會出現以下異常。

3四、Spring Cloud 2.x之整合工做流Activiti

五、新建一個controller,用來啓動請假流程

package sc.ad.controller;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ADController {

        @Autowired
        private RepositoryService repositoryService;

        @Autowired
        private RuntimeService runtimeService;

        @Autowired
        private TaskService taskService;

        @RequestMapping("/ad")
        public void firstDemo() {

                // 根據bpmn文件部署流程
                Deployment deployment = repositoryService.createDeployment()
                                .addClasspathResource("holiday.bpmn").deploy();
                // 獲取流程定義
                ProcessDefinition processDefinition = repositoryService
                                .createProcessDefinitionQuery()
                                .deploymentId(deployment.getId()).singleResult();
                // 啓動流程定義,返回流程實例
                ProcessInstance pi = runtimeService
                                .startProcessInstanceById(processDefinition.getId());
                String processId = pi.getId();
                System.out.println("流程建立成功,當前流程實例ID:" + processId);

                Task task = taskService.createTaskQuery().processInstanceId(processId)
                                .singleResult();
                System.out.println("第一次執行前,任務名稱:" + task.getName());
                taskService.complete(task.getId());

                task = taskService.createTaskQuery().processInstanceId(processId)
                                .singleResult();
                System.out.println("第二次執行前,任務名稱:" + task.getName());
                taskService.complete(task.getId());

                task = taskService.createTaskQuery().processInstanceId(processId)
                                .singleResult();
                System.out.println("task爲null,任務執行完畢:" + task);
        }
}

六、啓動項目並進行驗證

查看日誌,項目啓動成功
3四、Spring Cloud 2.x之整合工做流Activiti

訪問:http://127.0.0.1:8081/ad
3四、Spring Cloud 2.x之整合工做流Activiti
從控制檯看到打印出請假流程審批的對應日誌。
再從數據庫的數據變化看,自帶建立了以下表結構

3四、Spring Cloud 2.x之整合工做流Activiti

若是使用sql語句查看相應的表能夠看到對應的庫表裏留下了相應的請假及審批記錄信息。

相關文章
相關標籤/搜索