Activiti基础学习笔记

Activiti

  • act_re_deployment 部署信息
  • act_re_procdef 流程定义的信息
  • act_ge_bytearray 流程定义bpmn与png文件
  • act_hi_actinst 已完成活动信息
  • act_hi_identitylink 参与者信息
  • act_hi_procinst 流程实例
  • act_hi_taskinst 任务实例
  • act_ru_excution 执行表
  • act_ru_identitylink 参与者信息
  • act_ru_task 任务

创建方法

方法一
//创建ProcessEngineConfiguration
ProcessEngineConfiguration config = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
//创建ProcesssEngine
ProcesssEngine processEngine = config.buildProcessEngine();
HistoryService historyService = processEngine.getHistroyService();

方法二
//创建ProcesssEngine
//配置文件名称 activiti.cfg.xml
//bean name: processEngineConfiguration
ProcesssEngine processEngine = ProcessEngine.getDefaultProcessEngine();
HistoryService historyService = processEngine.getHistroyService();

流程定义部署

方法一
//1 创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngine.getDefaultProcessEngine();
//2 得到RepositoryService 实例
RepositoryService repositoryService = processEngine.getRepositoryService();
//3 进行部署
Deployeement deployeement = repositoryService.createDeployment()
        .addClasspathResource("xxx.bpmn")
        .addClasspathResource("xxx.png")
        .name("xxx")
        .deploy();
//4 输出部署消息
方法二
//适用于部署文件上传到服务器
//1 创建ProcessEngine对象
ProcessEngine processEngine = ProcessEngine.getDefaultProcessEngine();
//2 得到RepositoryService 实例
RepositoryService repositoryService = processEngine.getRepositoryService();
//3 转换ZipInputStream流对象
InputStream is = xxx.class.getClassLoader().getResourceAsStream("xxx.zip");
// 将InputStream转换未ZipInputStream
ZipInputStream zipInputStream = new ZipInputStream(is);
//3 进行部署
Deployeement deployeement = repositoryService.createDeployment()
        .addZipInputStream(zipInputStream)
        .name("xxx")
        .deploy();
//4 输出部署消息

流程实例创建

//必须先完成流程定义的部署
//1 得到ProcessEngine对象
ProcessEngine processEngine = ProcessEngine.getDefaultProcessEngine();
//2 得到RunService对象
RuntimeService runtimeService = RunprocessEngine.getRuntimeService();
//3 创建流程实例 流程定义的Key
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("xxx");
//4 输出相关信息
//processInstance.getDeploymentId()
//processInstance.getId()

任务查询

//1 得到ProcessEngine对象
ProcessEngine processEngine = ProcessEngine.getDefaultProcessEngine();
//2 得到TaskService对象
TaskService taskService = processEngine.getTaskService();
//3 根据流程定义的Key负责人assignee来实现当前用户的任务列表查询
taskService.createTaskQuery()
    .processDefinitionKey("xxx")
    .taskAssignee("xxx")
    .list();
//4 任务列表展示
for(Task task : list){
    //task.getId()
    //task.getProcessInstanceId()
    //task.getName()
    //task.getAssignee()
}

任务处理

//1 得到ProcessEngine对象
ProcessEngine processEngine = ProcessEngine.getDefaultProcessEngine();
//2 得到TaskService对象
TaskService taskService = processEngine.getTaskService();
//3 处理任务,结合当前用户任务列表查询操作(taskId)
taskService.complete("xxxx");

流程定义查询

//1 得到ProcessEngine对象
ProcessEngine processEngine = ProcessEngine.getDefaultProcessEngine();
//2 创建RepositoryService对象
RepositoryService repositoryService = processEngine.getRepositoryService();
//3 得到ProcessDefinitionQuery对象(查询器)
ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
//4 设置条件,并查询楚当前所有流程定义
List list = processDefinitionQuery.processDefinitionKey("xxx")
    .orderByProcessDefinitionVersion()
    .desc().list();
//5 输出流程信息
for(ProcessDefinition processDefinition : list){
    //processDefinition.getId()
    //processDefinition.getName()
    //processDefinition.getKey()
    //processDefinition.getVersion()
    //processDefinition.getDeploymentId()
}

流程定义删除

/*
当流程未完全审批结束,如果删除流程定义会失败
如果需要强制删除
repositoryService.deleteDeployment("xxx",true)
会先删除未完成节点信息,最后删除流程定义
*/
//1 得到ProcessEngine对象
ProcessEngine processEngine = ProcessEngine.getDefaultProcessEngine();
//2 创建RepositoryService对象
RepositoryService repositoryService = processEngine.getRepositoryService();
//3 执行删除 流程部署Id
repositoryService.deleteDeployment("xxx");

