当前位置: 移动技术网 > IT编程>开发语言>Java > spring boot activiti工作流的搭建与简单使用

spring boot activiti工作流的搭建与简单使用

2019年07月19日  | 移动技术网IT编程  | 我要评论

前言

最近一直研究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文件,如图

文件内容:

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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网