springboot整合activiti

整合出现的小插曲

插曲一:springboot和activiti 版本兼容问题

我的springboot版本是2.2.1,开始我pom引入的是activiti 6.0.0启动后报错,后来发现是版本兼容问题,然后各种尝试降低springboot版本,然后上网搜索发现好多都是用的是1.5.8 然后就尝试一下1.5.9可不可以,结果也可以。

activiti 6.0.0 配合 springboot 2.2.1启动报错如下:

java.lang.IllegalArgumentException: Could not find class [org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration]
	at org.springframework.util.ClassUtils.resolveClassName(ClassUtils.java:327) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at org.springframework.core.annotation.TypeMappedAnnotation.adapt(TypeMappedAnnotation.java:483) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at org.springframework.core.annotation.TypeMappedAnnotation.getValue(TypeMappedAnnotation.java:403) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at org.springframework.core.annotation.TypeMappedAnnotation.asMap(TypeMappedAnnotation.java:288) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]

插曲二:resource目录下新创建的目录processes启动时程序识别不存在

resource目录下创建的目录processes并放入bpmn文件后启动时程序报找不到processes目录,报错如下。

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2020-06-02 20:02:22.345 ERROR 9624 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'activityService': Unsatisfied dependency expressed through field 'runtimeService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'runtimeServiceBean' defined in class path resource [org/activiti/spring/boot/DataSourceProcessEngineAutoConfiguration$DataSourceProcessEngineConfiguration.class]: Unsatisfied dependency expressed through method 'runtimeServiceBean' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'processEngine' defined in class path resource [org/activiti/spring/boot/DataSourceProcessEngineAutoConfiguration$DataSourceProcessEngineConfiguration.class]: Unsatisfied dependency expressed through method 'processEngine' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springProcessEngineConfiguration' defined in class path resource [org/activiti/spring/boot/DataSourceProcessEngineAutoConfiguration$DataSourceProcessEngineConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.activiti.spring.SpringProcessEngineConfiguration]: Factory method 'springProcessEngineConfiguration' threw exception; nested exception is java.io.FileNotFoundException: class path resource [processes/] cannot be resolved to URL because it does not exist
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]

如何解决呢,很简单找不到是因为maven进行编译后的时候没有把这个文件打包到classes目录下。只需要在pom.xml中加上这句就好了**/*.bpmn

            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                    <!--加上这一句-->
                    <include>**/*.bpmn</include>
                </includes>
                <filtering>true</filtering>
            </resource>
   

试了几次错后,那么最终怎么整合起来的呢?现在就开始讲一下。

第一步:pom中添加依赖如下:

	<parent>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-parent</artifactId>
	    <version>1.5.9.RELEASE</version>
	    <relativePath/>
	</parent>
	
	 <dependency>
	     <groupId>org.activiti</groupId>
	     <artifactId>activiti-spring-boot-starter-basic</artifactId>
	     <version>6.0.0</version>
	 </dependency>

     <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>8.0.11</version>
     </dependency>

springboot1.5.9、activity6.0.0、mysql8.0.11

第二步:application.properties中添加activiti配置:
开始要把这个配置设置成true,这样程序启动后才会帮我们创建activity运行用到的28张表。首次启动后创建完了后,就可以改成false免的每次启动重新创建。
spring.activiti.database-schema-update=true

# 设置流程引擎启动和关闭时数据库执行的策略
# false:false为默认值,设置为该值后,Activiti在启动时,会对比数据库表中保存的版本,如果版本不匹配时,将在启动时抛出异常。
# true:设置为该值后,Activiti会对数据库中所有的表进行更新,如果表不存在,则Activiti会自动创建。
# create-drop:Activiti启动时,会执行数据库表的创建操作,在Activiti关闭时,执行数据库表的删除操作。
# drop-create:Activiti启动时,执行数据库表的删除操作在Activiti关闭时,会执行数据库表的创建操作。
spring.activiti.database-schema-update=true

第三步:application.properties中添加mysql连接配置:

因为activiti运行要有一套自己的表来交互,所以一定要配置数据库。
这里要注意我这里用的是最新的mysql版本8.0.11数据源url一定要拼时区

#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.datasource.url=jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
#spring.datasource.username=root(你数据库用户名)
#spring.datasource.password=root(你数据库密码)

第四步:resource目录下创建processes目录并创建一个bpmn流程定义文件
这里可以直接创建一个bpmn后缀的文件,copy下面的内容黏贴进去保存就好。这个改怎么写就自己慢慢研究吧,也可以后面装个插件直接画好生成出来。

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

第五步:当然是写helloworld 了

import org.activiti.engine.RepositoryService;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

@Service("activityService")
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;

    @Autowired
    private RepositoryService repositoryService;

    @Override
    public boolean startActivityDemo() {
        System.out.println("method startActivityDemo begin....");
        System.out.println("调用流程存储服务,查询部署数量:" + repositoryService.createDeploymentQuery().count());

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("apply", "大都督17918");
        map.put("approve", "大统领");

        //流程启动
        ExecutionEntity pi1 = (ExecutionEntity) runtimeService.startProcessInstanceByKey("leave", map);
        List<Task> tasks = taskService.createTaskQuery().taskAssignee("zhangsan").list();
        System.out.println(tasks.size());
        //当前任务办理人
        String assignee = "大都督一起加油";
        //创建一个任务查询对象
        List<Task> taskList = taskService.createTaskQuery().taskAssignee(assignee).list();
        if (! CollectionUtils.isEmpty(taskList)) {
            for (Task task : tasks) {
                System.out.println("-------------------------------------");
                System.out.println("任务ID:" + task.getId());
                System.out.println("任务的办理人:" + task.getAssignee());
                System.out.println("任务名称:" + task.getName());
                System.out.println("任务的创建时间:" + task.getCreateTime());
                System.out.println("流程实例ID:" + task.getProcessInstanceId());
                System.out.println("-------------------------------------");
            }
        }
        System.out.println("method startActivityDemo end....");
        return false;
    }
}

第六步:启动程序调用demo程序运行如下

method startActivityDemo begin....
调用流程存储服务,查询部署数量:1
1
-------------------------------------
任务ID:2507
任务的办理人:zhangsan
任务名称:提交申请
任务的创建时间:Tue Jun 02 01:09:30 CST 2020
流程实例ID:2501
-------------------------------------
method startActivityDemo end....

OK 到此整合就完成了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值