资源文件操作

// 从服务器上获取资源文件
//1 得到ProcessEngine对象
ProcessEngine processEngine = ProcessEngine.getDefaultProcessEngine();
//2 创建RepositoryService对象
RepositoryService repositoryService = processEngine.getRepositoryService();
//3 得到ProcessDefinitionQuery对象(查询器)
ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
//4 设置查询条件
processDefinitionQuery.processDefinitionKey("xxx");
//5 执行查询条件
ProcessDefinition processDefinition = processDefinitionQuery.singleResult();
//6 通过流程定义信息,得到部署Id
String deployment = processDefinition.getDeploymentId();
//7 通过RepositoryService方法读取文件信息
InputStream pngIs = repositoryService
    .getResourceAsStream(deploymentId,processSefinition.getDiagramResourceName());
InputStream bpmngIs = repositoryService
    .getResourceAsStream(deploymentId,processSefinition.getResourceName());
//8 构造出OutputStream流
OutputStream pngOs = 
    new FileOutStream("xxx\\xx\\xx\\"+processSefinition.getDiagramResourceName());
OutputStream bpmnOs = 
    new FileOutStream("xxx\\xx\\xx\\"+processSefinition.getResourceName());
//9 输入流,输出流转换 commons-io-xx.jar
IOUtils.copy(pngIs,pngOs);
IOUtils.copy(bpmnIs,bpmnOs);
//10 关闭流
pngIs.close();
pngOs.close();
bpmnIs.close();
bpmnOs.close();

历史信息

//1 得到ProcessEngine对象
ProcessEngine processEngine = ProcessEngine.getDefaultProcessEngine();
//2 创建HistoryService对象
HistoryService historyService = processEngine.getHistoryServiceService();
//3 得到HistoricActivitiInstanceQuery对象(查询器)
HistoricActivitiInstanceQuery historicActivitiInstanceQuery = historyService.createHistoricActivitiInstanceQuery();
//4 设置查询条件
historicActivitiInstanceQuery.processInstanceId("xxx");
//5 执行查询
List list = historicActivitiInstanceQuery
    .orderByHIstoricActivityInstaceStartTime()
    .asc().list();
//6 遍历输出
for(HistoryActivityInstance instance : list){
    //instance.getActivityId()
    //instance.getActivityName()
    //instance.getProcessDefinitionId()
    //instance.getActivityId()
}

整合bussinessKey

//bussinessKey存入act_ra_execution
//1 得到ProcessEngine对象
ProcessEngine processEngine = ProcessEngine.getDefaultProcessEngine();
//2 得到RuntimeService对象
RuntimeService runtimeService = RunprocessEngine.getRuntimeService();
//3 创建流程实例 流程定义的Key,指定bussinessKey(业务Id)
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("xxx",bussinessKey);
//4 输出相关信息
//processInstance.getDeploymentId()
//processInstance.getId()
//processInstance.getBussinessKey()

流程激活与挂起

全部流程实例
//1 得到ProcessEngine对象
ProcessEngine processEngine = ProcessEngine.getDefaultProcessEngine();
//2 创建RepositoryService对象
RepositoryService repositoryService = processEngine.getRepositoryService();
//3 获取流程定义
//根据processDefinitionKey也可以获取
ProcessDefinition processDefinition = repositoryService
    .createProcessDefintionQuery()
    .processDefinitionId(processDefinitionId).singleResult();
//4 是否为暂停状态
boolean suspended = processDefinition.isSuspended();
//5 判断
if(suspended){
    //激活
    repositoryService
        .activiteProcessDefinitionById(processDefinition.getId(),true,null);
}else{
    //挂起
    repositoryService.suspendProcessDefinitionById(processDefinition.getId());
}
单个流程实例
//1 得到ProcessEngine对象
ProcessEngine processEngine = ProcessEngine.getDefaultProcessEngine();
//2 得到RuntimeService对象
RuntimeService runtimeService = RunprocessEngine.getRuntimeService();
//3 获取流程实例对象
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
    .processInstanceBussinessKey(bussinessKey)
    .singelResult();
//4 是否为暂停状态
boolean suspended = processInstance.isSuspended();
String processInstanceId = processInstance.getId();
//5 判断
if(suspended){
    //激活
    runtimeService.activiteProcessInstanceById(processInstanceId);
}else{
    //挂起
    runtimeService.suspendProcessInstanceById(processInstanceId);
}

