我的原因是:
<!-- 这里可以使用 链接池 dbcp-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql:///activiti?serverTimezone=GMT" />//?serverTimezone=GMT" 缺少这个
<property name="username" value="root" />
<property name="password" value="123456" />
<property name="maxActive" value="3" />
<property name="maxIdle" value="1" />
</bean>
其他原因:
可能原因1:Activiti的配置文件名称或路径出错
通过如下方法获取的流程引擎对象,需要保证Activiti配置文件名称为"activiti.cfg.xml"并且位于classpath下(可参考Activiti源码)。
// 创建流程引擎
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
如图所示为java project的目录结构,按照某些教程把activiti.cfg.xml放在了新建的resource文件夹下,但是resource并不是资源文件夹也是会出错,这时应该右键resource文件夹->build path->use as source folder指定为资源目录(右上角出现"#"图案)。
可能原因2:jar包缺失
只简单添加了 activiti/libs 目录下的所有jar包以及log4j、slf4j相关包
缺少mysql驱动包
缺少spring-beans、spring-core相关包
缺少mybatis相关包
缺少commons-xxx相关包
缺少juel相关包
缺少jackson相关包
...
建议将 activiti/wars/中的某个war包解压,导入其中lib下的所有Jar包,缺少包会报ClassNotFoundException,再根据情况添包。附上lib图:
其它原因:配置文件内容有误或没有事先建立配置文件指向的数据库(数据库服务要开启)
(1)在activiti.cfg.xml配置文件中,添加了<property name="jobExecutorActivate" value="false" /> 去掉该属性试试。
(2)检查databaseSchemaUpdate配置是否与数据库表存在冲突?
附 测试源代码:
package com.bo.activiti;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
public class First {
public static void main(String[] args){
// 创建流程引擎
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// ProcessEngine processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml").buildProcessEngine();
System.out.println(processEngine);
// 得到流程存储服务组件
RepositoryService repositoryService = processEngine.getRepositoryService();
// 得到运行时服务组件
RuntimeService runtimeService = processEngine.getRuntimeService();
// 获取流程任务组件
TaskService taskService = processEngine.getTaskService();
// 部署流程定义文件
repositoryService.createDeployment().addClasspathResource("bpmn/first.bpmn").deploy();
// 启动流程
runtimeService.startProcessInstanceByKey("myProcess");
// 查询第一个任务
Task task = taskService.createTaskQuery().singleResult();
System.out.println("第一个任务完成前,当前任务名称:"+task.getName());
// 完成第一个任务
taskService.complete(task.getId());
// 查询第二个任务
task = taskService.createTaskQuery().singleResult();
System.out.println("第二个任务完成前,当前任务名称:"+task.getName());
// 完成第二个任务(流程结束)
taskService.complete(task.getId());
task = taskService.createTaskQuery().singleResult();
System.out.println("流程结束后,查找任务:" + task);
}
}
流程定义配置文件:first.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" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
<process id="myProcess" name="My process" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<userTask id="usertask2" name="Handle Request"></userTask>
<userTask id="usertask3" name="Expense Request"></userTask>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow1" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>
<sequenceFlow id="flow2" sourceRef="usertask3" targetRef="usertask2"></sequenceFlow>
<sequenceFlow id="flow3" sourceRef="startevent1" targetRef="usertask3"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_myProcess">
<bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess">
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="170.0" y="220.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
<omgdc:Bounds height="55.0" width="105.0" x="380.0" y="210.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask3" id="BPMNShape_usertask3">
<omgdc:Bounds height="55.0" width="105.0" x="240.0" y="210.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="520.0" y="220.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="485.0" y="237.0"></omgdi:waypoint>
<omgdi:waypoint x="520.0" y="237.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="345.0" y="237.0"></omgdi:waypoint>
<omgdi:waypoint x="380.0" y="237.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
<omgdi:waypoint x="205.0" y="237.0"></omgdi:waypoint>
<omgdi:waypoint x="240.0" y="237.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
流程定义设计图:
Activiti配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Activiti的引擎配置管理器 -->
<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<!-- 指定数据源 -->
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti" />
<property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
<property name="jdbcUsername" value="root" />
<property name="jdbcPassword" value="root" />
<property name="databaseSchemaUpdate" value="true" />
</bean>
</beans>
程序运行结果:
省了