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

 更新时间:2018年08月09日 10:58:46   作者:kkkder  
这篇文章主要给大家介绍了关于spring boot activiti工作流的搭建与简单使用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

前言

最近一直研究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。

总结

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

相关文章

  • SpringBoot自动装配原理小结

    SpringBoot自动装配原理小结

    Spring Boot主要作用就是简化Spring应用的开发,开发者只需要通过少量代码就可以创建一个Spring应用,而达到这一目的最核心的思想就是约定优于配置。
    2021-05-05
  • springMVC如何防止表单重复提交详解

    springMVC如何防止表单重复提交详解

    平时开发的项目中经常会遇到表单重复提交,造成数据重复,增加服务器负载,严重甚至会造成服务器宕机,因此有效防止表单重复提交有一定的必要性,这篇文章主要给大家介绍了关于springMVC如何防止表单重复提交的相关资料,需要的朋友可以参考下
    2021-11-11
  • java 注解实现一个可配置线程池的方法示例

    java 注解实现一个可配置线程池的方法示例

    这篇文章主要介绍了java 注解实现一个可配置线程池的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • java通过方向键控制小球移动的小游戏

    java通过方向键控制小球移动的小游戏

    这篇文章主要为大家详细介绍了java通过方向键控制小球移动的小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-10-10
  • Java使用FFmpeg处理视频文件的方法教程

    Java使用FFmpeg处理视频文件的方法教程

    这篇文章主要给大家介绍了关于Java使用FFmpeg处理视频文件的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-03-03
  • Java版本和C++版本的二叉树序列化与反序列化

    Java版本和C++版本的二叉树序列化与反序列化

    这篇文章主要介绍了Java版本和C++版本的二叉树序列化与反序列化,二叉树就是节点在内存区域中串联起来的,但是如果掉电,内存上的数据就没有了。为了保存这种结构,将二叉树用字符串的形式保存到硬盘中,这就是序列化;从字符串形式转换为二叉树,这就是反序列化
    2022-06-06
  • 解读JDK1.8 默认使用什么垃圾收集器

    解读JDK1.8 默认使用什么垃圾收集器

    这篇文章主要介绍了解读JDK1.8 默认使用什么垃圾收集器,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-05-05
  • Spring开发核心之AOP的实现与切入点持久化

    Spring开发核心之AOP的实现与切入点持久化

    面向对象编程是一种编程方式,此编程方式的落地需要使用“类”和 “对象”来实现,所以,面向对象编程其实就是对 “类”和“对象” 的使用,面向切面编程,简单的说,就是动态地将代码切入到类的指定方法、指定位置上的编程思想就是面向切面的编程
    2022-10-10
  • java 泛型的详解及实例

    java 泛型的详解及实例

    这篇文章主要介绍了java 泛型的详解及实例的相关资料,希望通过本文大家能彻底掌握泛型的使用方法,需要的朋友可以参考下
    2017-08-08
  • Eureka源码阅读解析Server服务端启动流程实例

    Eureka源码阅读解析Server服务端启动流程实例

    这篇文章主要为大家介绍了Eureka源码阅读解析Server服务端启动流程实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10

最新评论