核心API介绍
1.ProcessEngine获取方式:
activiti.cfg.xml -> ProcessEngineConfiguration -> ProcessEngine
2.核心API:
ProcessEngine ->
- RepositoryService(负责对流程定义文件的管理,如xml或流程图片;部署流程对象涉及到:部署对象,资源对象,两者为一对多关系)
- RuntimeService (流程进行控制的API,启动流程实例,对流程实例暂停挂起,对上下文数据的设置和获取)
- TaskService (管理运行任务服务,UserTask)
- IdentifyService(管理用户和用户组,并维护用户组的关系)
- FormService(解析流程定义中的设计表单)
- HistoryService(对运行结束的流程实例的查询功能)
- ManagementService(流程引擎基础的管理)
- DynamicBpmService(能对动态的流程模型做修改,不推荐使用)
RepositoryService
- RepositoryService 提供的功能:1)管理流程定义文件xml及静态资源的服务;2)对特定流程的暂停和激活; 3)流程定义启动权限管理
- RepositoryService 涉及到的API:部署文件构造器DeploymentBuilder; 部署文件查询器DeploymentQuery; 流程定义文件查询器ProcessDefinitionQuery
- 通过ReposityService可以获取到:流程部署文件对象Deployment; 流程定义文件对象ProcessDefinition; 流程定义的Java格式BpmnModel
通过RepositoryService查询部署文件和流程定义文件
核心代码概览:
创建部署文件:DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
查询部署文件:List<Deployment> deploymentList = repositoryService.createDeploymentQuery().orderByDeploymenTime().asc().listPage(0, 100);
查询流程定义文件:List<ProcessDefinition> definitionList = repositoryService.createProcessDefinitionQuery().orderByProcessDefinitionKey().asc().listPage(0, 100);
具体实现:
public class RepositoryServiceTest {
private static final Logger LOGGER = LoggerFactory
.getLogger(RepositoryServiceTest.class);
@Rule
public ActivitiRule activitiRule = new ActivitiRule();
@Test
public void testRepository(){
RepositoryService repositoryService = activitiRule.getRepositoryService();
DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
deploymentBuilder.name("测试部署资源1").addClasspathResource("my-process.bpmn20.xml").addClasspathResource("second_approve.bpmn20.xml");
Deployment deploy = deploymentBuilder.deploy();//奖一个部署对象两个定义文件部署到数据库中
LOGGER.info("deploy = {}", deploy);//一个部署对象记录了一次部署
DeploymentBuilder deploymentBuilder1 = repositoryService.createDeployment();
deploymentBuilder1.name("测试部署资源2").addClasspathResource("my-process.bpmn20.xml").addClasspathResource("second_approve.bpmn20.xml");
Deployment deploy1 = deploymentBuilder1.deploy();
LOGGER.info("deploy1 = {}", deploy1);
//部署文件查询器
DeploymentQuery deploymentQuery = repositoryService.createDeploymentQuery();
//Deployment deployment = deploymentQuery.deploymentId(deploy.getId()).singleResult();
List<Deployment> deploymentList = deploymentQuery.orderByDeploymenTime().asc().listPage(0, 100);
for(Deployment deployment : deploymentList){
LOGGER.info("deployment = {}", deployment);
}
LOGGER.info("deploymentList.size = {}", deploymentList.size());
//流程定义文件查询器
List<ProcessDefinition> definitionList = repositoryService.createProcessDefinitionQuery().orderByProcessDefinitionKey().asc().listPage(0, 100);
for(ProcessDefinition processDefinition : definitionList){
LOGGER.info("processDefinition = {}, version = {}, key = {}, id = {}", processDefinition, processDefinition.getVersion(), processDefinition.getKey(), processDefinition.getId());
}
}
}
测试流程的挂起和启动
核心代码概览:
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().singleResult();
挂起:repositoryService.suspendProcessDefinitionById(processDefinition.getId());
激活:repositoryService.activateProcessDefinitionById(processDefinition.getId());
启动:activitiRule.getRuntimeService().startProcessInstanceById(processDefinition.getId());
具体实现:
@Test
//测试流程的挂起和启动
@org.activiti.engine.test.Deployment(resources = {"my-process.bpmn20.xml"})
public void testSuspend(){
RepositoryService repositoryService = activitiRule.getRepositoryService();
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().singleResult();
repositoryService.suspendProcessDefinitionById(processDefinition.getId());//流程挂起
try {
activitiRule.getRuntimeService().startProcessInstanceById(processDefinition.getId());
} catch (Exception e) {
LOGGER.info("启动失败");
}
repositoryService.activateProcessDefinitionById(processDefinition.getId());//流程激活
activitiRule.getRuntimeService().startProcessInstanceById(processDefinition.getId());
LOGGER.info("启动成功");
}
删除部署文件:repositoryService.deleteDeployment(deployment.getId(),true);
@Test
public void deleteProcess(){
RepositoryService repositoryService = activitiRule.getRepositoryService();
DeploymentQuery deploymentQuery = repositoryService.createDeploymentQuery();
List<Deployment> deploymentList = deploymentQuery
.orderByDeploymenTime().asc().listPage(0, 100);
for(Deployment deployment : deploymentList){
//LOGGER.info("deployment = {}", deployment);
repositoryService.deleteDeployment(deployment.getId(),true);
}
}
为流程定义文件添加用户和用户组:repositoryService.addCandidateStarterUser(processDefinition.getId(),"user");
@Test
//为流程定义文件添加用户和用户组,以及删除
@org.activiti.engine.test.Deployment(resources = {"my-process.bpmn20.xml"})
public void testCandidateStarter(){
RepositoryService repositoryService = activitiRule.getRepositoryService();
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().singleResult();
repositoryService.addCandidateStarterUser(processDefinition.getId(),"user");
repositoryService.addCandidateStarterGroup(processDefinition.getId(),"groupM");
List<IdentityLink> identityLinkList = repositoryService.getIdentityLinksForProcessDefinition(processDefinition.getId());
for(IdentityLink identityLink : identityLinkList){
LOGGER.info("identityLink: {}" , identityLink);
}
repositoryService.deleteCandidateStarterUser(processDefinition.getId(),"user");
repositoryService.deleteCandidateStarterGroup(processDefinition.getId(),"groupM");
List<IdentityLink> identityLinkList1 = repositoryService.getIdentityLinksForProcessDefinition(processDefinition.getId());
for(IdentityLink identityLink : identityLinkList1){
LOGGER.info("identityLink: {}" , identityLink);
}
}