Activiti工作流开发——Activiti相关概念

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31728311/article/details/78163837

Activiti对流程的描述是基于数据库表的,Activiti有23张表,分别用来储存工作流各个阶段的数据,各个表的数据字段和作用请参考blog Activiti数据表结构说明

Activiti工作流的核心是一个叫ProcessEngine的接口,这个接口控制着整个工作流,其中工作流引擎又包含着若干个事务,比如有注册事服务(可以部署流程,也可以查看部署的流程和流程定义、删除一个流程等等),运行时服务(可以启动一个流程实例,查看流程实例的相关信息)、任务服务(可以查看任务、提交任务等等)、历史服务(可以查看流程历史相关信息),这些服务通过api向开发者提供了完整的流程控制操作与信息查看的方式,而且Activiti的接口风格非常人性化,还充分实现了链式编程的接口风格。下面是一个非常简单的流程

下面是一些Demo代码

package com.csu.software.benson;


import java.io.File;
import java.io.IOException;
import java.util.List;


import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;


public class HelloWorld {
private ProcessEngine processEngine;
@Before
public void before() {
processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg.xml").buildProcessEngine();
}
/**
* 部署流程
*/
@Test
public void deploymentProcessDefinition() {
Deployment deployment = processEngine.getRepositoryService().createDeployment().name("helloword")
.addClasspathResource("diagrams/HelloWorld.bpmn").addClasspathResource("diagrams/HelloWorld.png")
.deploy();
System.out.println(deployment.getId());
System.out.println(deployment.getName());
}
/**
* 启动流程实例
*/
@Test
public void startProcessInstance() {
ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey("helloworld");
System.out.println("processInstance id="+processInstance.getId());
System.out.println("processDefinition id="+processInstance.getProcessDefinitionId());
}
/**
* 查看流程实例
*/
@Test
public void findProcessInstance() {
System.out.println(processEngine.getRuntimeService().createProcessInstanceQuery().processInstanceId("901"));

}
/**
* 通过流程实例id获取任务
*/
@Test
public void checkTaskByProcessInstanceId() {
Task task = processEngine.getTaskService().createTaskQuery().processInstanceId("901").singleResult();
if(task==null){
System.out.println("该流程实例已经完成");
return;
}
System.out.println(task.getName());
}
/**
* 提交流程任务
*/
@Test
public void compeleteTaskByProcessInstanceId() {
Task task = processEngine.getTaskService().createTaskQuery().processInstanceId("901").singleResult();
System.out.println("当前任务="+task.getName());
processEngine.getTaskService().complete(task.getId());
task = processEngine.getTaskService().createTaskQuery().processInstanceId("901").singleResult();
System.out.println("下一个任务="+task.getName());
}

/**
* 根据流程实例id获取任务历史记录
*/
@Test
public void searchTaskHistoryByProcessInstanceId() {
List<HistoricTaskInstance> list = processEngine.getHistoryService().createHistoricTaskInstanceQuery().processInstanceId("901").list();
if(list!=null){
for(int i=0;i<list.size();i++) {
HistoricTaskInstance taskInstance = list.get(i);
System.out.println("taskInstanceName="+taskInstance.getName());
System.out.println("taskInstanceId="+taskInstance.getId());
System.out.println("taskInstanceStartTime="+taskInstance.getStartTime());
System.out.println("taskInstanceEndTime="+taskInstance.getEndTime());
}
}
}
/**
* 查询当前人的个人任务
*/
@Test
public void  findTask() {
Task task = processEngine.getTaskService().createTaskQuery().processDefinitionKey("helloworld").singleResult();
System.out.println("task id"+task.getId());
System.out.println("assignee="+task.getAssignee());
System.out.println("taskname="+task.getName());
System.out.println("priority="+task.getPriority());
System.out.println("create time="+task.getCreateTime());
}
/**
* 给Task设置变量
*/
@Test
public void SetTaskVariable() {
Task task = processEngine.getTaskService().createTaskQuery().processDefinitionKey("helloworld").singleResult();
processEngine.getTaskService().setVariable(task.getId(),"variable","variable");
processEngine.getTaskService().setVariableLocal(task.getId(), "variableLocal", "variableLocal");
}
/**
* 获取Task的变量
*/
@Test
public void GetTaskVariable() {
Task task = processEngine.getTaskService().createTaskQuery().processDefinitionKey("helloworld").singleResult();
System.out.println(processEngine.getTaskService().getVariable(task.getId(), "variable"));
System.out.println(processEngine.getTaskService().getVariableLocal(task.getId(), "variableLocal"));
}
/**
* 完成任务
*/
@Test
public void completeTask() {
Task task = processEngine.getTaskService().createTaskQuery().processDefinitionKey("helloworld").singleResult();
System.out.println(task.getName());
processEngine.getTaskService().complete(task.getId());
task = processEngine.getTaskService().createTaskQuery().processDefinitionKey("helloworld").singleResult();
System.out.println(task.getName());
}
/**
* 获取任务的历史记录
*/
@Test
public void searchTaskHistory() {
Task task = processEngine.getTaskService().createTaskQuery().processDefinitionKey("helloworld").singleResult();
List<HistoricTaskInstance> vars=processEngine.getHistoryService().createHistoricTaskInstanceQuery().taskDefinitionKey(task.getTaskDefinitionKey()).list();
System.out.println(vars.size());
if(vars!=null) {
for(HistoricTaskInstance var:vars){
System.out.println(var.getName());
System.out.println(var.getStartTime());
System.out.println(var.getEndTime());
}
}
}

/**
* 查询流程定义
*/
@Test
public void findProcessDefinition() {
List<ProcessDefinition> list = processEngine.getRepositoryService().createProcessDefinitionQuery().orderByProcessDefinitionVersion().asc().list();
if(list!=null) {
for(int i=0;i<list.size();i++) {
ProcessDefinition definition = list.get(i);
System.out.println(definition.getId());
System.out.println(definition.getName());
System.out.println(definition.getKey());
System.out.println(definition.getDeploymentId());
System.out.println(definition.getVersion());
System.out.println(definition.getResourceName());
System.out.println(definition.getDiagramResourceName());
System.out.println(definition.getDeploymentId());
System.out.println("##################################");
}
}
}
/**
* 删除流程定义
*/
@Test
public void deleteProcessDefinition() {
String deploymentId = "";
processEngine.getRepositoryService().deleteDeployment(deploymentId,true);
}
/**
* 查看流程图片
*/
@Test
public void viewPic() {
List<String> list = processEngine.getRepositoryService().getDeploymentResourceNames("1");
if(list!=null) {
for(int i=0;i<list.size();i++) {
System.out.println(list.get(i));
if(list.get(i).endsWith(".png")) {
try {
File file = new File("helloworld.png");
System.out.println(file.getAbsolutePath());
FileUtils.copyInputStreamToFile(processEngine.getRepositoryService().getResourceAsStream("1",list.get(i)),file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}


其实可以看到,对工作流的描述和控制都是基于前面提到的23张表的,要理解清楚工作流的内部机制就要好好地去研究这23张表之间的关系和每个处理过程每张表的变化。

阅读更多
相关热词
换一批

没有更多推荐了,返回首页