原文链接:http://www.bug315.com/article/125.htm
今天开始学习Activiti,在搭建环境的时候出现了很多错误。其他问题都解决了,唯独下面这个问题纠结了很久。还好最后还是解决了。
错误信息:
70 [main] INFO org.activiti.engine.ProcessEngines - Initializing process engine using configuration 'file:/F:/Project_All/Eclipase_Workspace/Activiti_test01/bin/activiti.cfg.xml'
89 [main] INFO org.activiti.engine.ProcessEngines - initializing process engine for resource file:/F:/Project_All/Eclipase_Workspace/Activiti_test01/bin/activiti.cfg.xml
2015-2-14 9:07:16 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from resource loaded through InputStream
17767 [main] INFO org.activiti.engine.impl.ProcessEngineImpl - ProcessEngine default created
17767 [main] INFO org.activiti.engine.impl.jobexecutor.JobExecutor - Starting up the JobExecutor[org.activiti.engine.impl.jobexecutor.DefaultJobExecutor].
17818 [main] INFO org.activiti.engine.ProcessEngines - initialised process engine default
17819 [Thread-1] INFO org.activiti.engine.impl.jobexecutor.AcquireJobsRunnable - JobExecutor[org.activiti.engine.impl.jobexecutor.DefaultJobExecutor] starting to acquire jobs
18204 [main] INFO org.activiti.engine.impl.bpmn.deployer.BpmnDeployer - Processing resource bpmn/activiti_test01.bpmn20.xml
21012 [main] ERROR org.activiti.engine.impl.interceptor.CommandContext - Error while closing command context
org.activiti.engine.ActivitiObjectNotFoundException: no deployed process definition found with id 'simpleApprovalProcess'
at org.activiti.engine.impl.persistence.deploy.DeploymentManager.findDeployedProcessDefinitionById(DeploymentManager.java:55)
at org.activiti.engine.impl.cmd.StartProcessInstanceCmd.execute(StartProcessInstanceCmd.java:59)
at org.activiti.engine.impl.cmd.StartProcessInstanceCmd.execute(StartProcessInstanceCmd.java:36)
at org.activiti.engine.impl.interceptor.CommandInvoker.execute(CommandInvoker.java:24)
at org.activiti.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:57)
at org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:31)
at org.activiti.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:40)
at org.activiti.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:35)
at org.activiti.engine.impl.RuntimeServiceImpl.startProcessInstanceById(RuntimeServiceImpl.java:72)
at com.bug315.ActivitiTest01.main(ActivitiTest01.java:22)
Exception in thread "main" org.activiti.engine.ActivitiObjectNotFoundException: no deployed process definition found with id 'simpleApprovalProcess'
at org.activiti.engine.impl.persistence.deploy.DeploymentManager.findDeployedProcessDefinitionById(DeploymentManager.java:55)
at org.activiti.engine.impl.cmd.StartProcessInstanceCmd.execute(StartProcessInstanceCmd.java:59)
at org.activiti.engine.impl.cmd.StartProcessInstanceCmd.execute(StartProcessInstanceCmd.java:36)
at org.activiti.engine.impl.interceptor.CommandInvoker.execute(CommandInvoker.java:24)
at org.activiti.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:57)
at org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:31)
at org.activiti.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:40)
at org.activiti.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:35)
at org.activiti.engine.impl.RuntimeServiceImpl.startProcessInstanceById(RuntimeServiceImpl.java:72)
at com.bug315.ActivitiTest01.main(ActivitiTest01.java:22)
我的Java代码:
package com.bug315;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
public class ActivitiTest01 {
public static void main(String[] args) {
// 创建流程引擎
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 获取流程存储服务组件
RepositoryService repositoryService = processEngine.getRepositoryService();
// 获取运行时服务组件
RuntimeService runtimeService = processEngine.getRuntimeService();
// 获取流程任务组件
TaskService taskService = processEngine.getTaskService();
// 1、部署流程文件
repositoryService.createDeployment().name("myProcess")
.addClasspathResource("bpmn/activiti_test01.bpmn").deploy();
// 2、启动流程
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess");
//runtimeService.startProcessInstanceById("myProcess");
System.out.println(processInstance.getId());
// 3、查询第一个任务
Task task = taskService.createTaskQuery().singleResult();
System.out.println("============" + task.getId() + "============");
taskService.complete(task.getId()); // 完成任务
// 4、查询第二个任务
task = taskService.createTaskQuery().singleResult();
System.out.println("============" + task.getId() + "============");
taskService.complete(task.getId()); // 完成任务
// 5、流程结束
task = taskService.createTaskQuery().singleResult();
System.out.println("============ 任务结束(" + task + ") ============");
}
}
解决办法:
仔细看上面的异常是指定的流程定义ID不存在。再仔细看看我的代码,我的愿意是通过流程定义中<process>元素的id去启动流程。但是我错误写使用了startProcessInstanceById方法。因此将startProcessInstanceById()方法改为startProcessInstanceByKey()方法就解决问题了。你到act_re_procdef表中去查看ID_字段的格式为:myProcess:1:4,所以根本就不存在ID等于myProcess的流程定义。这些都是初心大意造成的,不知你是否也犯过这种低级错误。
部分流程XML代码:
<process id="myProcess" name="My process" isExecutable="true">
部分测试代码:
// 2、启动流程
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess");
// runtimeService.startProcessInstanceById("myProcess"); // 这里调用方法写错了