进阶

Assignee UEL表达式赋值

//动态设置assignee
//1 得到ProcessEngine对象
ProcessEngine processEngine = ProcessEngine.getDefaultProcessEngine();
//2 得到RuntimeService对象
RuntimeService runtimeService = RunprocessEngine.getRuntimeService();
//3 设置assignee取值 
Map<String,Object> map = new HashMap<>();
map.put("assignee0","xxx");//与流程中UEL表达式一至${assignee0}
map.put("assignee1","xxx");//与流程中UEL表达式一至${assignee1}
map.put("assignee2","xxx");//与流程中UEL表达式一至${assignee2}
//启动流程实例,同时设置流程定义的assignee
ProcessInstance processInstance =  runtimeService.startProcessInstanceByKey(key,map);
//5 输出
//processEngine.getName();

监听器分配

//实现TaskListener接口
public class MyTaskListener implements TaskListener{
    public void notify(DelegateTask delegateTask){
        delegateTask.setAssignee("xxx");
    }
}

流程变量

全局变量
建立实体类
//请假实体
public class Holiday implements Serializable{
    private Integer id;
    private String holidayName;
    private Date beginDate;
    private Date endDate;
    private Float num;
    private String reason;
    private String type;
}
流程部署
//动态设置assignee
//1 得到ProcessEngine对象
ProcessEngine processEngine = ProcessEngine.getDefaultProcessEngine();
//2 创建RepositoryService对象
RepositoryService repositoryService = processEngine.getRepositoryService();
//3 部署
Deployment deploy = reposiytoryService.createDeployment()
    .addClasspathResource("xxx.bpmn")
    .addClasspathResource("xxxx.png")
    .name(deploymentName)
    .deploy();

启用流程,设置变量

####### 方法一

//1 得到ProcessEngine对象
ProcessEngine processEngine = ProcessEngine.getDefaultProcessEngine();
//2 得到RuntimeService对象
RuntimeService runtimeService = RunprocessEngine.getRuntimeService();
//3 流程定义Key
String key = "xxxx";
Map map = new HashMap();
Holiday holiday = new Holiday();
//${holiday.num>=3}
//${holiday.num<3}
holiday.setNum(1F);
map.put("holiday",holiday);
//4 启用流程实例 并且设置流程变量
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(key,map);
测试执行
//1 得到ProcessEngine对象
ProcessEngine processEngine = ProcessEngine.getDefaultProcessEngine();
//2 得到TaskService对象
TaskService taskService = RunprocessEngine.getTaskService();
//3 查询当前任务
String key = "xxxx";
Task task = taskService.createTaskQuery().processDefinitionKey(key)
    .taskAssignee("xxx")
    .singleResult();
//4 判断task!=null
if(task!=null){
    taskService.complete(task.getId());
}
其他方法设置

####### 方法二

//通过流程实例设置
//1 得到ProcessEngine对象
ProcessEngine processEngine = ProcessEngine.getDefaultProcessEngine();
//2 得到RuntimeService对象
RuntimeService runtimeService = RunprocessEngine.getRuntimeService();
//3 流程定义Key
String key = "xxxx";
Holiday holiday = new Holiday();
//${holiday.num>=3}
//${holiday.num<3}
holiday.setNum(1F);
//4 通过实例Id,设置流程变量
runtimeService.setVariable(instanceId,"holiday",holiday);
//runtimeService.setVariables(instanceId,variables);
结束当前任务时添加

####### 方法三


//1 得到ProcessEngine对象
ProcessEngine processEngine = ProcessEngine.getDefaultProcessEngine();
//2 得到TaskService对象
TaskService taskService = RunprocessEngine.getTaskService();
//3 查询当前任务
String key = "xxxx";
Task task = taskService.createTaskQuery().processDefinitionKey(key)
    .taskAssignee("xxx")
    .singleResult();
    
Map map = new HashMap();
Holiday holiday = new Holiday();
//${holiday.num>=3}
//${holiday.num<3}
holiday.setNum(1F);
map.put("holiday",holiday);

//4 判断task!=null
if(task!=null){
    taskService.complete(task.getId(),map);//完成当前任务,设置流程变量
}
通过任务id设置

####### 方法四

