最近公司使用flowable作为流程引擎的框架,所以先研究了下它的使用方式,也做了一些简单使用小结。
具体参考测试项目https://github.com/yaojf/holidayrequest。
感谢分享牛提供学习资源:http://www.niu.com/category/236.htm
1、开启一个流程引擎实例
ProcessEngine processEngine =
ProcessEngines.getDefaultProcessEngine();
它会默认使用classpath下面的flowable.cfg.xml配置文件,实例内容如下:
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">
id="processEngineConfiguration"
class="org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration">
name="dataSource"
ref="dataSource"
/>
name="databaseSchemaUpdate"
value="true"
/>
name="asyncExecutorActivate"
value="false"
/>
name="mailServerHost"
value="mail.my-corp.com"
/>
name="mailServerPort"
value="5025"
/>
id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
>
name="driverClassName"
value="com.mysql.jdbc.Driver"
/>
name="url"
value="jdbc:mysql://localhost:3306/flowable"
/>
name="username"
value="root"
/>
name="password"
value="root"
/>
name="defaultAutoCommit"
value="false"
/>
这里有个配置
name="databaseSchemaUpdate"
value="true"
/>
代表是否自动更新数据库表,配置为true,当流程引擎检测到表不存在时,会自动创建。表结构如下图所示:
2、部署一个流程模版(涉及ACT_RE_DEPLOYMENT,ACT_RE_PROCDEF,ACT_GE_BYTEARRAY)
RepositoryService repositoryService =
processEngine.getRepositoryService();
Deployment deployment =
repositoryService.createDeployment()
.addClasspathResource("one-task-process.bpmn20.xml")
.deploy();
这里使用的是标准的bpmn建模语言配置文件,示例内容如下:
version="1.0"
encoding="UTF-8"?>
xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:flowable="http://flowable.org/bpmn"
targetNamespace="Examples">
id="oneTaskProcess"
name="The One Task
Process">
id="theStart"
/>
id="flow1"
sourceRef="theStart"
targetRef="theTask"
/>
id="theTask"
name="my task"
/>
id="flow2"
sourceRef="theTask"
targetRef="theEnd"
/>
id="theEnd"
/>
3、启动一个流程实例(涉及ACT_RU_EXECUTION,ACT_RU_TASK,ACT_HI_ACTINST,ACT_HI_PROCINST,ACT_HI_TASKINST)
RuntimeService runtimeService =
processEngine.getRuntimeService();
Map variables = new HashMap();
variables.put("employee", employee);
variables.put("nrOfHolidays", nrOfHolidays);
variables.put("description", description);
ProcessInstance processInstance =
runtimeService.startProcessInstanceByKey("oneTaskProcess",
variables);
流程实例启动的时候可以传递参数,这些参数可以作用于整个流程实例的生命周期,比如在配置文件里可以使用这个变量
id="holidayApprovedTask"
name="Holiday approved"
flowable:assignee="${employee}"/>
4、完成一个用户任务
variables = new HashMap();
variables.put("approved", approved);
taskService.complete(task.getId(), variables);
流程模版里有分开始事件,用户任务,结束事件等流程节点,其中userTask需要通过代码complete,其余节点属于自动节点,当完成一个任务的时候,流程实例会自动走下一个节点
直到流程结束。
5、这里有个ManagementService我们可以查询flowable框架里的JavaBean对应哪张表。
public class ManagementServiceTest {
ProcessEngine
processEngine = null;
@Before
public void before()
{
processEngine =
ProcessEngines.getDefaultProcessEngine();
}
@Test
public void
testManagementService() {
ManagementService managementService =
processEngine.getManagementService();
System.out.println(managementService.getTableName(HistoricActivityInstance.class));
}
}
6、小结
ACT_HI_PROCINST表里的BUSINESS_KEY_可以作为业务流程主键,关联唯一的一条记录
ACT_HI_TASKINST表里的ASSIGNEE_和TENANT_ID_可以区分任务分配给哪个受理人