//创建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存入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
//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);//完成当前任务,设置流程变量
}
####### 方法四
//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);
//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");
//交接任务 就是候选人拾取任务的过程
}