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

    1. <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">
    2. <modelVersion>4.0.0</modelVersion>
    3. <groupId>com.xxx</groupId>
    4. <artifactId>xxx</artifactId>
    5. <version>0.0.1-SNAPSHOT</version>
    6. <properties>
    7. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    8. <java.version>1.8</java.version>
    9. </properties>
    10. <parent>
    11. <groupId>org.springframework.boot</groupId>
    12. <artifactId>spring-boot-starter-parent</artifactId>
    13. <version>1.5.8.RELEASE</version>
    14. </parent>
    15. <dependencies>
    16. <dependency>
    17. <groupId>org.springframework.boot</groupId>
    18. <artifactId>spring-boot-starter-web</artifactId>
    19. </dependency>
    20. <dependency>
    21. <groupId>org.activiti</groupId>
    22. <artifactId>activiti-spring-boot-starter-basic</artifactId>
    23. <version>6.0.0</version>
    24. </dependency>
    25. <dependency>
    26. <groupId>org.springframework.boot</groupId>
    27. <artifactId>spring-boot-starter-data-jpa</artifactId>
    28. </dependency>
    29. <dependency>
    30. <groupId>org.springframework.boot</groupId>
    31. <artifactId>spring-boot-starter-thymeleaf</artifactId>
    32. </dependency>
    33. <dependency>
    34. <groupId>org.springframework.boot</groupId>
    35. <artifactId>spring-boot-starter-web</artifactId>
    36. </dependency>
    37. <dependency>
    38. <groupId>mysql</groupId>
    39. <artifactId>mysql-connector-java</artifactId>
    40. <scope>runtime</scope>
    41. </dependency>
    42. <dependency>
    43. <groupId>org.springframework.boot</groupId>
    44. <artifactId>spring-boot-starter-tomcat</artifactId>
    45. </dependency>
    46. <dependency>
    47. <groupId>org.springframework.boot</groupId>
    48. <artifactId>spring-boot-starter-test</artifactId>
    49. <scope>test</scope>
    50. </dependency>
    51. </dependencies>
    52. <build>
    53. <plugins>
    54. <plugin>
    55. <groupId>org.springframework.boot</groupId>
    56. <artifactId>spring-boot-maven-plugin</artifactId>
    57. </plugin>
    58. </plugins>
    59. </build>
    60. </project>
    确认服务是可用

    2.连接服务名、服务端口、数据库配置

    在resources目录下的application.properties(项目定位原因没有使用yaml,可根据自己项目使用)
    1. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    2. spring.datasource.url=jdbc:mysql://127.0.0.1:3306/activity?characterEncoding=utf8&useSSL=true
    3. spring.datasource.username=root
    4. spring.datasource.password=root
    5. spring.jpa.properties.hibernate.hbm2ddl.auto=update
    6. spring.jpa.show-sql=true
    7. server.port=8081
    8. server.context-path=/activity
    9. server.session.timeout=10
    10. server.tomcat.uri-encoding=UTF-8
    确认配置的数据库可用

    3.main

    1. import org.springframework.boot.SpringApplication;
    2. import org.springframework.boot.autoconfigure.SpringBootApplication;
    3. /**
    4. * Created by Guo on 2017/11/15.
    5. */
    6. @SpringBootApplication
    7. public class ActivityApp
    8. {
    9. public static void main(String[] args)
    10. {
    11. SpringApplication.run(ActivityApp.class, args);
    12. }
    13. }

    4.service及实现

    service:
    1. import org.activiti.engine.task.TaskQuery;
    2. import org.springframework.web.bind.annotation.RequestMapping;
    3. import org.springframework.web.bind.annotation.RequestMethod;
    4. import org.springframework.web.bind.annotation.RestController;
    5. @RestController
    6. @RequestMapping("/activityService")
    7. public interface ActivityConsumerService {
    8. /**
    9. * 流程demo
    10. * @return
    11. */
    12. @RequestMapping(value="/startActivityDemo",method=RequestMethod.GET)
    13. public boolean startActivityDemo();
    14. }
    impl
    1. import java.util.HashMap;
    2. import java.util.Map;
    3. import org.activiti.engine.RuntimeService;
    4. import org.activiti.engine.TaskService;
    5. import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
    6. import org.activiti.engine.task.Task;
    7. import org.activiti.engine.task.TaskQuery;
    8. import org.apache.commons.lang3.StringUtils;
    9. import org.springframework.beans.factory.annotation.Autowired;
    10. import org.springframework.stereotype.Service;
    11. import com.hongguaninfo.activity.service.ActivityConsumerService;
    12. @Service("activityService")
    13. public class ActivityConsumerServiceImpl implements ActivityConsumerService {
    14. @Autowired
    15. private RuntimeService runtimeService;
    16. @Autowired
    17. private TaskService taskService;
    18. @Override
    19. public boolean startActivityDemo() {
    20. System.out.println("method startActivityDemo begin....");
    21. Map<String,Object> map = new HashMap<String,Object>();
    22. map.put("apply","zhangsan");
    23. map.put("approve","lisi");
    24. //流程启动
    25. ExecutionEntity pi1 = (ExecutionEntity) runtimeService.startProcessInstanceByKey("leave",map);
    26. String processId = pi1.getId();
    27. String taskId = pi1.getTasks().get(0).getId();
    28. taskService.complete(taskId, map);//完成第一步申请
    29. Task task = taskService.createTaskQuery().processInstanceId(processId).singleResult();
    30. String taskId2 = task.getId();
    31. map.put("pass", false);
    32. taskService.complete(taskId2, map);//驳回申请
    33. System.out.println("method startActivityDemo end....");
    34. return false;
    35. }
    36. }

    5.bpmn

    在resources目录下新建文件夹:processes,并在processes创建一个新的bpmn文件,如图

    文件内容:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <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="">
    3. <process id="leave" isExecutable="true" isClosed="false" processType="None">
    4. <startEvent id="_2" name="StartEvent"></startEvent>
    5. <endEvent id="_3" name="EndEvent"></endEvent>
    6. <userTask id="approve" name="经理审批" activiti:assignee="${approve}"></userTask>
    7. <exclusiveGateway id="_5" name="ExclusiveGateway"></exclusiveGateway>
    8. <sequenceFlow id="_6" sourceRef="approve" targetRef="_5"></sequenceFlow>
    9. <sequenceFlow id="_7" name="通过" sourceRef="_5" targetRef="_3">
    10. <conditionExpression xsi:type="tFormalExpression"><![CDATA[${pass}]]></conditionExpression>
    11. </sequenceFlow>
    12. <userTask id="application" name="提交申请" activiti:assignee="${apply}"></userTask>
    13. <sequenceFlow id="_9" sourceRef="_2" targetRef="application"></sequenceFlow>
    14. <sequenceFlow id="_10" sourceRef="application" targetRef="approve"></sequenceFlow>
    15. <userTask id="modify" name="修改申请" activiti:assignee="${apply}"></userTask>
    16. <sequenceFlow id="_12" name="不通过" sourceRef="_5" targetRef="modify">
    17. <conditionExpression xsi:type="tFormalExpression"><![CDATA[${!pass}]]></conditionExpression>
    18. </sequenceFlow>
    19. <sequenceFlow id="_13" sourceRef="modify" targetRef="approve"></sequenceFlow>
    20. </process>
    21. <bpmndi:BPMNDiagram id="BPMNDiagram_leave">
    22. <bpmndi:BPMNPlane bpmnElement="leave" id="BPMNPlane_leave">
    23. <bpmndi:BPMNShape bpmnElement="_2" id="BPMNShape__2">
    24. <omgdc:Bounds height="35.0" width="35.0" x="15.0" y="60.0"></omgdc:Bounds>
    25. </bpmndi:BPMNShape>
    26. <bpmndi:BPMNShape bpmnElement="_3" id="BPMNShape__3">
    27. <omgdc:Bounds height="35.0" width="35.0" x="630.0" y="63.0"></omgdc:Bounds>
    28. </bpmndi:BPMNShape>
    29. <bpmndi:BPMNShape bpmnElement="approve" id="BPMNShape_approve">
    30. <omgdc:Bounds height="55.0" width="85.0" x="315.0" y="50.0"></omgdc:Bounds>
    31. </bpmndi:BPMNShape>
    32. <bpmndi:BPMNShape bpmnElement="_5" id="BPMNShape__5">
    33. <omgdc:Bounds height="40.0" width="40.0" x="505.0" y="60.0"></omgdc:Bounds>
    34. </bpmndi:BPMNShape>
    35. <bpmndi:BPMNShape bpmnElement="application" id="BPMNShape_application">
    36. <omgdc:Bounds height="55.0" width="85.0" x="135.0" y="50.0"></omgdc:Bounds>
    37. </bpmndi:BPMNShape>
    38. <bpmndi:BPMNShape bpmnElement="modify" id="BPMNShape_modify">
    39. <omgdc:Bounds height="55.0" width="85.0" x="315.0" y="150.0"></omgdc:Bounds>
    40. </bpmndi:BPMNShape>
    41. <bpmndi:BPMNEdge bpmnElement="_6" id="BPMNEdge__6">
    42. <omgdi:waypoint x="400.0" y="77.0"></omgdi:waypoint>
    43. <omgdi:waypoint x="505.0" y="80.0"></omgdi:waypoint>
    44. </bpmndi:BPMNEdge>
    45. <bpmndi:BPMNEdge bpmnElement="_7" id="BPMNEdge__7">
    46. <omgdi:waypoint x="545.0" y="80.0"></omgdi:waypoint>
    47. <omgdi:waypoint x="630.0" y="80.0"></omgdi:waypoint>
    48. </bpmndi:BPMNEdge>
    49. <bpmndi:BPMNEdge bpmnElement="_9" id="BPMNEdge__9">
    50. <omgdi:waypoint x="50.0" y="77.0"></omgdi:waypoint>
    51. <omgdi:waypoint x="135.0" y="77.0"></omgdi:waypoint>
    52. </bpmndi:BPMNEdge>
    53. <bpmndi:BPMNEdge bpmnElement="_10" id="BPMNEdge__10">
    54. <omgdi:waypoint x="220.0" y="77.0"></omgdi:waypoint>
    55. <omgdi:waypoint x="315.0" y="77.0"></omgdi:waypoint>
    56. </bpmndi:BPMNEdge>
    57. <bpmndi:BPMNEdge bpmnElement="_12" id="BPMNEdge__12">
    58. <omgdi:waypoint x="525.0" y="100.0"></omgdi:waypoint>
    59. <omgdi:waypoint x="525.0" y="177.0"></omgdi:waypoint>
    60. <omgdi:waypoint x="400.0" y="177.0"></omgdi:waypoint>
    61. </bpmndi:BPMNEdge>
    62. <bpmndi:BPMNEdge bpmnElement="_13" id="BPMNEdge__13">
    63. <omgdi:waypoint x="357.0" y="150.0"></omgdi:waypoint>
    64. <omgdi:waypoint x="357.0" y="105.0"></omgdi:waypoint>
    65. </bpmndi:BPMNEdge>
    66. </bpmndi:BPMNPlane>
    67. </bpmndi:BPMNDiagram>
    68. </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目录。需要在配置文件中添加一下内容:
    1. spring:
    2. activiti:
    3. ####校验流程文件,默认校验resources下的processes文件夹里的流程文件
    4. check-process-definitions: false
    即启动项目,不去校验processes。


    原文地址:https://blog.csdn.net/kkkder/article/details/78562349
  • 相关阅读:
    大数据量下协同推荐的难点与优化方法
    几个常见的Mysql索引问题
    Orchard CMS -Migration文件更新后数据库不更新的问题 new properties not updating after migrationData migration is not working?
    一个可以设置中奖概率的抽奖程序[转]
    Implementing HTTPS Everywhere in ASP.Net MVC application.
    通过jQuery Ajax使用FormData对象上传文件
    SqlServer 数据去重
    向json中添加新的熟悉或对象 Add new attribute (element) to JSON object using JavaScript
    Dapper的基本使用,Insert、Update、Select、Delete
    DapperExtensions and Dapper.Contrib在表构架不是默认dbo时的处理 DapperExtensions and Dapper.Contrib with non-dbo Schema
  • 原文地址:https://www.cnblogs.com/jpfss/p/11077178.html
Copyright © 2020-2023  润新知