Activiti教程介绍了这个轻量级的工作流引擎的基本概念和功能,包括其安装、配置、流程设计和API编程等内容。文章详细讲解了Activiti的应用场景和实战案例,帮助读者全面了解如何使用Activiti处理复杂的业务流程,实现企业流程自动化和业务流程管理。
Activiti是一个轻量级的、开源的工作流引擎,它基于Spring框架,支持BPMN 2.0标准。Activiti主要应用于企业流程自动化和业务流程管理,能够帮助企业实现业务流程的自动化处理和优化。
Activiti是基于BPMN 2.0的轻量级工作流引擎,它提供了API和工具,用于设计、执行、管理和监控业务流程。其核心功能包括流程定义、流程实例的启动、任务的处理和流程的监控等。相比其他复杂的业务流程引擎,Activiti具有简单易用的特点,同时提供了强大的功能支持。
Activiti适用于以下场景:
假设一家公司需要处理大量的订单,从接收订单到发货,涉及多个步骤,如验证订单、支付处理、库存检查等。使用Activiti可以将这些步骤定义为一个流程,自动处理订单的整个生命周期。
在开始使用Activiti之前,需要准备一个Java开发环境。以下步骤将指导你如何搭建Java开发环境:
JAVA_HOME
和PATH
。安装Activiti的步骤如下:
下载Activiti:从Activiti官网下载最新版本的Activiti。也可以通过Maven仓库获取Activiti依赖。
pom.xml
文件中添加Activiti依赖。<dependencies> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-engine</artifactId> <version>6.0.0</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring</artifactId> <version>6.0.0</version> </dependency> </dependencies>
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"> <property name="jobExecutorActivate" value="false"/> <property name="databaseSchemaUpdate" value="true"/> <property name="transactionManager" ref="transactionManager"/> <property name="dataSource" ref="dataSource"/> </bean> <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean"> <property name="processEngineConfiguration" ref="processEngineConfiguration"/> </bean>
Activiti默认使用H2内存数据库,但通常会将其配置为使用MySQL或PostgreSQL等持久化数据库。以下是配置MySQL数据库的步骤:
activiti
。<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/activiti"/> <property name="username" value="root"/> <property name="password" value="password"/> </bean>
-- Run this SQL script to create the Activiti database tables in MySQL CREATE DATABASE activiti; USE activiti; CREATE TABLE ACT_RE_DEPLOYMENT ( ID_ VARCHAR(64) NOT NULL, NAME_ VARCHAR(255), CATEGORY_ VARCHAR(255), TENANT_ID_ VARCHAR(255) DEFAULT '', DEPLOY_TIME_ datetime, PRIMARY KEY (ID_) ); -- Additional tables...
BPMN (Business Process Model and Notation) 是一种图形化建模语言,用于定义业务流程。Activiti支持BPMN 2.0标准,这意味着你可以使用BPMN工具绘制流程图,并将其转换为Activiti可以理解和执行的XML格式。
Activiti提供了一个名为Activiti Designer的工具,用于创建和编辑BPMN 2.0流程图。以下是如何使用Activiti Designer创建一个简单的请假流程:
假设你需要创建一个简单的请假流程,如下所示:
以下是创建请假流程的步骤:
在Activiti Designer中绘制流程图后,导出为XML文件,如下所示:
<process id="leaveProcess" name="Leave Process" isExecutable="true"> <startEvent id="startEvent" /> <sequenceFlow id="flow1" sourceRef="startEvent" targetRef="applyLeaveTask" /> <userTask id="applyLeaveTask" name="Apply for Leave" /> <sequenceFlow id="flow2" sourceRef="applyLeaveTask" targetRef="approveTask" /> <userTask id="approveTask" name="Approve Leave" /> <sequenceFlow id="flow3" sourceRef="approveTask" targetRef="hrReviewTask" /> <userTask id="hrReviewTask" name="HR Review" /> <sequenceFlow id="flow4" sourceRef="hrReviewTask" targetRef="endEvent" /> <endEvent id="endEvent" /> </process>
以下是常用的工作流元素及其作用:
startEvent
):表示流程的开始。endEvent
):表示流程的结束。userTask
):需要用户执行的任务。serviceTask
):执行业务逻辑的任务。parallelGateway
):用于分支流程。exclusiveGateway
):用于决策流程。subProcess
):包含一个独立的子流程。eventSubProcess
):由特定事件触发的子流程。timerEvent
):定时触发的事件,如定时任务。在Activiti中,流程定义可以由BPMN文件描述,并从文件中加载或从内存中创建。以下是如何部署一个流程定义的示例代码:
import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.repository.Deployment; public class ActivitiExample { public static void main(String[] args) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RepositoryService repositoryService = processEngine.getRepositoryService(); Deployment deployment = repositoryService.createDeployment() .addClasspathResource("leaveProcess.bpmn20.xml") .deploy(); System.out.println("Deployment ID: " + deployment.getId()); } }
启动流程实例是指创建一个新的流程实例并开始执行流程。以下是如何启动流程实例的示例代码:
import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RuntimeService; import org.activiti.engine.ProcessEngineConfiguration; public class ActivitiExample { public static void main(String[] args) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RuntimeService runtimeService = processEngine.getRuntimeService(); String processDefinitionId = "leaveProcess:1:2501"; String businessKey = "leaveRequest123"; String processInstanceId = runtimeService.startProcessInstanceById(processDefinitionId, businessKey).getId(); System.out.println("Process Instance ID: " + processInstanceId); } }
在流程执行过程中,可能会遇到任务和事件。以下是如何处理任务和事件的示例代码:
获取并完成任务。
import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.TaskService; import org.activiti.engine.task.Task; import org.activiti.engine.task.TaskQuery; public class ActivitiExample { public static void main(String[] args) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); TaskService taskService = processEngine.getTaskService(); TaskQuery taskQuery = taskService.createTaskQuery() .taskAssignee("john"); List<Task> tasks = taskQuery.list(); for (Task task : tasks) { System.out.println("Task ID: " + task.getId()); System.out.println("Task Name: " + task.getName()); taskService.complete(task.getId()); } } }
事件可以在流程执行中触发,例如定时事件或信号事件。
import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.ProcessEngineConfiguration; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; import org.activiti.engine.task.TaskQuery; import org.activiti.engine.runtime.Execution; public class ActivitiExample { public static void main(String[] args) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); TaskService taskService = processEngine.getTaskService(); RuntimeService runtimeService = processEngine.getRuntimeService(); String processInstanceId = "yourProcessInstanceId"; Execution execution = runtimeService.createExecutionQuery() .processInstanceId(processInstanceId) .singleResult(); runtimeService.signalEventReceived("myCustomSignal", execution); TaskQuery taskQuery = taskService.createTaskQuery() .processInstanceId(processInstanceId); List<Task> tasks = taskQuery.list(); for (Task task : tasks) { System.out.println("Task ID: " + task.getId()); System.out.println("Task Name: " + task.getName()); taskService.complete(task.getId()); } } }
在这一部分,我们将实现一个完整的请假流程案例,包括流程模型的设计、部署、启动和任务处理。
创建一个简单的请假流程模型,包括以下步骤:
使用Activiti API将请假流程部署到Activiti引擎中。
import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.repository.Deployment; public class LeaveProcessExample { public static void main(String[] args) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RepositoryService repositoryService = processEngine.getRepositoryService(); Deployment deployment = repositoryService.createDeployment() .addClasspathResource("leaveProcess.bpmn20.xml") .deploy(); System.out.println("Deployment ID: " + deployment.getId()); } }
启动一个新的请假流程实例。
import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RuntimeService; import org.activiti.engine.ProcessEngineConfiguration; public class LeaveProcessExample { public static void main(String[] args) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RuntimeService runtimeService = processEngine.getRuntimeService(); String processDefinitionId = "leaveProcess:1:2501"; String businessKey = "leaveRequest123"; String processInstanceId = runtimeService.startProcessInstanceById(processDefinitionId, businessKey).getId(); System.out.println("Process Instance ID: " + processInstanceId); } }
部门经理和HR分别处理审批任务。
import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.TaskService; import org.activiti.engine.task.Task; import org.activiti.engine.task.TaskQuery; public class LeaveProcessExample { public static void main(String[] args) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); TaskService taskService = processEngine.getTaskService(); TaskQuery taskQuery = taskService.createTaskQuery() .taskAssignee("departmentManager"); List<Task> tasks = taskQuery.list(); for (Task task : tasks) { System.out.println("Task ID: " + task.getId()); System.out.println("Task Name: " + task.getName()); taskService.complete(task.getId()); } taskQuery = taskService.createTaskQuery() .taskAssignee("hrManager"); tasks = taskQuery.list(); for (Task task : tasks) { System.out.println("Task ID: " + task.getId()); System.out.println("Task Name: " + task.getName()); taskService.complete(task.getId()); } } }
假设需要实现一个审批流程,包括以下步骤:
创建一个简单的审批流程模型,包括以下步骤:
使用Activiti API将审批流程部署到Activiti引擎中。
import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.repository.Deployment; public class ApprovalProcessExample { public static void main(String[] args) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RepositoryService repositoryService = processEngine.getRepositoryService(); Deployment deployment = repositoryService.createDeployment() .addClasspathResource("approvalProcess.bpmn20.xml") .deploy(); System.out.println("Deployment ID: " + deployment.getId()); } }
启动一个新的审批流程实例。
import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RuntimeService; import org.activiti.engine.ProcessEngineConfiguration; public class ApprovalProcessExample { public static void main(String[] args) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RuntimeService runtimeService = processEngine.getRuntimeService(); String processDefinitionId = "approvalProcess:1:2501"; String businessKey = "approvalRequest123"; String processInstanceId = runtimeService.startProcessInstanceById(processDefinitionId, businessKey).getId(); System.out.println("Process Instance ID: " + processInstanceId); } }
部门经理和财务经理分别处理审批任务。
import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.TaskService; import org.activiti.engine.task.Task; import org.activiti.engine.task.TaskQuery; public class ApprovalProcessExample { public static void main(String[] args) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); TaskService taskService = processEngine.getTaskService(); TaskQuery taskQuery = taskService.createTaskQuery() .taskAssignee("departmentManager"); List<Task> tasks = taskQuery.list(); for (Task task : tasks) { System.out.println("Task ID: " + task.getId()); System.out.println("Task Name: " + task.getName()); taskService.complete(task.getId()); } taskQuery = taskService.createTaskQuery() .taskAssignee("financeManager"); tasks = taskQuery.list(); for (Task task : tasks) { System.out.println("Task ID: " + task.getId()); System.out.println("Task Name: " + task.getName()); taskService.complete(task.getId()); } } }
假设需要实现一个销售流程,包括以下步骤:
创建一个简单的销售流程模型,包括以下步骤:
使用Activiti API将销售流程部署到Activiti引擎中。
import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.repository.Deployment; public class SalesProcessExample { public static void main(String[] args) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RepositoryService repositoryService = processEngine.getRepositoryService(); Deployment deployment = repositoryService.createDeployment() .addClasspathResource("salesProcess.bpmn20.xml") .deploy(); System.out.println("Deployment ID: " + deployment.getId()); } }
启动一个新的销售流程实例。
import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RuntimeService; import org.activiti.engine.ProcessEngineConfiguration; public class SalesProcessExample { public static void main(String[] args) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RuntimeService runtimeService = processEngine.getRuntimeService(); String processDefinitionId = "salesProcess:1:2501"; String businessKey = "salesOrder123"; String processInstanceId = runtimeService.startProcessInstanceById(processDefinitionId, businessKey).getId(); System.out.println("Process Instance ID: " + processInstanceId); } }
销售经理和财务人员分别处理审批任务。
import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.TaskService; import org.activiti.engine.task.Task; import org.activiti.engine.task.TaskQuery; public class SalesProcessExample { public static void main(String[] args) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); TaskService taskService = processEngine.getTaskService(); TaskQuery taskQuery = taskService.createTaskQuery() .taskAssignee("salesManager"); List<Task> tasks = taskQuery.list(); for (Task task : tasks) { System.out.println("Task ID: " + task.getId()); System.out.println("Task Name: " + task.getName()); taskService.complete(task.getId()); } taskQuery = taskService.createTaskQuery() .taskAssignee("finance"); tasks = taskQuery.list(); for (Task task : tasks) { System.out.println("Task ID: " + task.getId()); System.out.println("Task Name: " + task.getName()); taskService.complete(task.getId()); } } }
Activiti的源码相对简单,可以方便地进行调试和修改。以下是如何解析和调试Activiti源码的步骤:
以下是一个简单的调试示例,展示如何在任务处理过程中设置断点并运行调试:
import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.TaskService; import org.activiti.engine.task.Task; import org.activiti.engine.task.TaskQuery; public class DebugExample { public static void main(String[] args) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); TaskService taskService = processEngine.getTaskService(); TaskQuery taskQuery = taskService.createTaskQuery() .taskAssignee("departmentManager"); List<Task> tasks = taskQuery.list(); for (Task task : tasks) { System.out.println("Task ID: " + task.getId()); System.out.println("Task Name: " + task.getName()); taskService.complete(task.getId()); } } }
在上述代码中,可以在taskService.complete(task.getId());
这一行设置断点,以便在任务完成时进行调试,检查任务的状态和执行过程。
以下是一个简单的缓存示例,展示如何使用Activiti的缓存机制来缓存流程定义:
import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.ProcessEngineConfiguration; import org.springframework.cache.CacheManager; import org.springframework.cache.concurrent.ConcurrentMapCacheManager; public class CacheExample { public static void main(String[] args) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); ProcessEngineConfiguration configuration = processEngine.getProcessEngineConfiguration(); CacheManager cacheManager = new ConcurrentMapCacheManager(); configuration.setCacheManager(cacheManager); System.out.println("使用缓存机制"); } }
以下是常见的Activiti错误及其解决方案:
通过本文的介绍,你应该对Activiti有了全面的了解,包括其安装、配置、流程设计、API编程和实战案例。Activiti是一个强大的工作流引擎,可以处理复杂的业务流程。通过实践示例,你可以更好地掌握如何使用Activiti解决实际问题。希望本教程对你有所帮助。