一般先定义一个流程模型,也就是通过界面拖拉拽一个图形流程出来,然后保存。这个数据就是流程模型数据
repositoryService.newModel()
....
repositoryService.saveModel()
repositoryService.createModelQuery().listPage(firstResult,maxResults)
// 查询总数
repositoryService.createModelQuery().count()
repositoryService.deleteModel()
repository.createDeployment().name().deploy();
@Test
public void deployProcessByZip() throws FileNotFoundException {
// 流程部署 参与的表 act_re_procdef act_ge_bytearry act_re_deployment
// 加斜杠代表从classpah的根目录里面找文件
InputStream inputStream = this.getClass().getResourceAsStream("/processes/processes.zip");
// 部署流程
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
// 保存act_re_deployment
Deployment deployment = repositoryService.createDeployment()
.name("请假流程009")
.addZipInputStream(zipInputStream)
.deploy();
System.out.println("部署成功,流程部署id:" + deployment.getId());//57501
}
@Test
public void queryDeployment(){
List<Deployment> deployments = repositoryService.createDeploymentQuery().listPage(0, 4);
for (Deployment deployment : deployments) {
System.out.println("部署id:"+deployment.getId());
System.out.println("部署名称:"+deployment.getName());
System.out.println("部署时间:"+deployment.getDeploymentTime());
}
}
@Test
public void queryProcessDef(){
List<ProcessDefinition> processDefinitions = repositoryService.createProcessDefinitionQuery().listPage(0, 4);
for (ProcessDefinition processDefinition : processDefinitions) {
System.out.println("流程定义id:"+processDefinition.getId());
System.out.println("流程定义名称:"+processDefinition.getName());
System.out.println("流程定义bmp:"+processDefinition.getResourceName());
System.out.println("流程定义png:"+processDefinition.getDiagramResourceName());
System.out.println("部署id:"+processDefinition.getDeploymentId());
System.out.println("流程定义版本:"+processDefinition.getVersion());
}
}
同时删除流程定义
@Test
public void deleteDeploy(){
// 根据部署id删除流程定义,如果当前部署id有对应的流程定义运行的流程实例运行,那么会报错
repositoryService.deleteDeployment("1");
// 级联删除表,会删除正在执行的流程数据以及act_ru_*和act_hi_*里面的数据
// repositoryService.deleteDeployment("1",true);
}
@Test
public void queryProcessDragram() throws Exception {
//根据流程定义id查询流程定义的图片
InputStream processDiagram = repositoryService.getProcessDiagram("helloWorld:1:4");
File file = new File("./demo.png");
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
int len = 0;
byte[] bys = new byte[1024];
while((len = processDiagram.read(bys))!=0){
bufferedOutputStream.write(bys,0,len);
bufferedOutputStream.flush();
}
bufferedOutputStream.close();
processDiagram.close();
System.out.println("输出预览流程定义图片");
}
流程模型部署以后,对应生成流程定义。
一次部署,可以产生多条流程定义;
对应的表是act_re_deployment、 act_re_procdef
@Test
public void startProcessDef() {
// (1)通过processInstanceId启动
//ProcessInstance processInstance = runtimeService.startProcessInstanceById();
// (2) 通过processInstanceKey启动
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("helloWorld");
// (3)设置业务主键并通过processInstanceId启动流程,(重要,业务id)
// ProcessInstance startProcessInstanceById(String processDefinitionId, String businessKey);
// 获取流程实例id
System.out.println("流程实例id:" + processInstance.getId());
}
@Test
public void queryTask(){
// 分页查询张三需要办理的业务
List<Task> tasks = taskService.createTaskQuery().taskCandidateOrAssigned("张三").listPage(0, 4);
for (Task task : tasks) {
System.out.println("任务id:"+task.getId());
System.out.println("任务名称:"+task.getName());
System.out.println("任务办理人:"+task.getAssignee());
System.out.println("流程定义id:"+task.getProcessDefinitionId());
System.out.println("流程实例id:"+task.getProcessInstanceId());
System.out.println("执行实例id:"+task.getExecutionId());
}
}
@Test
public void completeTask(){
// 完成任务id为12505的任务
taskService.complete("12505");
}
@Test
public void isComplete(){
//判断流程实例对象是否还存在,不存在则已经结束
//备注:流程中所有任务完成后,流程实例表中的该流程数据全部会删除,则查询该流程实例返回的应该是空对象。
ProcessInstanceQuery processInstanceQuery = runtimeService.createProcessInstanceQuery().processInstanceId("12501");
if(processInstanceQuery!=null){
System.out.println("流程未结束");
}else{
System.out.println("流程已结束");
}
}
@Test
public void queryRunningProcessInstance(){
// 分页查询流程实例
List<ProcessInstance> processInstances = runtimeService.createProcessInstanceQuery().listPage(0, 4);
for (ProcessInstance processInstance : processInstances) {
System.out.println("执行实例id:"+processInstance.getId());
System.out.println("流程定义id:"+processInstance.getProcessDefinitionId());
System.out.println("流程部署id:"+processInstance.getDeploymentId());
}
}
@Test
public void queryHisotyProcess(){
List<HistoricProcessInstance> historicProcessInstances = historyService.createHistoricProcessInstanceQuery().listPage(0, 4);
for (HistoricProcessInstance historicProcessInstance : historicProcessInstances) {
System.out.println("执行实例id:"+historicProcessInstance.getId());
System.out.println("流程定义id:"+historicProcessInstance.getProcessDefinitionId());
System.out.println("部署id:"+historicProcessInstance.getDeploymentId());
System.out.println("流程定义名称:"+historicProcessInstance.getProcessDefinitionName());
System.out.println("流程定义版本:"+historicProcessInstance.getProcessDefinitionVersion());
}
}
/**
* 查询历史流程任务
*/
@Test
public void queryHistoryTask(){
List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery().list();
for (HistoricTaskInstance historicTaskInstance : list) {
System.out.println("任务id:"+historicTaskInstance.getId());
System.out.println("任务名称:"+historicTaskInstance.getName());
System.out.println("办理人:"+historicTaskInstance.getAssignee());
System.out.println("执行实例id:"+historicTaskInstance.getExecutionId());
System.out.println("流程定义id:"+historicTaskInstance.getProcessDefinitionId());
System.out.println("流程实例id:"+historicTaskInstance.getProcessInstanceId());
System.out.println("任务创建时间:"+historicTaskInstance.getCreateTime());
System.out.println("任务持续:"+historicTaskInstance.getDurationInMillis());
System.out.println(">>>>>>>>>>");
}
}
@Test
public void startProcessAndSetVariable() {
Map<String, Object> map = new HashMap<>(10);
map.put("请假天数", 5);
// 通过流程定义id启动流程并设置流程变量
// runtimeService.startProcessInstanceById("helloWorld:1:4",map);
// 通过流程定义key启动流程并设置流程变量
runtimeService.startProcessInstanceByKey("helloWorld", map);
}
@Test
public void runSetVariableByExecutionId(){
Map<String, Object> map = new HashMap<>(10);
map.put("请假天数", 10);
runtimeService.setVariables("17503",map);
}
@Test
public void setVariableByTaskId(){
Map<String, Object> map = new HashMap<>(10);
map.put("请假原因", "病假");
taskService.setVariables("17506",map);
}
@Test
public void getVariable(){
//通过执行实例id和key获取流程变量请假原因的值
String reason = (String)runtimeService.getVariable("17501","请假原因");
System.out.println(reason);
}
@Test
public void getVariableByHistory(){
// 通过执行实例id
List<HistoricVariableInstance> list = historyService.createHistoricVariableInstanceQuery().executionId("17501").list();
for (HistoricVariableInstance historicVariableInstance : list) {
System.out.println("变量名称:"+historicVariableInstance.getVariableName());
System.out.println("变量值:"+historicVariableInstance.getValue());
}
}
@Test
public void queryHistoryProcess(){
List<HistoricProcessInstance> list = historyService.createHistoricProcessInstanceQuery()
//查询已完成的
.finished()
.processDefinitionId("helloWorld:1:4")
.orderByProcessInstanceDuration().desc()
.listPage(0, 10);
for (HistoricProcessInstance historicProcessInstance : list) {
System.out.println("历史流程实例id:"+historicProcessInstance.getId());
System.out.println("流程定义id:"+historicProcessInstance.getProcessDefinitionId());
System.out.println("部署id:"+historicProcessInstance.getDeploymentId());
System.out.println("开始节点id:"+historicProcessInstance.getStartActivityId());
System.out.println("结束节点id:"+historicProcessInstance.getEndActivityId());
System.out.println("结束时间:"+historicProcessInstance.getEndTime());
System.out.println("耗费时间:"+historicProcessInstance.getDurationInMillis());
System.out.println("====================");
}
}
@Test
public void queryHistoryActiviti() {
List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery()
.processDefinitionId("helloWorld:1:4")
.finished()
.orderByHistoricActivityInstanceEndTime()
.desc()
.listPage(0, 10);
for (HistoricActivityInstance historicActivityInstance : list) {
System.out.println("id:" + historicActivityInstance.getId());
System.out.println("流程执行:" + historicActivityInstance.getExecutionId());
System.out.println("流程定义id:" + historicActivityInstance.getProcessDefinitionId());
System.out.println("流程实例id:" + historicActivityInstance.getProcessInstanceId());
System.out.println("活动id:" + historicActivityInstance.getActivityId());
System.out.println("活动类型:" + historicActivityInstance.getActivityType());
System.out.println("活动名称:" + historicActivityInstance.getActivityName());
System.out.println("办理人:" + historicActivityInstance.getAssignee());
System.out.println("开始时间:" + historicActivityInstance.getStartTime());
System.out.println("结束时间:" + historicActivityInstance.getEndTime());
System.out.println("持续时间:" + historicActivityInstance.getDurationInMillis());
System.out.println("==========================");
}
}
@Test
public void queryHistoryTask(){
// 查询刘备曾经办理的任务
List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery()
.finished()
.taskAssignee("刘备")
.listPage(0, 10);
for (HistoricTaskInstance historicTaskInstance : list) {
System.out.println("任务id:"+historicTaskInstance.getId());
System.out.println("任务名称:"+historicTaskInstance.getName());
System.out.println("办理人:"+historicTaskInstance.getAssignee());
System.out.println("创建时间:"+historicTaskInstance.getCreateTime());
System.out.println("结束时间:"+historicTaskInstance.getEndTime());
System.out.println("流程实例id:"+historicTaskInstance.getProcessInstanceId());
System.out.println("流程定义id:"+historicTaskInstance.getProcessDefinitionId());
System.out.println("执行实例id:"+historicTaskInstance.getExecutionId());
System.out.println("持续时间:"+historicTaskInstance.getDurationInMillis());
System.out.println("=======================");
}
}
https://www.activiti.org/userguide/#chapterApi