//1 得到ProcessEngine对象
ProcessEngine processEngine = ProcessEngine.getDefaultProcessEngine();
//2 得到TaskService对象
TaskService taskService = RunprocessEngine.getTaskService();
//3 查询当前任务
String key = "xxxx";
Task task = taskService.createTaskQuery().processDefinitionKey(key)
    .taskAssignee("xxx")
    .singleResult();
Holiday holiday = new Holiday();
//${holiday.num>=3}
//${holiday.num<3}
holiday.setNum(1F);

//4 设置流程变量
taskService.setVariable(taskId,"hiliday",holiday);
//taskService.setVariables(taskId,variables);

设置本地变量

//1 得到ProcessEngine对象
ProcessEngine processEngine = ProcessEngine.getDefaultProcessEngine();
//2 得到TaskService对象
TaskService taskService = RunprocessEngine.getTaskService();
Map map = new HashMap();
Holiday holiday = new Holiday();
//${holiday.num>=3}
//${holiday.num<3}
holiday.setNum(1F);
map.put("holiday",holiday);

//3 查询当前任务
String key = "xxxx";
Task task = taskService.createTaskQuery().processDefinitionKey(key)
    .taskAssignee("xxx")
    .singleResult();
//办理任务时设置,也可以直接设置
taskService.setVariablesLocal(task.getId(),map);
taskService.complete(taskId);

候选人

  • Candidate User

查询候选人

//1 得到ProcessEngine对象
ProcessEngine processEngine = ProcessEngine.getDefaultProcessEngine();
//2 得到TaskService对象
TaskService taskService = RunprocessEngine.getTaskService();
//3 查询组任务
List task = taskService.createTaskQuery()
    .processDefinitionKey("xxxx")
    .taskCandidateUser("xx")
    .list();

拾取

//1 得到ProcessEngine对象
ProcessEngine processEngine = ProcessEngine.getDefaultProcessEngine();
//2 得到TaskService对象
TaskService taskService = RunprocessEngine.getTaskService();
//3 查询组任务
String candidate_user = "xxxx";
Task task = taskService.createTaskQuery()
    .processDefinitionKey("xxxx")
    .taskCandidateUser("candidate_user")
    .singleResult();
//4 拾取
//拾取任务就是将候选人转换处理人(Assignee)
if(task!=null){
    taskService.claim(task.getId(),"candidate_user");
}
//5 重新查询任务
Task task = taskService.createTaskQuery()
    .processDefinitionKey("xxxx")
    .taskAssignee("candidate_user")
    .singleResult();
if(task!=null){
    taskService.complete(task.getId());
}

归还任务

//1 得到ProcessEngine对象
ProcessEngine processEngine = ProcessEngine.getDefaultProcessEngine();
//2 得到TaskService对象
TaskService taskService = RunprocessEngine.getTaskService();
//3 查询组任务
String candidate_user = "xxxx";
Task task = taskService.createTaskQuery()
    .processDefinitionKey("xxxx")
    .taskCandidateUser("candidate_user")
    .singleResult();
//4 拾取
//拾取任务就是将候选人转换处理人(Assignee)
if(task!=null){
    taskService.claim(task.getId(),"candidate_user");
}
//5 重新查询任务
Task task = taskService.createTaskQuery()
    .processDefinitionKey("xxxx")
    .taskAssignee("candidate_user")
    .singleResult();
if(task!=null){
    //taskService.complete(task.getId());
    taskService.setAssignee(task.getId(),null);
}

任务交接

//1 得到ProcessEngine对象
ProcessEngine processEngine = ProcessEngine.getDefaultProcessEngine();
//2 得到TaskService对象
TaskService taskService = RunprocessEngine.getTaskService();
//3 查询组任务
String candidate_user = "xxxx";
Task task = taskService.createTaskQuery()
    .processDefinitionKey("xxxx")
    .taskAssignee("candidate_user")
    .singleResult();
if(task!=null){
    //taskService.complete(task.getId());
    taskService.setAssignee(task.getId(),"xxxx");
    //交接任务 就是候选人拾取任务的过程
}

网关

  • 排他网关(异或(XOR)网关)
    • ExclusiveGadeWay
    • 只会选择一条条件为true的分支
    • 选择id小的那一条
  • 并行网关
    • ParalleGateway
    • 只有并行分支都完成后才能继续
    • 并行网关不会解析条件
  • 包含网关
    • InclusiveGatway
    • 并行网关与排他网关的结合
    • 满足条件的分支完成后才能继续

activiti7 api

  • ProcessRuntime
  • TaskRuntime

你可能感兴趣的:(activiti)