@Test
public void processEngine01(){
// 获取 ProcessEngineConfiguration 对象
ProcessEngineConfiguration configuration = new StandaloneProcessEngineConfiguration();
// 配置 相关的数据库的连接信息
configuration.setJdbcDriver("com.mysql.cj.jdbc.Driver");
configuration.setJdbcUsername("root");
configuration.setJdbcPassword("123456");
configuration.setJdbcUrl("jdbc:mysql://localhost:3306/flowable-learn?serverTimezone=UTC&nullCatalogMeansCurrent=true");
// 如果数据库中的表结构不存在就新建
configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
ProcessEngine processEngine = configuration.buildProcessEngine();
}
flowable.cfg.xml
文件<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">
<bean id="processEngineConfiguration"
class="org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/flowable-learn?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC&nullCatalogMeansCurrent=true" /><property name="jdbcDriver" value="com.mysql.cj.jdbc.Driver" />
<property name="jdbcUsername" value="root" />
<property name="jdbcPassword" value="123456" />
<property name="databaseSchemaUpdate" value="true" />
<property name="asyncExecutorActivate" value="false" />
bean>
beans>
/**
* 加载默认的配置文件
*/
@Test
public void processEngin02(){
ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
System.out.println("defaultProcessEngine = " + defaultProcessEngine);
}
/**
* 加载自定义名称的配置文件
*/
@Test
public void processEngin03() {
ProcessEngineConfiguration configuration = ProcessEngineConfiguration
.createProcessEngineConfigurationFromResource("flowable.cfg.xml");
ProcessEngine processEngine = configuration.buildProcessEngine();
System.out.println("processEngine = " + processEngine);
}
/**
* 获取服务
*/
@Test
public void getService() {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 获取运行时服务 RuntimeService
RuntimeService runtimeService = processEngine.getRuntimeService();
// 获取仓库服务 RepositoryService
RepositoryService repositoryService = processEngine.getRepositoryService();
// 获取任务服务 TaskService
TaskService taskService = processEngine.getTaskService();
// 获取管理服务 ManagementService
ManagementService managementService = processEngine.getManagementService();
// 获取身份认证服务 IdentityService
IdentityService identityService = processEngine.getIdentityService();
// 获取历史服务 HistoryService
HistoryService historyService = processEngine.getHistoryService();
// 获取表单服务 FormService
FormService formService = processEngine.getFormService();
// 获取动态BPMN服务 DynamicBpmnService
DynamicBpmnService dynamicBpmnService = processEngine.getDynamicBpmnService();
System.out.println("Runtime Service: " + runtimeService);
System.out.println("Repository Service: " + repositoryService);
System.out.println("Task Service: " + taskService);
System.out.println("Management Service: " + managementService);
System.out.println("Identity Service: " + identityService);
System.out.println("History Service: " + historyService);
System.out.println("Form Service: " + formService);
System.out.println("Dynamic BPMN Service: " + dynamicBpmnService);
}
RuntimeService: 提供了与运行时流程实例相关的服务,如启动流程实例、查询任务等。
RepositoryService: 提供了与流程定义仓库相关的服务,如部署、查询和删除流程定义等。
TaskService: 提供了与任务相关的服务,如查询、完成和委托任务等。
ManagementService: 提供了与引擎管理相关的服务,如查询引擎表和作业等。
IdentityService: 提供了与用户和组相关的服务,如创建、查询和删除用户等。
HistoryService: 提供了与历史数据相关的服务,如查询流程实例历史、任务历史等。
FormService: 提供了与表单相关的服务,如提交表单和获取表单数据等。
DynamicBpmnService: 提供了与动态BPMN相关的服务,如修改流程定义的BPMN XML等。
在Flowable中的事件图标启动事件
、边界事件
、中间事件
和结束事件
启动事件(Start Event):
边界事件(Boundary Event):
中间事件(Intermediate Event):
结束事件(End Event):
在 Flowable 中,活动(Activity)或任务(Task)是流程中的执行单元,代表了工作流程中的一个步骤或操作。活动可以是用户任务、自动任务、服务任务等,它们在流程中定义了工作的具体内容。
结构图标可以看做是整个流程活动的结构,一个流程中可以包括子流程。
/**
*部署流程
*/
@Test
public void testDeploy(){
// 1.获取 ProcessEngine 对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 2.获取RepositoryService
RepositoryService repositoryService = processEngine.getRepositoryService();
// 3.完成流程的部署操作
Deployment deploy = repositoryService.createDeployment()// 创建Deployment对象
.addClasspathResource("myProcess.bpmn20.xml") // 添加流程部署文件
.name("请假流程") // 设置部署流程的名称
.deploy(); // 执行部署操作
System.out.println("deploy.getId() = " + deploy.getId());
System.out.println("deploy.getName() = " + deploy.getName());
}
/**
* 流程定义挂起和激活
*/
@Test
public void testProcessDefinitionActivateAndSuspend() {
// 获取流程引擎对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
.processDefinitionId("myProcess:5:155004")
.singleResult();
// 获取流程定义的状态
boolean suspended = processDefinition.isSuspended();
System.out.println("suspended = " + suspended);
if (suspended) {
// 表示被挂起
System.out.println("激活流程定义");
repositoryService.activateProcessDefinitionById("myProcess:5:155004", true, null);
} else {
// 表示激活状态
System.out.println("挂起流程");
repositoryService.suspendProcessDefinitionById("myProcess:5:155004", true, null);
}
}
/**
* 启动流程实例
*/
@Test
public void testRunProcess(){
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 我们需要通过RuntimeService来启动流程实例
RuntimeService runtimeService = processEngine.getRuntimeService();
// 启动流程实例
ProcessInstance holidayRequest = runtimeService.startProcessInstanceById("myProcess:5:155004"); //从库中act_re_procdef表查看该值
System.out.println("holidayRequest.getProcessDefinitionId() = " + holidayRequest.getProcessDefinitionId());
System.out.println("holidayRequest.getActivityId() = " + holidayRequest.getActivityId());
System.out.println("holidayRequest.getId() = " + holidayRequest.getId());
}
/**
* 流程实例挂起和激活
*/
@Test
public void testProcessInstanceActivateAndSuspend() {
// 1.获取ProcessEngine对象
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
// 2.获取RuntimeService
RuntimeService runtimeService = engine.getRuntimeService();
// 3.获取流程实例对象
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
.processInstanceId("162501")
.singleResult();
// 4.获取相关的状态操作
boolean suspended = processInstance.isSuspended();
String id = processInstance.getId();
if (suspended) {
// 挂起--》激活
runtimeService.activateProcessInstanceById(id);
System.out.println("流程实例:" + id + ",已激活");
} else {
// 激活--》挂起
runtimeService.suspendProcessInstanceById(id);
System.out.println("流程实例:" + id + ",已挂起");
}
}
/**
* 完成zhangsan当前任务
*/
@Test
public void testCompleteTask(){
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
List<Task> list = taskService.createTaskQuery()
.processDefinitionKey("myProcess")
.taskAssignee("zhangsan")
.list();
for (Task task : list) {
// 完成任务
taskService.complete(task.getId());
}
}