1. 程式人生 > >Spring Cloud 2.x之Activiti整合規則引擎Drools

Spring Cloud 2.x之Activiti整合規則引擎Drools

日常生活是由規則驅動的。紅燈停綠燈行,這是我們的交通規則;我們站著往上跳,最終還是要落下來,這是地球的引力規則。規則在生活中無處不在。軟體開發中我們也需要規則,滿足什麼規則應該進入什麼分支。如果做過風控系統,就知道風控系統裡存在非常多的規則(比如:age < 16 || age > 50 -> REJECT )。最便捷的實現就是用 if-else 來寫,但是隨著規則的增加以及需求的變動,程式碼將變得越來越難閱讀和理解,如果再去修改這些程式碼,然後測試不夠充分的話,將產生嚴重的生產事故。這時候就要引入Drools等規則引擎了。Drools就是為了解決業務程式碼和業務規則分離的引擎。

要使用Drools規則引擎,需要先安裝安裝JBoss Drools Support外掛,這裡就不多說怎麼安裝安裝JBoss Drools Support外掛。下載地址如下:

https://download.jboss.org/drools/release/7.3.0.Final/droolsjbpm-tools-distribution-7.3.0.Final.zip

1、 新建專案sc-activiti-drools,對應的pom.xml檔案如下

<?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-drools</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-engine</artifactId>
        <version>6.0.0</version>
    </dependency>

    <dependency>
        <groupId>org.activiti</groupId>
        <artifactId>activiti-spring</artifactId>
        <version>6.0.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-core</artifactId>
        <version>7.0.0.Final</version>
    </dependency>

    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-compiler</artifactId>
        <version>7.0.0.Final</version>
    </dependency>

    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>knowledge-api</artifactId>
        <version>6.5.0.Final</version>
    </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>

2、新建spring配置檔案application.yml

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 #自動部署驗證設定:true-開啟(預設)、false-關閉
  jpa:
    properties:
      hibernate:
        hbm2ddl:
          auto: update
  show-sql: true

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

3、新建activiti對應的配置檔案activiti.cfg.xml

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

<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/act?useUnicode=true&amp;characterEncoding=UTF-8" />
    <property name="username" value="root" />
    <property name="password" value="root" />
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
    <property name="dataSource" ref="dataSource" />
    <property name="transactionManager" ref="transactionManager" />
    <property name="databaseSchemaUpdate" value="true" />
    <property name="customPostDeployers">
        <list> 
              <bean class="org.activiti.engine.impl.rules.RulesDeployer" />
        </list> 
    </property>
    <!-- 
        <property name="deploymentResources" value="classpath*:/bpmn/*.bpmn" />  
     -->
</bean>

<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
    <property name="processEngineConfiguration" ref="processEngineConfiguration" />
</bean>

<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
<bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
<bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />

4、新建請假流程對應的bpmn檔案如下

<?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:xsd="http://www.w3.org/2001/XMLSchema"
    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="leave" name="請假審批" isExecutable="true">
        <startEvent id="startevent1" name="Start"></startEvent>
        <endEvent id="endevent1" name="End"></endEvent>
        <userTask id="usertask1" name="部門經理審批"></userTask>
        <businessRuleTask id="businessruletask1" name="天數判斷" activiti:ruleVariablesInput="${leave}" activiti:rules="leave1,leave2" activiti:resultVariable="reason"></businessRuleTask>
        <serviceTask id="servicetask1" name="獲取變數" activiti:class="sc.ad.service.DroolsService"></serviceTask>
        <userTask id="usertask2" name="HR審批"></userTask>
        <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
        <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="businessruletask1"></sequenceFlow>
        <sequenceFlow id="flow3" sourceRef="businessruletask1" targetRef="servicetask1"></sequenceFlow>
        <userTask id="usertask3" name="總經理審批"></userTask>
        <sequenceFlow id="flow4" sourceRef="servicetask1" targetRef="usertask3">
            <conditionExpression xsi:type="tFormalExpression"><![CDATA[${reason[0].total >= 10}]]></conditionExpression>
        </sequenceFlow>
        <sequenceFlow id="flow5" sourceRef="servicetask1" targetRef="usertask2">
            <conditionExpression xsi:type="tFormalExpression"><![CDATA[${reason[0].total < 10}]]></conditionExpression>
        </sequenceFlow>
        <sequenceFlow id="flow6" sourceRef="usertask3" targetRef="usertask2"></sequenceFlow>
        <sequenceFlow id="flow7" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>
    </process>
</definitions>

bpmn檔案對應的圖如下
在這裡插入圖片描述
5、新建規則檔案leave.drl

package sc.ad;

import sc.ad.model.Leave;

rule "leave1"
    when
        u : Leave(day < 3);
    then
        u.setTotal(u.getDay() + 2);
end

rule "leave2"
    when
        u : Leave(day >= 3);
    then
        u.setTotal(u.getDay() + 5);
end

對應的圖如下
在這裡插入圖片描述
6、新建springboot啟動類檔案

package sc.ad;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource("activiti.cfg.xml")
public class ActivitiDroolsApp {
    public static void main(String[] args)
    {
        SpringApplication.run(ActivitiDroolsApp.class, args);
    }

}

7、新建一個controller,用來發起請假請求

package sc.ad.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.DeploymentBuilder;
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;

import sc.ad.model.Leave;

@RestController
public class ADController {

@Autowired
private RepositoryService repositoryService;

@Autowired
private RuntimeService runtimeService;

@Autowired
private TaskService taskService;

//    @RequestMapping("/ad")
//    public void ad() {
//
//        // 根據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);
//    }

@RequestMapping("/drl")
public void drl() {
    /**
     * 注意這裡:必須要把drl檔案一起deploy
     */
    DeploymentBuilder deploy = repositoryService.createDeployment();
    deploy.addClasspathResource("leave.bpmn").addClasspathResource("leave.drl");
    deploy.deploy();
    ProcessInstance pi = runtimeService.startProcessInstanceByKey("leave");
    Map<String, Object> vars = new HashMap<String, Object>();  
    vars.put("leave", new Leave("白展堂", 12));
    /**
     * 當前任務
     */
    List<Task> tasks = taskService.createTaskQuery().processInstanceId(pi.getId()).list();
    for(Task task : tasks)
    {
        System.out.println(task.getId() + " , " + task.getName());
        taskService.complete(task.getId(), vars);
    }
    /**
     * 下一步任務
     */
    tasks = taskService.createTaskQuery().processInstanceId(pi.getId()).list();
    for(Task task : tasks)
    {
        System.out.println(task.getId() + " , " + task.getName());
    }
}

}

8、啟動並驗證規則是否生效

從日誌中看啟動成功
在這裡插入圖片描述
訪問http://127.0.0.1:8081/drl後,再次檢視日誌:
在這裡插入圖片描述
把修改controller的如下程式碼

    	vars.put("leave", new Leave("白展堂", 12));

改成
vars.put(“leave”, new Leave(“喬峰”, 2));
在這裡插入圖片描述
可以看到規則檔案的規則已經生效:
在這裡插入圖片描述