天天看點

springboot activiti工作流簡單示例1.建立空白的maven微服務架構2.連接配接服務名、服務端口、資料庫配置3.main4.service及實作5.bpmn6.驗證

最近一直研究springboot,根據工作需求,工作流需要作為一個單獨的微服務工程來提供給其他服務調用,現在簡單的寫下工作流(使用的activiti)微服務的搭建與簡單使用

jdk:1.8

資料庫:mysql  5.7

IDE:eclipse

springboot:1.5.8

activiti:6.0.0

1.建立空白的maven微服務架構

建立maven項目的流程不在闡述,這裡添加上activiti、myslq連接配接的依賴,隻貼主要代碼 pox.xml

<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.xxx</groupId>
  <artifactId>xxx</artifactId>
  <version>0.0.1-SNAPSHOT</version>
   <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
    </parent>
    <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>6.0.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>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
           

确認服務是可用

2.連接配接服務名、服務端口、資料庫配置

在resources目錄下的application.properties(項目定位原因沒有使用yaml,可根據自己項目使用)

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/activity?characterEncoding=utf8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.show-sql=true
server.port=8081
server.context-path=/activity
server.session.timeout=10
server.tomcat.uri-encoding=UTF-8
           

确認配置的資料庫可用

3.main

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Created by Guo on 2017/11/15.
 */
@SpringBootApplication
public class ActivityApp
{
    public static void main(String[] args)
    {
        SpringApplication.run(ActivityApp.class, args);
    }
}
           

4.service及實作

service:

import org.activiti.engine.task.TaskQuery;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/activityService")
public interface ActivityConsumerService {
	/**
	 * 流程demo
	 * @return
	 */
	@RequestMapping(value="/startActivityDemo",method=RequestMethod.GET)
    public boolean startActivityDemo();
	
}
           

impl

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

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hongguaninfo.activity.service.ActivityConsumerService;
@Service("activityService")
public class ActivityConsumerServiceImpl implements ActivityConsumerService {

	@Autowired
    private RuntimeService runtimeService;
	@Autowired
    private TaskService taskService;
	
	@Override
	public boolean startActivityDemo() {
		System.out.println("method startActivityDemo begin....");
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("apply","zhangsan");
        map.put("approve","lisi");
//流程啟動
        ExecutionEntity pi1 = (ExecutionEntity) runtimeService.startProcessInstanceByKey("leave",map);
        String processId = pi1.getId();
        String taskId = pi1.getTasks().get(0).getId();
        taskService.complete(taskId, map);//完成第一步申請
        
        Task task = taskService.createTaskQuery().processInstanceId(processId).singleResult();
        String taskId2 = task.getId();
        map.put("pass", false);
        taskService.complete(taskId2, map);//駁回申請
        System.out.println("method startActivityDemo end....");
        return false;
	}
}
           

5.bpmn

在resources目錄下建立檔案夾:processes,并在processes建立一個新的bpmn檔案,如圖

springboot activiti工作流簡單示例1.建立空白的maven微服務架構2.連接配接服務名、服務端口、資料庫配置3.main4.service及實作5.bpmn6.驗證

檔案内容:

<?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" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="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/testm1510735932336" id="m1510735932336" name="">
  <process id="leave" isExecutable="true" isClosed="false" processType="None">
    <startEvent id="_2" name="StartEvent"></startEvent>
    <endEvent id="_3" name="EndEvent"></endEvent>
    <userTask id="approve" name="經理審批" activiti:assignee="${approve}"></userTask>
    <exclusiveGateway id="_5" name="ExclusiveGateway"></exclusiveGateway>
    <sequenceFlow id="_6" sourceRef="approve" targetRef="_5"></sequenceFlow>
    <sequenceFlow id="_7" name="通過" sourceRef="_5" targetRef="_3">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${pass}]]></conditionExpression>
    </sequenceFlow>
    <userTask id="application" name="送出申請" activiti:assignee="${apply}"></userTask>
    <sequenceFlow id="_9" sourceRef="_2" targetRef="application"></sequenceFlow>
    <sequenceFlow id="_10" sourceRef="application" targetRef="approve"></sequenceFlow>
    <userTask id="modify" name="修改申請" activiti:assignee="${apply}"></userTask>
    <sequenceFlow id="_12" name="不通過" sourceRef="_5" targetRef="modify">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${!pass}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="_13" sourceRef="modify" targetRef="approve"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_leave">
    <bpmndi:BPMNPlane bpmnElement="leave" id="BPMNPlane_leave">
      <bpmndi:BPMNShape bpmnElement="_2" id="BPMNShape__2">
        <omgdc:Bounds height="35.0" width="35.0" x="15.0" y="60.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_3" id="BPMNShape__3">
        <omgdc:Bounds height="35.0" width="35.0" x="630.0" y="63.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="approve" id="BPMNShape_approve">
        <omgdc:Bounds height="55.0" width="85.0" x="315.0" y="50.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_5" id="BPMNShape__5">
        <omgdc:Bounds height="40.0" width="40.0" x="505.0" y="60.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="application" id="BPMNShape_application">
        <omgdc:Bounds height="55.0" width="85.0" x="135.0" y="50.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="modify" id="BPMNShape_modify">
        <omgdc:Bounds height="55.0" width="85.0" x="315.0" y="150.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="_6" id="BPMNEdge__6">
        <omgdi:waypoint x="400.0" y="77.0"></omgdi:waypoint>
        <omgdi:waypoint x="505.0" y="80.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_7" id="BPMNEdge__7">
        <omgdi:waypoint x="545.0" y="80.0"></omgdi:waypoint>
        <omgdi:waypoint x="630.0" y="80.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_9" id="BPMNEdge__9">
        <omgdi:waypoint x="50.0" y="77.0"></omgdi:waypoint>
        <omgdi:waypoint x="135.0" y="77.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_10" id="BPMNEdge__10">
        <omgdi:waypoint x="220.0" y="77.0"></omgdi:waypoint>
        <omgdi:waypoint x="315.0" y="77.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_12" id="BPMNEdge__12">
        <omgdi:waypoint x="525.0" y="100.0"></omgdi:waypoint>
        <omgdi:waypoint x="525.0" y="177.0"></omgdi:waypoint>
        <omgdi:waypoint x="400.0" y="177.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_13" id="BPMNEdge__13">
        <omgdi:waypoint x="357.0" y="150.0"></omgdi:waypoint>
        <omgdi:waypoint x="357.0" y="105.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>
           

需要認知的問題:.項目啟動的時候,activiti會自動在mysql中建立activiti相關表,不用像oracle那樣需要手動去建立

6.驗證

啟動項目前,連接配接資料庫,檢視需要連接配接資料庫中沒有表,啟動項目完成後,重新整理資料庫,activiti已經建立相關表,打開act_re_procdef表,流程資料已經存在,即流程已經部署成功。 用浏覽器通路位址:http://127.0.0.1:8081/activity/activityService/startActivityDemo 列印了預計的日志,沒有報錯資訊,檢視資料庫中的act_ru_task表,發現剛才執行形成的資料,項目成功。 PS:隻是簡單的微服務,沒有去寫注冊服務、網關配置、熔斷機制等等,僅用于activiti與springboot的結合 =========================後續========================== 1.在項目單獨作為一個引擎,本身不部署流程的時候,如果resources目錄沒有“processes”目錄,啟動項目報錯--找不到processes目錄。需要在配置檔案中添加一下内容:

spring: 
  activiti: 
####校驗流程檔案,預設校驗resources下的processes檔案夾裡的流程檔案
    check-process-definitions: false
           

即啟動項目,不去校驗processes。

繼續閱讀